欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mssql存儲過程表名和字段名為變量的實現(xiàn)方法

 更新時間:2011年11月15日 19:02:28   作者:  
mssql存儲過程表名和字段名為變量的實現(xiàn)方法,需要的朋友可以參考下。
沒有使用動態(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

消息 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

相關(guān)文章

最新評論