Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Execute dynamically build query

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-08-22 : 07:01:01
Vinod writes "I am building dynamic SQL statement in @SQL variable. I want to execute this statement and want to get the result in some variable say @REC_COUNT. Below is the sample code...

USE Northwind
GO

DECLARE @REC_COUNT INT
DECLARE @SQL NVARCHAR(255)

SET @SQL = 'SELECT COUNT(*) FROM Employees'
EXECUTE sp_executeSQL @SQL

Can anybody have any idea? How can I get the value in @REC_COUNT variable..

Thanks in advance
Vinod"

nr
SQLTeam MVY

12543 Posts

Posted - 2005-08-22 : 08:31:10
declare @i int
SET @SQL = 'SELECT @i = COUNT(*) FROM Employees'
EXECUTE sp_executeSQL @SQL, N'@i int out', @i out

see
http://www.mindsdoor.net/SQLTsql/sp_executeSQL.html

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -