Solved

declare statment

Posted on 2006-11-13
5
2,898 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

710 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