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 sp_executesql @Stat how to get output

Author  Topic 

adhik
Yak Posting Veteran

66 Posts

Posted - 2003-03-28 : 01:10:24
Hi Professionals,

I am creating one runtime query in @Stat variable of size varchar(4000)
This is of the count(*) type query.
e.g.
set @Stat = 'Select Count(*) from '+@TableName+' where compId ='+ Cast(compId as varchar(10))
Here @TableName is selected on some conditions in main procedure, i.e. why i need to create run time query.

Now i m executing this @Stat using execute sp_executesql in my main procedure.

In Books online sp_excutesql gives following Remarks
Local cursors or variables in the sp_executesql batch are not visible to the batch calling sp_executesql.

but i m not able to get count(*) value in main procedure.
How can i achive this.....??

Thanks in advance
Adhik Kadam


nr
SQLTeam MVY

12543 Posts

Posted - 2003-03-28 : 05:29:53
select @tblname = 'mytbl'

declare @sql nvarchar(1000) ,
@cnt int
select @sql = 'select @cnt = count(*) from ' + @tblname
exec sp_executesql @sql, N'@cnt int', @cnt out

alternatively
select @cnt = select rows from sysindexes where id = object_id(@tblname) and indid in (0,1)

see
www.nigelrivett.com
sp_executeSQL


==========================================
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

adhik
Yak Posting Veteran

66 Posts

Posted - 2003-03-28 : 05:54:24
Thanks a lot....

it works....

regards
Adhik


Go to Top of Page
   

- Advertisement -