mssql存儲過程表名和字段名為變量的實現(xiàn)方法
更新時間:2011年11月15日 19:02:28 作者:
mssql存儲過程表名和字段名為變量的實現(xiàn)方法,需要的朋友可以參考下。
沒有使用動態(tài)語句直接報錯
錯誤的
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)語句
錯誤的
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里邊
正確的
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
錯誤的
復(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)文章
Sql?Prompt?10下載與安裝破解圖文教程(最新推薦)
sql?prompt?10是一款擁有智能提示功能的SQL?Server和VS插件,,特別適合開發(fā)者不大熟悉腳本時候使用,這篇文章主要介紹了Sql?Prompt?10下載與安裝破解圖文教程,需要的朋友可以參考下2023-03-03SqlServer編寫數(shù)據(jù)庫表的操作方式(建庫、建表、修改語句)
這篇文章主要介紹了SqlServer編寫數(shù)據(jù)庫表的操作方式(建庫、建表、修改語句)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09SQL Server內(nèi)存遭遇操作系統(tǒng)進程壓榨案例分析
最近一臺DB服務(wù)器偶爾出現(xiàn)CPU報警,我的郵件報警閾值設(shè)置的是15%,開始時沒當(dāng)回事,以為是有什么統(tǒng)計類的查詢,后來越來越頻繁2014-03-03批量替換sqlserver數(shù)據(jù)庫掛馬字段并防范sql注入攻擊的代碼
有時候網(wǎng)站sqlserver數(shù)據(jù)庫被掛馬了,網(wǎng)上的很多軟件與方法都是針對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ù)。2009-05-05