1

この声明を実行するのに苦労しています

SET @SelectStatement = 'INSERT INTO [ArchiveProcessHistory] VALUES (' + @TableName + ',' + @TotalRowsSource +',' + @TotalRowsDestination + ',' + GetDate()') '

[上記のステートメントに何か問題があることは知っていますが、修正することはできません)

SET @FullStatement = @SelectStatement 

ArchiveProcessHistory テーブルの構造は次のとおりです。

TableName - nvarch(5)
TotalRowsSource - integer
TotalRowsDestination - integer
DateofInsert - Date

sp_executesql を実行すると、このエラーが発生します

メッセージ 102、レベル 15、状態 1、手順 usp_ArchiveTable、行 39 ')' 付近の構文が正しくありません。

どうすればこれを解決できますか?

4

3 に答える 3

1

潜在的なSQLインジェクション攻撃を回避するには、代わりにsp_executesqlを使用できます。

declare @SelectStatement nvarchar(max)

set @SelectStatement = 
  'INSERT INTO [ArchiveProcessHistory] VALUES
    (@TableName, @TotalRowsSource, @TotalRowsDestination, GetDate())'

exec sp_executesql @SelectStatement,
                   N'@TableName nvarchar(5), @TotalRowsSource int, @TotalRowsDestination int',
                   @TableName = '123', 
                   @TotalRowsSource = 4, 
                   @TotalRowsDestination = 5

また、動的SQLの呪いと祝福もご覧ください。

于 2012-05-14T10:42:44.657 に答える
1
SET @SelectStatement = 'INSERT INTO [ArchiveProcessHistory] VALUES (' + @TableName + ',' + @TotalRowsSource +',' + @TotalRowsDestination + ',' + GetDate() + ') '

+の後に1 つがありませんgetdate()

于 2012-05-14T10:34:32.600 に答える
0

関数である挿入ステートメントの GetDate() の代わりに、current_timestamp を使用します。

SET @SelectStatement = 'INSERT INTO [ArchiveProcessHistory] VALUES (' + @TableName + ','   + @TotalRowsSource +',' + @TotalRowsDestination + ',' + current_timestamp + ') '
于 2012-05-14T10:41:00.787 に答える