动态sql提示错误。请指教。
create procedure [dbo].[清理搅拌楼数据]
as
declare @date varchar(10)
declare @tablename1 varchar(30)
declare @tablename2 varchar(30)
declare @sql varchar(1000)
set @date=convert(varchar(10),dateadd(day,-50,getdate()),120)
set @tablename1='ProdMasterData'+convert(varchar(10),dateadd(day,-50,getdate()),120)
set @tablename2='ConsumpMaterial'+convert(varchar(10),dateadd(day,-50,getdate()),120)
select @sql=' select * into data_bak.dbo.'+@tablename1+' from data.dbo.ProdMasterData
where pdate<=@date '
print @sql
execute (@sql)
select * into data_bak.dbo.ProdMasterData2013-08-09 from data.dbo.ProdMasterData
where pdate<=@date
消息 170,级别 15,状态 1,第 1 行
第 1 行: '-' 附近有语法错误。
------解决方案--------------------create procedure [dbo].[清理搅拌楼数据]
as
declare @date varchar(10)
declare @tablename1 varchar(30)
declare @tablename2 varchar(30)
declare @sql varchar(1000)
set @date=convert(varchar(10),dateadd(day,-50,getdate()),120)
set @tablename1='ProdMasterData'+convert(varchar(10),dateadd(day,-50,getdate()),120)
set @tablename2='ConsumpMaterial'+convert(varchar(10),dateadd(day,-50,getdate()),120)
select @sql=' select * into data_bak.dbo.['+@tablename1+'] from data.dbo.ProdMasterData
where pdate<='''+@date+''''
print @sql
execute (@sql)
select * into data_bak.dbo.[ProdMasterData2013-08-09] from data.dbo.ProdMasterData
where pdate<='2013-08-09'