How to use DECLARE/SET in a query and get the result?
Posted: Mon 20 Oct 2008 21:32
Hi Greetings!
I am exploring SQLDirect.Net in VS2008. Following is my query against any database. How do I get the result set?
I am trying with:
a. SqlConnection
b. SqlDataTable
c. SqlCommand
Please note, I have DECLARE and SET statement in my SQL. What is the best way to get result set without removing DECLARE and SET keyword?
DECLARE @dbid int
SET @dbid = db_id()
SELECT object_name(i.object_id) TableNme,
i.name IndexName,
i.index_id indexID,
i.type_desc TypeDesc,
c.index_columns IndexedColumn
FROM sys.indexes i
LEFT OUTER JOIN sys.dm_db_index_usage_stats d
ON d.object_id = i.object_id
AND i.index_id = d.index_id
AND d.database_id = @dbid
LEFT OUTER JOIN (
SELECT DISTINCT object_id,
index_id,
stuff((SELECT ','+col_name(object_id,column_id ) AS 'data()'
FROM sys.index_columns t2
WHERE t1.object_id = t2.object_id
AND t1.index_id = t2.index_id
FOR XML PATH ('')),1,1,'') AS 'index_columns'
FROM sys.index_columns t1
) c
ON c.index_id = i.index_id
AND c.object_id = i.object_id
WHERE objectproperty(i.object_id, 'IsIndexable') = 1
AND d.index_id IS NULL
AND i.type_desc NOT IN ('heap', 'clustered')
I am exploring SQLDirect.Net in VS2008. Following is my query against any database. How do I get the result set?
I am trying with:
a. SqlConnection
b. SqlDataTable
c. SqlCommand
Please note, I have DECLARE and SET statement in my SQL. What is the best way to get result set without removing DECLARE and SET keyword?
DECLARE @dbid int
SET @dbid = db_id()
SELECT object_name(i.object_id) TableNme,
i.name IndexName,
i.index_id indexID,
i.type_desc TypeDesc,
c.index_columns IndexedColumn
FROM sys.indexes i
LEFT OUTER JOIN sys.dm_db_index_usage_stats d
ON d.object_id = i.object_id
AND i.index_id = d.index_id
AND d.database_id = @dbid
LEFT OUTER JOIN (
SELECT DISTINCT object_id,
index_id,
stuff((SELECT ','+col_name(object_id,column_id ) AS 'data()'
FROM sys.index_columns t2
WHERE t1.object_id = t2.object_id
AND t1.index_id = t2.index_id
FOR XML PATH ('')),1,1,'') AS 'index_columns'
FROM sys.index_columns t1
) c
ON c.index_id = i.index_id
AND c.object_id = i.object_id
WHERE objectproperty(i.object_id, 'IsIndexable') = 1
AND d.index_id IS NULL
AND i.type_desc NOT IN ('heap', 'clustered')