[FireDAC][Phys][MSSQL]-310._数据库安装工具_连载_3

//先来看看我们碰到的问题,再来求解答
SQL脚本执行失败,[FireDAC][Phys][MSSQL]-310. Cannot execute command returning result sets. Hint: use Open method for SELECT-like commands
在执行 ExecSQL之前,代码中增加此句可解决问题   (  FDQuery1.Command.CommandKind:=skUpdate; )

Problem getting FireDAC to execute a query with Common Table Expressions

 
I was writing some queries to crunch data to export to a spreadsheet for some users. Since I was flattening out the data into a non-normal form, I had some queries that would apply some redundant aggregate counts and averages to every occurrence of an employee in the dataset. These queries used SQL Server Common Table Expressions (CTE) to build a temporary named result set of aggregate data to join to my primary data in order to update it. These queries ran great from SQL Server Management Studio.

When I would try to run them from the utility I was writing, FireDAC appears to be confused by the syntax. If I ran the query using ExecSQL it would return the error message:

[FireDAC][Phys][MSSQL]-310. Cannot execute command returning result sets.
Hint: use Open method for SELECT-like commands.

If I followed the advice of the error message and used OpenOrExecute it would return the error message:

[FireDAC][Phys][MSSQL]-310. Cannot open / define command, which does not return result sets.
Hint: use Execute / ExecSQL method for non-SELECT commands.

I tried every variation of using a TFDQuery or a direct execute from a TFDConnection that I could think of. But FireDAC would always return one of the two error messages above.

The specific query that I was running is as follows:

WITH MED_AVG_CTE AS
(
    SELECT EMPLOYEE_NBR, AVG(MEDIAN_PERCENT) AS MED_AVG
    FROM   TEMP_CAD_ONS_HIT_RATE
	WHERE  MEDIAN_PERCENT IS NOT NULL
    GROUP BY EMPLOYEE_NBR
)
UPDATE TEMP_CAD_ONS_HIT_RATE
SET    MEDIAN_AVERAGE = MED_AVG
FROM   MED_AVG_CTE
WHERE  TEMP_CAD_ONS_HIT_RATE.EMPLOYEE_NBR = MED_AVG_CTE.EMPLOYEE_NBR
  
This query uses a SELECT statement with an AVG function to create a CTE result set called MED_AVG_CTE. Then it does an UPDATE by joining this newly created CTE to the data that I'm currently crunching. So while this command does have a SELECT - FROM - WHERE statement in it, it is a pure update command / action query that does not return a result set.  

After beating my head against a wall for a day on this, I finally came up with a workable hack by sticking in a "OUTPUT DELETED.MEDIAN" line to explicitly make the update command return a result set of a single field of data that I simply ignore. I can now execute the update using an OpenOrExecute() command.

WITH MED_AVG_CTE AS
(
    SELECT EMPLOYEE_NBR, AVG(MEDIAN_PERCENT) AS MED_AVG
    FROM   TEMP_CAD_ONS_HIT_RATE
	WHERE  MEDIAN_PERCENT IS NOT NULL
    GROUP BY EMPLOYEE_NBR
)
UPDATE TEMP_CAD_ONS_HIT_RATE
SET    MEDIAN_AVERAGE = MED_AVG
OUTPUT DELETED.MEDIAN  -- Result set created here
FROM   MED_AVG_CTE
WHERE  TEMP_CAD_ONS_HIT_RATE.EMPLOYEE_NBR = MED_AVG_CTE.EMPLOYEE_NBR
  
My question is: Is there any way to submit an SQL statement to a server where FireDAC will not try and inspect the command being run to determine whether it returns a result set or is an action query? I want FireDAC to simply submit what I tell it to run without question (or error). Is there a flag, property, or method parameter that I'm missing somewhere? Or is this simply a case where FireDAC can't be expected to understand all the various syntax options of all the databases it connects to?

Thanks for any help or insight you might have.
0
Darryl
11/5/2014 6:30:11 PM
embarcadero.delphi.firedac 822 articles. 2 followers.

1 Replies
3229 Views

