我的动态语句怎么才能运行?
问题行要怎么写才能运行?
我试过下面几种写法,都不行:
if exists (@str1)
if exists (exec @str1)
if exists (exec sp_executesql @str1)
declare @fisyear varchar(4)
declare @str1 varchar(300)
declare @t table (msg nvarchar(100))
set @fisyear='2003'
set @str1 = 'select distinct ts_office from cpm_timesheet where ts_fisyear='''+@fisyear+''' and ts_office not in (select distinct mf_office from cpm_MF where mf_fisyear='''+@fisyear+''')'
select @str1
if exists (@str1) <--问题行
begin
declare @err_mf cursor for @str1
open @err_mf
fetch next from @err_mf into @err_office
while @@fetch_status=0
begin
insert into @t values (@err_office+N'办公室 '+@fisyear+N'财政年度 没有办公室倍率')
fetch next from @err_mf into @err_office
end
end
select * from @t