我有一個動態查詢其內容是這樣SQL SERVER 2008動態查詢問題
Alter PROCEDURE dbo.mySP
-- Add the parameters for the stored procedure here
(
@DBName varchar(50),
@tblName varchar(50)
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
declare @string as varchar(50)
declare @string1 as varchar(50)
set @string1 = '[' + @DBName + ']' + '.[dbo].' + '[' + @tblName + ']'
set @string = 'select * from ' + @string1
exec @string
END
我打電話這樣
dbo.mySP 'dbtest1','tblTest'
而且我遇到一個錯誤
"Msg 203, Level 16, State 2, Procedure mySP, Line 27
The name 'select * from [dbtest1].[dbo].[tblTest]' is not a valid identifier."
什麼是wr翁?和如何克服?提前
感謝
我真希望你正在檢查SQL注入的地方... – GilaMonster 2009-09-21 12:39:51
我不願意認爲任何人都會考慮寫這樣的sp。請閱讀: http://www.sommarskog.se/dynamic_sql.html – HLGEM 2009-09-21 14:37:57