Solved

declare statment

Posted on 2006-11-13
5
2,890 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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

832 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