Similar Articles

[PageSpeed] 58
 
Set FDQuery.Command.CommandKind to skUpdate before calling ExecSQL.

-- 
With best regards,
Dmitry Arefiev / FireDAC Architect
-1
Dmitry
11/7/2014 8:43:38 AM
Reply:
 
 
 


Similar Artilces:

FireDAC and Delphi Pro XE4... firedac tab not visible?
I am moving from a very old Borland product (2006) to Delphi XE4 Professional with FireDAC addon. I purchased both from Embarcadero, and have downloaded both. The documentation says the FireDAC components should automagically appear in the tabset for Delphi. They do not. I don't want to fool around too much as my environment is strictly controlled by IT, and they are very slow to do anything. Is there something that I should have done, but did not do? I installed XE4 first, registered it, then used the link in my email from Embarcadero to download the FireDAC components. Then I ra...

The problem about Common Table Expression (CTE)
Could you tell me what different there are between Part 1 and Part 2 ?  Thanks! //------------------------Part 1 ---------------------------------ALTER PROCEDURE [HD_ToDoList_ListForPageByUserID]( @UserId uniqueidentifier, @StartIndex int, @PageSize int)AS   With Temp As(     select ROW_NUMBER() Over (Order by CreateDate Desc)     As Row, ToDoListID, DeskID,UserId,CreateDate,Subject,Description     From HD_ToDoList     where UserId=@UserId   )  SELECT ToDoListID,...

FireDac Delphi XE3 installation problem
Hi all, I have just purchased the FireDAC package and try to install the package. I tried to install AnyDAC_D7.bpg package to XE3 and I got this error message. Required package 'designdgm' not found. Can someone assist me about the problem? Thanks. > {quote:title=Cem Zafer Demirsoy wrote:}{quote} > Hi all, > I have just purchased the FireDAC package and try to install the package. I tried to install AnyDAC_D7.bpg package to XE3 and I got this error message. > Required package 'designdgm' not found. > Can someone assist me about the problem? > Thanks...

DB2 Common Table Expression + DBD::DB2 problem
I have several Perl scripts (currently being used under Win2k + ActiveState + DBI 1.14 / DBD-DB2 0.75) which use DB2 "common table expressions" (WITH CTE( .. ) UNION ALL .. fullselect ) which have been running just fine for about 6 months or more now. A production DB2 server had to be upgraded to FixPack 9 (this is all DB2 v6.1), and so I dutifully upgraded my local development machine to Fixpack 9 as well (I have server + local copy of production data). Now all Perl scripts work fine until they try and execute() a prepared statement that includes a common table expression...

FireDAC problem
This message is in MIME format. Since your mail reader does not understand this format, some or all of this message may not be legible. --JivePart=_8ec8c.zeB8CwDOH5aMJzMl Content-Type: text/plain; charset="Utf-8" For Dmitry --JivePart=_8ec8c.zeB8CwDOH5aMJzMl Content-Type: application/x-zip-compressed; name="FireDACproblem.zip" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="FireDACproblem.zip" UEsDBBQAAgAIAAdUBUNCTTjuKAQAAK0TAAAOAAAAZG1UdW5ndmVqMS5kZm3tV21v2zYQ/j5g/4Hf tGFG4XhOmrbzANcvm7GktiOlGZAVBWOebXaSqJKUU+/X...

FireDac fields in query from joined tables (or UNIONS) become read-only
Hi, I'm seeing that for queries I build that join multiple tables, the fields in secondary tables are marked read-only in the resulting clientdataset (via a TADQuery and a provider). I'm updating these tables manually so I don't want the fields set ReadOnly. I notice there are a lot of options in the TADQuery component for controlling read-only settings, but I haven't found the combination that works for my queries. Any suggestions? Thanks, Dan Palley Hello Dan Check the topic: http://docwiki.embarcadero.com/Libraries/XE5/en/FireDAC.Stan.Option.TFDUpdateOpti...

