sp_executesql的运用
书写语法要点:
exec sp_executesql @sql,N’参数1 类型1,参数2 类型2,参数3 类型3 OUTPUT’,参数1,参数2,参数3 OUTPUT;
注意参数前后顺序必须对应好 如下图不同颜色的标记
declare @ID int declare @Oid int declare @Login varchar(50) --定义一个游标 declare zb_cur cursor for select zb_gl.zb_sql_gs from zb_gl declare @zb_sql varchar(2000) declare @ymd varchar(20) set @ymd='2019-1-1' --打开游标 open zb_cur while 0=0 begin --读取游标 fetch next from zb_cur into @zb_sql if @@FETCH_STATUS=0 begin set @zb_sql=N'select zj_cydlv from scsj_account_daily_jz where ymd=@ymd and jz=1' print @zb_sql exec sp_executesql N'select zj_cydlv from scsj_account_daily_jz where ymd=@ymd and jz=1',N'@ymd varchar(20)',@ymd end else break end close zb_cur --摧毁游标 deallocate zb_cur