Solved

declare statment

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

617 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