Possible to get field names and types in a table without executing a query?
------_=_NextPart_001_01C699F7.A9E61529 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi all. My goal is to get a list of all field names and data types for those fields in any given table in a mysql database. From reading the DBI documentation, the only way I've been able to do this is by preparing and executing a query against a table first. Then I would use $sth->{NAME} and $sth->{TYPE} to get the field names and their respective data types. it looks like this: =20 $sth =3D $dbh->prepare("SELECT * FROM $tab...

Delphi XE6 FireDAC SQLite copy a table from one database to another
How do I copy a single table from one SQLite database to another? Is there a simple command to do this? Thanks! > How do I copy a single table from one SQLite database to another? Is there a simple command to do this? Thanks! You should use ATTACH command. For example: {code} ATTACH 'c:\secondDB.sdb' AS sec; INSERT INTO SomeTab SELECT * FROM sec.SomeTab; {code} -- With best regards, Dmitry Arefiev / FireDAC Architect Thanks! > {quote:title=Dmitry Arefiev wrote:}{quote} > > How do I copy a single table from one SQLite database to another? Is there a...

Re: Possible to get field names and types in a table without executing a query?
> It doesn't matter what fetchall_hashref() returns. You get the list of > columns from $sth->{NAME}. Cool. ...

FireDac
Hello, I am using Delphi XE4 and facing a problem with FireDac (8.0.5) and MySQL. I'm trying to get the last 100 records from a large table (20 million rows). When I run the following SQL: {code}select * from largetable order by FId limit 100{code} in MySQL Workbench it returns in 0.2 seconds. When I run the above SQL in Delphi through an ADQuery it never returns (Delphi freezes). Any Ideas ? Thank you ...

FireDac, table not found (wrong table owner) [Edit]
Hello, I am testing an small project with Delphi XE7 and Firedac. My DB system is SQL Anywhere Version 11. I created the DB connection like this: FDConnection1 (with Login Dialog) -> FDQuery1 (SQL: select Titel from DOC where docid = '000000000' -> Datasource1 -> DBEdit1 (for editing Column 'Titel') Connection to DB, selecting, etc. works perfect. But when i try to edit and post the new value, FireDac says 'Table DOC not found'... The Table 'DOC' has the (default) owner named 'olympia'. When i analyze the update statement with FDMonito...

FireDAC Interbase SQL execution fails when IBScript execution does not
Hello, I am making a table in Interbase XE3 using FireDAC that looks like the following: {code} CREATE TABLE "CardTypes" ( "CardTypeID" INTEGER DEFAULT 1 NOT NULL, "CardTypeName" VARCHAR(30) NOT NULL, PRIMARY KEY ("CardTypeID") ); /* Generator and Trigger for CardTypeID */ CREATE GENERATOR "GenCardID"; SET GENERATOR "GenCardID" TO 100; CREATE TRIGGER "IncCardID" FOR "CardTypes" BEFORE INSERT AS BEGIN NEW."CardTypeID" = GEN_ID("GenCardID", 1); END; INSERT INTO &q...

Problems using Oracle Proxy Tables in queries to ASA tables
I have created a remote server to an oracle database from a asa9 database. Using Powerbuilder10 to create a query joining a table from the oracle proxy table to a asa table does not work. Are there any parameters that should be modifed that anyone is aware of in either ASA or Powerbuilder Can you be more specific than "does not work"? What error do you get? -- Reg Domaratzki, Sybase iAnywhere Solutions Sybase Certified Professional - Sybase ASA Developer Version 8 Please reply only to the newsgroup iAnywhere Developer Community : http://www.ianywhere.com/developer ...

How can I query a table to get the result in a table-like format ?
Dear all, I have a table something like this: emp_id, hire_date (yyyy/mm/dd), salary_band, profit_centre,..... I need obtain the following info out of this table: counts of employees by month for each salary band, ie a cross-table like this: Jan Feb Mar..... Total Year Salary band 1 Salary band 2 ..... Total Is it possible to get this type of info with one query ? Shall I write one query where band is parameter, and execute it for each month ? (I tried a "group by datepart(month)" but did not get the expected count). What is t...

猜你喜欢

转载自www.cnblogs.com/CDPJ/p/12748542.html