declare @mnth varchar(7),@date as datetime,@vardate as varchar(10), @dateper_01 varchar(7),@period_01 varchar(7),@period_02 varchar(7),@period_03 varchar(7),@period_04 varchar(7)
declare @period_05 varchar(7),@period_06 varchar(7),@period_07 varchar(7),@period_08 varchar(7),@period_09 varchar(7),@period_10 varchar(7),@period_11 varchar(7),@period_12 varchar(7)
declare @Query varchar(4000)
set @mnth='2014-04'
set @vardate=@mnth+'-01'
--select @vardate
set @date=cast(@vardate as datetime)
set @period_01=right(convert(varchar,DATEADD(MM,-0,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-0,@date),101),2)
set @period_02=right(convert(varchar,DATEADD(MM,-1,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-1,@date),101),2)
set @period_03=right(convert(varchar,DATEADD(MM,-2,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-2,@date),101),2)
set @period_04=right(convert(varchar,DATEADD(MM,-3,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-3,@date),101),2)
set @period_05=right(convert(varchar,DATEADD(MM,-4,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-4,@date),101),2)
set @period_06=right(convert(varchar,DATEADD(MM,-5,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-5,@date),101),2)
set @period_07=right(convert(varchar,DATEADD(MM,-6,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-6,@date),101),2)
set @period_08=right(convert(varchar,DATEADD(MM,-7,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-7,@date),101),2)
set @period_09=right(convert(varchar,DATEADD(MM,-8,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-8,@date),101),2)
set @period_10=right(convert(varchar,DATEADD(MM,-9,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-9,@date),101),2)
set @period_11=right(convert(varchar,DATEADD(MM,-10,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-10,@date),101),2)
set @period_12=right(convert(varchar,DATEADD(MM,-11,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-11,@date),101),2)
drop table ##temp
SET @Query =
'CREATE TABLE [dbo].[##temp](
[bu] [nvarchar](12) NOT NULL,
[objsub] [nvarchar](20) NOT NULL,
[obj_desc] [nchar](130) NULL,'
+'['+@Period_12+'] [float] NULL,'
+'['+@Period_11+'] [float] NULL,'
+'['+@Period_10+'] [float] NULL,'
+'['+@Period_09+'] [float] NULL,'
+'['+@Period_08+'] [float] NULL,'
+'['+@Period_07+'] [float] NULL,'
+'['+@Period_06+'] [float] NULL,'
+'['+@Period_05+'] [float] NULL,'
+'['+@Period_04+'] [float] NULL,'
+'['+@Period_03+'] [float] NULL,'
+'['+@Period_02+'] [float] NULL,'
+'['+@Period_01+'] [float] NULL,
) '
EXECUTE (@query)
-- drop table ##temp
select * from ##temp
declare @period_05 varchar(7),@period_06 varchar(7),@period_07 varchar(7),@period_08 varchar(7),@period_09 varchar(7),@period_10 varchar(7),@period_11 varchar(7),@period_12 varchar(7)
declare @Query varchar(4000)
set @mnth='2014-04'
set @vardate=@mnth+'-01'
--select @vardate
set @date=cast(@vardate as datetime)
set @period_01=right(convert(varchar,DATEADD(MM,-0,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-0,@date),101),2)
set @period_02=right(convert(varchar,DATEADD(MM,-1,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-1,@date),101),2)
set @period_03=right(convert(varchar,DATEADD(MM,-2,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-2,@date),101),2)
set @period_04=right(convert(varchar,DATEADD(MM,-3,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-3,@date),101),2)
set @period_05=right(convert(varchar,DATEADD(MM,-4,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-4,@date),101),2)
set @period_06=right(convert(varchar,DATEADD(MM,-5,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-5,@date),101),2)
set @period_07=right(convert(varchar,DATEADD(MM,-6,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-6,@date),101),2)
set @period_08=right(convert(varchar,DATEADD(MM,-7,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-7,@date),101),2)
set @period_09=right(convert(varchar,DATEADD(MM,-8,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-8,@date),101),2)
set @period_10=right(convert(varchar,DATEADD(MM,-9,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-9,@date),101),2)
set @period_11=right(convert(varchar,DATEADD(MM,-10,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-10,@date),101),2)
set @period_12=right(convert(varchar,DATEADD(MM,-11,@date),101),4)+'-'+left(convert(varchar,DATEADD(MM,-11,@date),101),2)
drop table ##temp
SET @Query =
'CREATE TABLE [dbo].[##temp](
[bu] [nvarchar](12) NOT NULL,
[objsub] [nvarchar](20) NOT NULL,
[obj_desc] [nchar](130) NULL,'
+'['+@Period_12+'] [float] NULL,'
+'['+@Period_11+'] [float] NULL,'
+'['+@Period_10+'] [float] NULL,'
+'['+@Period_09+'] [float] NULL,'
+'['+@Period_08+'] [float] NULL,'
+'['+@Period_07+'] [float] NULL,'
+'['+@Period_06+'] [float] NULL,'
+'['+@Period_05+'] [float] NULL,'
+'['+@Period_04+'] [float] NULL,'
+'['+@Period_03+'] [float] NULL,'
+'['+@Period_02+'] [float] NULL,'
+'['+@Period_01+'] [float] NULL,
) '
EXECUTE (@query)
-- drop table ##temp
select * from ##temp
No comments:
Post a Comment