Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update Query using Dynamic SQL

Author  Topic 

nirene
Yak Posting Veteran

98 Posts

Posted - 2014-08-05 : 02:34:02
Dear All,

Help me by giving a Dynamic query statement for the below mentioned tables

Target
Cocode-Brcode-Loccode-Month-Year-Target
C1 -B1 -L1 -4 -2014-100
C1 -B1 -L1 -5 -2014-120
C1 -B1 -L1 -6 -2014-140

TargetFormat
Cocode-Brcode-Loccode-Apr_T-May_T-Jun_T
C1 -B1 -L1 -0 -0 -0

Now I'm updating this TargetFormat

Update TargetFormat Set Apr_T=T.Target from TargetFormat TF,Target T
Where TF.Cocode=T.Cocode and TF.Brcode=T.Brcode and TF.Loccode=T.Loccode and T.Month=4 and T.Year=2014

And so on

After Update

TargetFormat
Cocode-Brcode-Loccode-Apr_T-May_T-Jun_T
C1 -B1 -L1 -100 -120 -140

Select Left(DateName(month,DateAdd(month,Month,0)-1),3)+'_T' from Target

Taking the above as example, can a update be done in single stroke using dynamic sql.

Thanks in advance.

Nirene







gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-08-05 : 08:35:50
Here's one way without dynamic sql:




declare @target table (Cocode char(2), Brcode char(2), Loccode char(2), [Month] int, [Year] int, target int);
declare @TargetFormat table (Cocode char(2), Brcode char(2), Loccode char(2), Apr_T int, May_T int, Jun_T int);

insert into @target values
('C1', 'B1', 'L1', 4, 2014, 100),
('C1', 'B1', 'L1', 5, 2014, 120),
('C1', 'B1', 'L1', 6, 2014, 140);

insert into @TargetFormat values
('C1', 'B1', 'L1', 0, 0 ,0);

with
targetupvt as (
select Cocode, Brcode, Loccode,
case month
when 'Apr_T' then 4
when 'May_T' then 5
when 'Jun_T' then 6
end as month,
target

from @TargetFormat
unpivot (target for month in (Apr_T, May_T, Jun_T)) u
),
targetbymonth as (
select ca.*
from @target T
cross apply (
select tf.month, t.target
from targetupvt tf
where TF.Cocode=T.Cocode and TF.Brcode=T.Brcode and TF.Loccode=T.Loccode and tf.month = t.Month and T.Year=2014
) ca
)

update @TargetFormat
set Apr_T = p.[4],
May_T = p.[5],
Jun_T = p.[6]

from targetbymonth
pivot(max(target) for month in ([4],[5],[6])) p

select * from @targetFormat
Go to Top of Page

nirene
Yak Posting Veteran

98 Posts

Posted - 2014-08-08 : 01:19:40
Hai gbritton,

Thanks for your post and sorry for the delayed reply.

Forgot to mention in my previous post that achievement also to be added to target.

Sample Data

declare @target table (Cocode char(2), Brcode char(2), Loccode char(2), [Month] int, [Year] int, target int,achievement int);

declare @TargetFormat table (Cocode char(2), Brcode char(2), Loccode char(2), Apr_T int,Apr_A int, May_T int,May_A int, Jun_T int,Jun_A int);

insert into @target values
('C1', 'B1', 'L1', 4, 2014, 100,80)
insert into @target values
('C1', 'B1', 'L1', 5, 2014, 120,110)
insert into @target values
('C1', 'B1', 'L1', 6, 2014, 140,120)

insert into @TargetFormat values
('C1', 'B1', 'L1', 0,0,0,0,0,0);

Expected Result
Cocode,Brcode,Loccode,Apr_T,Apr_A,May_T,May_A,Jun_T,Jun_A
C1 ,B1 ,L1 ,100 ,80 ,120 ,110 ,140 ,120

Regards
Nirene
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-08-08 : 10:24:25
Try to work with my solution and modify it to do what you want
Go to Top of Page
   

- Advertisement -