我希望每次通过存储过程创建一个新表。
declare @datetime datetime
declare @date varchar(20)
select @datetime = (GETDATE() - 1)
select @date = convert(varchar(10), @datetime, 112)
print @datetime
print @date
create table #businessmster+'_'+@date
(
contentid int
)
我想要的表名是#businessmaster_20171103
答案 0 :(得分:4)
您可以使用此查询。
Declare @strTableName varchar(500)
SET @strTableName = 'create table businessmster_' + @date +'( contentid int )
GO
INSERT INTO businessmster_' + @date +' Select Field FROM <<YourTable>> WHERE <<Condition>>
GO'
Print @strTableName
Exec(@strTableName)
答案 1 :(得分:0)
您可以将动态查询用作:
DECLARE @TableName NVARCHAR(MAX);
SET @TableName = N'CREATE TABLE businessmaster_'+
CONVERT(VARCHAR(10),GetDate()-1,112)+
N' ( contentid int )';
SELECT @TableName
EXEC(@TableName);
根据your comment,我删除了#
符号。