这个存储过程为什么说必须声明变量??帮忙解决一下
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'。
存储过程算法不太好,希望能提供好点的算法。谢谢