沒有使用動態(tài)語句直接報(bào)錯
錯誤的
復(fù)制代碼 代碼如下:
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
消息 156,級別 15,狀態(tài) 1,過程 testpapers,第 1 行
關(guān)鍵字 'select' 附近有語法錯誤。
消息 1087,級別 15,狀態(tài) 2,過程 testpapers,第 1 行
必須聲明表變量 "@tems"。
首先要讓表名或者字段為變量則要用到動態(tài)語句
錯誤的
復(fù)制代碼 代碼如下:
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,狀態(tài) 2,第 1 行
必須聲明表變量 "@tems"。
將表名和字段名寫到exec里邊
正確的
復(fù)制代碼 代碼如下:
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
exec testpapers
您可能感興趣的文章:- SqlServer快速檢索某個字段在哪些存儲過程中(sql 語句)