我想通過構建動態查詢並存儲在變量中並執行變量來編寫簡單的SP。動態SQL查詢給出類型錯誤
我目前得到以下錯誤:
Msg 206, Level 16, State 2, Line 16
Operand type clash: datetime2 is incompatible with float
對於下面的代碼:
DECLARE
@table_Num
@1 varchar(100) = 'boo',
@2 int =2,
@3 varchar(100) ='default',
@4 varchar(50) = NULL,
@5 int =NULL,
@6 float =12,
@7 datetime2(0) ='1970-01-01 00:00:00',
@8 datetime2(0)='1970-01-01 00:00:00',
@9 varchar(50)='',
@10 varchar(50)=NULL,
@11 decimal(18,0)=0000000000000,
@12 int =999999
DECLARE @SQLString NVARCHAR(MAX)
SET @SQLString = 'INSERT INTO abc_'[email protected]_Num+'(col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12)
VALUES ('[email protected]+',2,'[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+','[email protected]+')'
EXEC (@SQLString)
至於我能看到的變量是同一類型的表山坳類型。有任何想法嗎?
您是否還可以包含'table_1'的定義? –
這是構建動態SQL字符串的絕對最糟糕的方法。你能解釋爲什麼它首先需要成爲動態SQL嗎? –
@AaronBertrand偉大的一點。我認爲這是理所當然的,因爲它是動態SQL。 –