Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2912
  • Last Modified:

declare statment

declare @countme int
declare @sql varchar(300)
SELECT @sql='select @countme=count(*) from temptable123'
exec(@sql)
print @countme

this gives an error saying must declare @countme

how to accomplish this
0
PradeepYadhav
Asked:
PradeepYadhav
1 Solution
 
Aneesh RetnakaranDatabase AdministratorCommented:
declare @countme int
declare @sql nvarchar(300)
SELECT @sql='select @countme=count(*) from temptable123'
EXEC sp_executeSQL @SQl, N'@countme int Out',@Countme out
print @countme

0
 
dduserCommented:
declare @countme int
declare @sql varchar(300)
Set @sql='select count(*) from temptable123'
Execute sp_executesql @sql,@countme out
print @countme

Regards,

dduser
0
 
GokulmCommented:
You need to declare @sql as nvarchar
0
 
PradeepYadhavAuthor Commented:
how to deal with multiple parameters

SELECT @sql='select @countme=count(*), @count1=count(*) from '+ @tempval
0
 
derobyCommented:
exactly the same :

Like aneeshattingal said (points to him, not me please)

declare @countme int
declare @count1 int
declare @sql nvarchar(500)

SELECT @sql='select @countme=count(*), @count1=count(*) from '+ @tempval

EXEC sp_executeSQL @SQl, N'@countme int Output @count1 int output',@countme output, @count1 output

SELECT @countme, @count1

I suggest you also have a look at the documentation regarding sp_executsql to understand what's going on here... .
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now