存储过程变量申明 Must declare the scalar variable "@var1".
2012-07-29 22:52
489 查看
http://forums.devshed.com/ms-sql-development-95/stored-procedure-must-declare-the-scalar-variable-var1t-403571.html
#2 November 20th, 2006, 06:28 AM | ||||||
| ||||||
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 |
#3 November 20th, 2006, 06:51 AM | |||||
| |||||
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 |
#4 November 20th, 2006, 06:57 AM | ||||||
| ||||||
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" |
#5 November 22nd, 2006, 11:15 AM | |||||
| |||||
Quote:
http://support.microsoft.com/kb/262499 http://www.sqlteam.com/item.asp?ItemID=4619 Try something like this: 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 "@Author".
- [学习笔记][ASP.NET2.0]Must declare the variable '@Rid' exception
- 错误:is quoted with " which must be escaped when used within the value
- The content of element type "session-factory" must match "(property*,mapping*,(class-cache|collectio
- android工程出现"The method * of type * must override a superclass method"的解决方法
- “The content of element type "package" must match "(result-types?,interceptors?,
- web.xml The content of element type "web-app" must match "
- The content of element type "hibernate-configuration" must match "(session-factory,security?)
- struts.xml错误提示:The content of element type "package" must match "(result-types?,interceptors?,defaul
- The reference to entity "autoReconnect" must end with the ';' delimiter.
- IAR编译器一个警告信息-Warning[Pe550]: variable "变量" was set but never used
- Attribute value ("XXX") is quoted with " which must be escaped when used within the value 异常的修正1
- The reference to entity "characterEncoding" must end with the ';' delimiter
- The content of element type "package" must match "(result-types?,interceptors?,default-intercept .
- 关于异常“The specified child already has a parent. You must call removeView"的解决(举例说明,附源码)
- The reference to entity "characterEncoding" must end with the ';' delimiter
- The reference to entity "" must end with the ';' delimiter
- 解决itunesconnect上传时"at least one of the following architecture(s) must be present:armv6"问题
- The content of element type "struts-config" must match "(data-sources?,form-
- The document type declaration for root element type "config" must end with '>'.错误