Wednesday, March 21, 2012

Qusetion about return values from EXEC('select count(*) from xTable')

Hello everybody!

As the topic:

Can i get the value "count(*)" from EXEC('select count(*) from xTable')

Any helps will be usefull! Thanks!

Hi,

Yes you can do that. it will return a table with one column and a row. Please make sure to specify column name for count(*)

'select count(*) as RowCount from xTable'

|||

How about this ;

create table #results (
cnt int
)

insert #results
exec ('select count(*) from master..sysdatabases')

select cnt from #results

|||

Hi

You can return a parameter from executing a string , but you need to use sp_executesql , like

DECLARE @.iCount INT

EXEC sp_executesql N'SELECT @.iCount= COUNT(*) FROM xTable', 'N'@.iCount INT OUT', @.iCount OUT

SELECT @.iCount

|||

Thanks Shallu, Rod Colledge, and NB2006.

I will try to test whether it works. Thanks !!

Not good at english! Sorry!!

No comments:

Post a Comment