Jump to content
thomh

TFDQuery - Query all databases on a server

Recommended Posts

Hi,

I have the following SQL which queries all DBs on a server:

 

declare @SQL nvarchar(max)

set @SQL = STUFF((SELECT '

UNION ALL

' + 
'SELECT COUNT(DISTINCT DocNo) AS DocCount,
' + quotename(name,'''') + ' AS ClientDatabase
FROM ' + quotename(name) + '.dbo.TestData
WHERE Flags & 2 = 0
AND (Flags & 4 = 0 OR Flags & 128 = 128)
AND Flags & 2048 = 0
AND Flags & 268435456 = 0'
from sys.Databases
WHERE name IN ('C003','C100','C200')
FOR XML PATH(''),
type).value('.','varchar(max)'),1,15,'')

execute(@SQL)

 

Using it in Microsoft SQL Server Management Studio in returns the correct result set but when I use the FireDAC TFDQuery it returns the following error on the Open command:

 

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

 

Any ideas how to get this SQL statement to work in FireDAC?

 

Thanks,
Thom
 

Share this post


Link to post

Hi haentschman,

 

Thought TFDScript was for SQL commands like INSERT, UPDATE, etc. How would you write such a script and loop the result set using TFDScript?

 

// Thom

 

Share this post


Link to post

Mystery solved.

 

With this particular SQL statement, TFDQuery returns this error message when the result set is empty. 

 

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

 

// Thom
 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×