国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

mssql存儲過程表名和字段名為變量的實現方法

2024-08-31 00:45:25
字體:
來源:轉載
供稿:網友
沒有使用動態語句直接報錯

錯誤的

復制代碼 代碼如下:


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,狀態 1,過程 testpapers,第 1 行
關鍵字 'select' 附近有語法錯誤。
消息 1087,級別 15,狀態 2,過程 testpapers,第 1 行
必須聲明表變量 "@tems"。

首先要讓表名或者字段為變量則要用到動態語句

錯誤的

復制代碼 代碼如下:


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

exec testpapers

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潞西市| 景德镇市| 讷河市| 长治县| 淮南市| 万荣县| 东辽县| 金阳县| 海门市| 灵璧县| 大渡口区| 宁晋县| 长兴县| 信阳市| 枣阳市| 电白县| 海阳市| 庆安县| 平谷区| 天峻县| 洞口县| 荆门市| 蒙自县| 石狮市| 大新县| 屏东市| 巨野县| 天水市| 汤阴县| 登封市| 龙口市| 福清市| 松滋市| 红安县| 武城县| 泗洪县| 榆社县| 江油市| 德惠市| 越西县| 旬阳县|