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

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

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

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

錯誤的

復制代碼 代碼如下:


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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲阜市| 简阳市| 扎兰屯市| 乌鲁木齐县| 普陀区| 延长县| 金华市| 西平县| 沅江市| 纳雍县| 视频| 肃南| 靖边县| 田东县| 江北区| 新巴尔虎右旗| 凤山县| 江源县| 望城县| 盐城市| 达尔| 丹阳市| 易门县| 霍邱县| 长顺县| 保定市| 宝山区| 汾西县| 东丰县| 宾川县| 如东县| 交口县| 六盘水市| 北海市| 南京市| 巩留县| 平泉县| 汽车| 镇沅| 扬中市| 安新县|