mssql存儲(chǔ)過程表名和字段名為變量的實(shí)現(xiàn)方法
更新時(shí)間:2011年11月15日 19:02:28 作者:
mssql存儲(chǔ)過程表名和字段名為變量的實(shí)現(xiàn)方法,需要的朋友可以參考下。
沒有使用動(dòng)態(tài)語句直接報(bào)錯(cuò)
錯(cuò)誤的
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,級(jí)別 15,狀態(tài) 1,過程 testpapers,第 1 行
關(guān)鍵字 'select' 附近有語法錯(cuò)誤。
消息 1087,級(jí)別 15,狀態(tài) 2,過程 testpapers,第 1 行
必須聲明表變量 "@tems"。
首先要讓表名或者字段為變量則要用到動(dòng)態(tài)語句
錯(cuò)誤的
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,級(jí)別 15,狀態(tài) 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
錯(cuò)誤的
復(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,級(jí)別 15,狀態(tài) 1,過程 testpapers,第 1 行
關(guān)鍵字 'select' 附近有語法錯(cuò)誤。
消息 1087,級(jí)別 15,狀態(tài) 2,過程 testpapers,第 1 行
必須聲明表變量 "@tems"。
首先要讓表名或者字段為變量則要用到動(dòng)態(tài)語句
錯(cuò)誤的
復(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,級(jí)別 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)文章
Sql?Prompt?10下載與安裝破解圖文教程(最新推薦)
sql?prompt?10是一款擁有智能提示功能的SQL?Server和VS插件,,特別適合開發(fā)者不大熟悉腳本時(shí)候使用,這篇文章主要介紹了Sql?Prompt?10下載與安裝破解圖文教程,需要的朋友可以參考下2023-03-03SqlServer編寫數(shù)據(jù)庫表的操作方式(建庫、建表、修改語句)
這篇文章主要介紹了SqlServer編寫數(shù)據(jù)庫表的操作方式(建庫、建表、修改語句)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09SQL Server連接中三個(gè)常見的錯(cuò)誤分析
SQL Server連接中三個(gè)常見的錯(cuò)誤分析...2007-03-03SQL Server內(nèi)存遭遇操作系統(tǒng)進(jìn)程壓榨案例分析
最近一臺(tái)DB服務(wù)器偶爾出現(xiàn)CPU報(bào)警,我的郵件報(bào)警閾值設(shè)置的是15%,開始時(shí)沒當(dāng)回事,以為是有什么統(tǒng)計(jì)類的查詢,后來越來越頻繁2014-03-03批量替換sqlserver數(shù)據(jù)庫掛馬字段并防范sql注入攻擊的代碼
有時(shí)候網(wǎng)站sqlserver數(shù)據(jù)庫被掛馬了,網(wǎng)上的很多軟件與方法都是針對(duì)text小于8000的,這里的方法貌似可行,需要的朋友可以參考下。2010-04-04Sql Server使用cursor處理重復(fù)數(shù)據(jù)過程詳解
本節(jié)主要介紹了Sql Server cursor的使用,以處理重復(fù)數(shù)據(jù)為例,需要的朋友可以參考下2014-08-08sql server中的decimal或者numeric的精度問題
在sql server中定義列的數(shù)據(jù)類型decimal時(shí)需要制定其精度和小數(shù)位數(shù)。2009-05-05