存储过程变量申明 Must declare the scalar variable "@var1".

http://forums.devshed.com/ms-sql-development-95/stored-procedure-must-declare-the-scalar-variable-var1t-403571.html
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 ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[admin3c]
	-- Add the parameters for the stored procedure here
	@tableName varchar(100),
	@authorityId varchar(50)

AS
BEGIN
	SET NOCOUNT ON;

Declare @SQL VarChar(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 ( @SQL)

select @total - @var2

END

Reply With Quote
  # 2  
Old November 20th, 2006, 06:28 AM
r937's Avatar
SQL Consultant
Dev Shed God 41st Plane (25000 - 25499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,352  r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level)  
Time spent in forums: 3 Months 4 Days 14 h 14 m 27 sec 
Reputation Power: 3899
i'm not sure why you're getting that particular error message, however, there's still a problem in how you're approaching this --

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 

Reply With Quote
  # 3  
Old November 20th, 2006, 06:51 AM
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 2  mcal1 User rank is Just a Lowly Private (1 - 20 Reputation Level)  
Time spent in forums: 19 m 53 sec 
Reputation Power: 0
Actually this query will only return one row. I modified the statement at little before i posted it here and probaly messed it up but its guareented to return only one row. Also the query works when i dont run it as an stored procedure. 
This works: 

Code:
declare @var1 varchar(50),
@var2 int,
@total int

select	@var1 = something,
		@var2 = sum(case when something != '12' then 1 else 0 end),
		@total = count(*)
from sometable
where something = 1111
group by something

select @total - @var2

Reply With Quote
  # 4  
Old November 20th, 2006, 06:57 AM
r937's Avatar
SQL Consultant
Dev Shed God 41st Plane (25000 - 25499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 25,352  r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level) r937 User rank is General 43rd Grade (Above 100000 Reputation Level)  
Time spent in forums: 3 Months 4 Days 14 h 14 m 27 sec 
Reputation Power: 3899
if you're gonna write the WHERE clause with only one "something" then you shouldn't even have it in the query -- remove it from the SELECT, and remove the GROUP BY

i still don't know why you're getting "Must declare the scalar variable "@var1"

Reply With Quote
  # 5  
Old November 22nd, 2006, 11:15 AM
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2003
Posts: 508  BillyDunny User rank is Sergeant (500 - 2000 Reputation Level) BillyDunny User rank is Sergeant (500 - 2000 Reputation Level) BillyDunny User rank is Sergeant (500 - 2000 Reputation Level) BillyDunny User rank is Sergeant (500 - 2000 Reputation Level) BillyDunny User rank is Sergeant (500 - 2000 Reputation Level)  
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
          
          
  1. DECLARE
  2.     @sql nvarchar(2000),
  3.     @var1 varchar(50),
  4.     @var2 int,
  5.     @total int
  6.  
  7. SELECT @sql = '
  8. select
  9.     @var1 = name,
  10.     @var2 = sum(age),
  11.     @total = count(*)
  12. from dbo.'+@tablename+'
  13. where authority='+@authorityid+'
  14. group by name'
  15.  
  16.  
  17. exec sp_executesql @sql,
  18.     N'@var1 varchar(50) output, @var2 int output, @total int output',
  19.     @var1 output, @var2 output, @total output
  20.  
  21.  
  22. SELECT @total - @var2


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值