这个存储过程为什么说必须声明变量??帮忙解决一下

lxxstarii 2005-02-28 05:50:40
CREATE PROCEDURE sp_swgl
@uYGBH INT
AS
SET NOCOUNT ON
declare @sql nvarchar(500)
declare @ygbh int
declare @gwxh int
declare @blzt nvarchar(4)
declare @maxspsxh int
declare sw_cursor cursor for
Select gwxh,blzt from gw_zb where (blzt='SPTG' or blzt='SPWG') order by nzrq desc



BEGIN
Create table #temptable (
gwxh int,
blzt nvarchar(4),
spsxh int,
nzr int
)

Open sw_cursor
FETCH NEXT FROM sw_cursor into @gwxh,@blzt

While @@FETCH_Status = 0
BEGIN

If @blzt = 'SPWG'
BEGIN
Select @maxspsxh = MAX(spsxh) from gw_spb where gwxh = @gwxh
If @maxspsxh >=3
BEGIN
Set @ygbh = @uYGBH
Set @Sql=N'insert into #temptable Select gw_zb.gwxh , gw_zb.blzt ,gw_spb.spsxh , gw_spb.spr from gw_spb,gw_zb where gw_spb.spsxh =2 and gw_spb.spr =@ygbh and gw_zb.blzt = "SPWG" and gw_spb.gwxh = @gwxh and gw_spb.gwxh = gw_zb.gwxh' --估计在这行报错
exec sp_executesql @Sql
END
Else
BEGIN
Select * from gw_zb where nzr = @uYGBH and blzt = 'SPWG' and gwxh =@gwxh
END
END

ELSE if @blzt = 'SPTG'
BEGIN
Select * from gw_zb where nzr = @uYGBH and blzt = 'SPTG' and gwxh = @gwxh
END


FETCH next from sw_cursor into @gwxh,@blzt
END
CLOSE sw_cursor
Deallocate sw_cursor
END
GO


报错消息:
服务器: 消息 137,级别 15,状态 2,行 1
必须声明变量 '@ygbh'。

存储过程算法不太好,希望能提供好点的算法。谢谢
...全文
157 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiang130 2005-02-28
  • 打赏
  • 举报
回复
Set @Sql='insert into #temptable Select gw_zb.gwxh , gw_zb.blzt ,gw_spb.spsxh , gw_spb.spr from gw_spb,gw_zb where gw_spb.spsxh =2 and gw_spb.spr ='''+@ygbh +''' and gw_zb.blzt = "SPWG" and gw_spb.gwxh ='''+ @gwxh +''' and gw_spb.gwxh = gw_zb.gwxh'
xluzhong 2005-02-28
  • 打赏
  • 举报
回复
--try
Set @ygbh = @uYGBH
Set @Sql=N'insert into #temptable Select gw_zb.gwxh , gw_zb.blzt ,gw_spb.spsxh , gw_spb.spr from gw_spb,gw_zb where gw_spb.spsxh =2 and gw_spb.spr ='''+@ygbh+''' and gw_zb.blzt = "SPWG"
and gw_spb.gwxh = '''+@gwxh+''' and --这样修改
gw_spb.gwxh = gw_zb.gwxh' --估计在这行报错
exec sp_executesql @Sql
xluzhong 2005-02-28
  • 打赏
  • 举报
回复
--try
Set @ygbh = @uYGBH
Set @Sql=N'insert into #temptable Select gw_zb.gwxh , gw_zb.blzt ,gw_spb.spsxh , gw_spb.spr from gw_spb,gw_zb where gw_spb.spsxh =2 and gw_spb.spr =@ygbh and gw_zb.blzt = "SPWG" and gw_spb.gwxh = '''+@gwxh+''' and --这样修改gw_spb.gwxh = gw_zb.gwxh' --估计在这行报错
exec sp_executesql @Sql
jinjazz 2005-02-28
  • 打赏
  • 举报
回复
Set @Sql=N'insert into #temptable Select gw_zb.gwxh , gw_zb.blzt ,gw_spb.spsxh , gw_spb.spr from gw_spb,gw_zb where gw_spb.spsxh =2 and gw_spb.spr ='''+@ygbh+''' and gw_zb.blzt = "SPWG" and gw_spb.gwxh = '''+@gwxh+''' and gw_spb.gwxh = gw_zb.gwxh'
yjzhg 2005-02-28
  • 打赏
  • 举报
回复
exec sp_executesql @Sql,N'@ygbh int',@ygbh=@uYGBH

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