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
SET @SelectStatement = 'INSERT INTO [ArchiveProcessHistory] VALUES (' + @TableName + ',' + @TotalRowsSource +',' + @TotalRowsDestination + ',' + GetDate() + ') '

+后缺一个getdate()

于 2012-05-14T10:34:32.600 回答
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 回答
0

在作为函数的插入语句中,不要使用 GetDate(),而是使用 current_timestamp。

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