Solved

declare statment

Posted on 2006-11-13
5
2,884 Views
Last Modified: 2012-06-21
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
Comment
Question by:PradeepYadhav
5 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 17929004
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
 
LVL 9

Expert Comment

by:dduser
ID: 17929016
declare @countme int
declare @sql varchar(300)
Set @sql='select count(*) from temptable123'
Execute sp_executesql @sql,@countme out
print @countme

Regards,

dduser
0
 
LVL 6

Expert Comment

by:Gokulm
ID: 17929029
You need to declare @sql as nvarchar
0
 

Author Comment

by:PradeepYadhav
ID: 17929085
how to deal with multiple parameters

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

Expert Comment

by:deroby
ID: 17929095
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now