p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   SQL Server 2005 (http://p2p.wrox.com/forumdisplay.php?f=220)
-   -   get the execute query result in variable (http://p2p.wrox.com/showthread.php?t=67170)

mahen_pali April 5th, 2008 03:43 AM

get the execute query result in variable
hello all of you

i want to get the execute query result in variable
Here is code

declare @Trans varchar(50)
set @Trans = 'Trans'
declare @val int
set @val = (execute('select count(*) from '+@Trans))

How can i solve that please help me soon

mahendra singh

Preethi April 7th, 2008 03:41 AM

To Get the value back to a variable you should use sp_ExecuteSQL
Here is an example of what I tried against AdventureWorks

DECLARE @SQL nvarchar(2000), @Rows int
SET @SQL =N'SELECT @RowCount =COUNT(*) FROM '+ +'Sales.SalesOrderHeader'
EXEC sp_ExecuteSQL @SQL, N'@RowCount int OUTPUT',@RowCount=@Rows OUTPUT

I purposefully used @RowCount and @Rows to differentiate them. Hope this helps

G.R.Preethiviraj Kulasingham

mahen_pali April 7th, 2008 04:04 AM

My hearty thanks to Preethi

My problem have solved

mahendra singh

All times are GMT -4. The time now is 08:39 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.