存储过程变量申明 Must declare the scalar variable "@var1".
来源:互联网 发布:网络运营总监岗位职责 编辑:程序博客网 时间:2024/05/16 01:59
http://forums.devshed.com/ms-sql-development-95/stored-procedure-must-declare-the-scalar-variable-var1t-403571.html
Hi all, when i run this query i get the following error "Must declare the scalar variable "@var1". What am i doing wrong?
Stored procedure - Must declare the scalar variable "@var1".
Hi all, when i run this query i get the following error "Must declare the scalar variable "@var1". What am i doing wrong?
Code:
set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[admin3c]-- Add the parameters for the stored procedure here@tableName varchar(100),@authorityId varchar(50)ASBEGINSET NOCOUNT ON;Declare @SQL VarChar(2000),@var1 varchar(50),@var2 int,@total intSELECT @SQL = 'select@var1 = name,@var2 = sum(age),@total = count(*)from dbo.'+@tableName+' where authority='+@authorityId+'group by name'Exec ( @SQL)select @total - @var2END
#2
November 20th, 2006, 06:28 AM
SQL Consultant
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,352
Time spent in forums: 3 Months 4 Days 14 h 14 m 27 sec
Reputation Power: 3899
Time spent in forums: 3 Months 4 Days 14 h 14 m 27 sec
Reputation Power: 3899
your SELECT will return one row per name, i.e. there will be more than one row in the results
therefore, you cannot stuff the multiple values of name, sum(age), and count(*) into the scalar variables @var1, @var2, @total
(at best, you'd manage to snag only the last ones)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
r937.com | rudy.ca
please visit Simply SQL and buy my book
#3
November 20th, 2006, 06:51 AM
Registered User
Join Date: Nov 2006
Posts: 2
Time spent in forums: 19 m 53 sec
Reputation Power: 0
Time spent in forums: 19 m 53 sec
Reputation Power: 0
This works:
Code:
declare @var1 varchar(50),@var2 int,@total intselect@var1 = something,@var2 = sum(case when something != '12' then 1 else 0 end),@total = count(*)from sometablewhere something = 1111group by somethingselect @total - @var2
#4
November 20th, 2006, 06:57 AM
SQL Consultant
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,352
Time spent in forums: 3 Months 4 Days 14 h 14 m 27 sec
Reputation Power: 3899
Time spent in forums: 3 Months 4 Days 14 h 14 m 27 sec
Reputation Power: 3899
i still don't know why you're getting "Must declare the scalar variable "@var1"
#5
November 22nd, 2006, 11:15 AM
Contributing User
Join Date: Sep 2003
Posts: 508
Time spent in forums: 4 Days 8 h 28 m 55 sec
Reputation Power: 15
Time spent in forums: 4 Days 8 h 28 m 55 sec
Reputation Power: 15
Quote:
Originally Posted by mcal1
Hi all, when i run this query i get the following error "Must declare the scalar variable "@var1". What am i doing wrong?
You're getting that error because of a scope issue. The variables @var1, @var2 etc are declared outside of dynamic SQL that you are executing. Read these articles on dynamic SQL.
http://support.microsoft.com/kb/262499
http://www.sqlteam.com/item.asp?ItemID=4619
Try something like this:
SQL Code:
Original - SQL Code
- DECLARE
- @sql nvarchar(2000),
- @var1 varchar(50),
- @var2 int,
- @total int
- SELECT @sql = '
- select
- @var1 = name,
- @var2 = sum(age),
- @total = count(*)
- from dbo.'+@tablename+'
- where authority='+@authorityid+'
- group by name'
- exec sp_executesql @sql,
- N'@var1 varchar(50) output, @var2 int output, @total int output',
- @var1 output, @var2 output, @total output
- SELECT @total - @var2
- 存储过程变量申明 Must declare the scalar variable "@var1".
- Sql declare申明变量
- 存储过程的变量申明
- SQL中declare申明变量
- SQL中declare申明变量
- SQL中declare申明变量
- SQL中declare申明变量
- SQL中declare申明变量
- MySql存储过程之变量declare set
- [学习笔记][ASP.NET2.0]Must declare the variable '@Rid' exception
- 【转载】SQL中declare申明变量 declare @id int
- mysql 存储过程中 declare set 变量 区别 difference
- mysql创建存储过程declare 变量时报错
- 存储过程,declare
- 存储过程中的declare
- 存储过程中的declare
- Mysql存储过程的declare声明变量必须放在最前面
- mysql存储过程中 declare 和 set 定义变量的区别
- ubuntu更改ls显示目录的颜色
- NeHe教程Qt实现——lesson15
- LinearLayout布局中View添加不同的单击或触摸事件
- 自定义MyLineNumberReader类实现LineNumberReader类工作原理
- CheckPoint
- 存储过程变量申明 Must declare the scalar variable "@var1".
- PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系
- 求两个数的最大公约数的方法
- Automation Object
- Actions
- openCv学习笔记(九)-自己实现分水岭算法并且用openCv测试
- Utility Objects
- 搜索引擎是如何最大化关键字广告收益的
- Library Files库文件