alter proc testpapers as begin declare @tems nvarchar(max),@zidaun nvarchar(max) set @tems=select * from @tems order by @zidaun exec(@tems) end exec testpapers
alter proc testpapers as begin declare @tems nvarchar(max),@zidaun nvarchar(max) set @tems='select * from @tems order by @zidaun '; exec(@tems) end
exec testpapers
消息 1087,級別 15,狀態 2,第 1 行 必須聲明表變量 "@tems"。
將表名和字段名寫到exec里邊
正確的
復制代碼 代碼如下:
alter proc testpapers as begin declare @startRow nvarchar(max),@tems nvarchar(max),@zidaun nvarchar(max) set @startRow='temp' set @tems='select * from '; set @zidaun='p_id'; exec(@tems+@startRow+' order by '+@zidaun) end