使用变量作为 table 名称失败
Using a variable as table name fails
为什么会失败并显示以下错误消息:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'reporting_rawdata_v2'.
table 的名称是 "dbo.reporting_rawdata_v2" 但是 with/without "dbo" 它仍然失败...
Use reporting2
Go
Declare @Backupdate varchar(25), @sql NVARCHAR(max)
Set @Backupdate = REPLACE(REPLACE(CAST(CONVERT(VARCHAR(20), SYSDATETIME(), 100) as varchar),' ','_'),':', '')
Select @Backupdate
SET @sql = 'Select * Into reporting_rawdata_BACKUP_' + @Backupdate + 'From reporting_rawdata_v2';
EXEC (@sql);
在动态命名的 table 和 From
之间没有 space
SET @sql = 'Select * Into reporting_rawdata_BACKUP_' + @Backupdate + ' From reporting_rawdata_v2';
EXEC (@sql);
为什么会失败并显示以下错误消息:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'reporting_rawdata_v2'.
table 的名称是 "dbo.reporting_rawdata_v2" 但是 with/without "dbo" 它仍然失败...
Use reporting2
Go
Declare @Backupdate varchar(25), @sql NVARCHAR(max)
Set @Backupdate = REPLACE(REPLACE(CAST(CONVERT(VARCHAR(20), SYSDATETIME(), 100) as varchar),' ','_'),':', '')
Select @Backupdate
SET @sql = 'Select * Into reporting_rawdata_BACKUP_' + @Backupdate + 'From reporting_rawdata_v2';
EXEC (@sql);
在动态命名的 table 和 From
SET @sql = 'Select * Into reporting_rawdata_BACKUP_' + @Backupdate + ' From reporting_rawdata_v2';
EXEC (@sql);