Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
DECLARE @sqlCommand nvarchar(1000)

DECLARE @countnumber int = 0

SET @sqlCommand = 'Set @countnumber = (SELECT count(*) from dsnv)'

EXEC (@sqlCommand)

print @countnumber ;

But i get error

What I have tried:

SQL
DECLARE @sqlCommand nvarchar(1000)

DECLARE @countnumber int = 0

SET @sqlCommand = 'SELECT count(*) into @countnumber from dsnv'

EXEC (@sqlCommand)

print @countnumber ;

But i get error
Posted
Updated 24-Jun-16 18:45pm
v2

1 solution

try this, using sp_executesql[^]

SQL
DECLARE @sqlCommand nvarchar(1000)
DECLARE @countnumber int = 0
SET @sqlCommand = 'SELECT @countnumber=count(*) from [dsnv]'
EXEC sp_executesql  @sqlCommand ,N'@countnumber int out' , @countnumber out
select @countnumber ;
print @countnumber
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900