sql - INSERT INTO View Dynamically -
i've view created referring 2 synonyms. current query i've harvest view table below
select dbo.synonym1.jobno , dbo.synonym1.customer , dbo.synonym2.postalservice , dbo.synonym2.firstdate , dbo.synonym2.seconddate dbo.synonym1 inner join dbo.synonym2 on dbo.synonym1.jobno = sbo.synonym2.jobno dbo.synonym2.postalservice = 'ups' now find difference between dbo.synonym2.firstdate , dbo.synonym2.seconddate in each row , insert same row again view table many times there difference.
for example if firstdate 2012-06-03 00:00:00.000 , seconddate 2012-06-05 00:00:00.000 should insert same row twice.
i'm using sql server 2008 r2 , i'm newbie sql. please me out
sample data gets populated above query in view below,
jobno customer postalservice firstdate seconddate 1 abc ups 2012-06-03 00:00:00.000 2012-06-03 00:00:00.000 2 def ups 2012-06-03 00:00:00.000 2012-06-06 00:00:00.000 3 xyz ups 2012-06-03 00:00:00.000 2012-06-05 00:00:00.000 i need result below
jobno customer postalservice firstdate seconddate 1 abc ups 2012-06-03 00:00:00.000 2012-06-03 00:00:00.000 2 def ups 2012-06-03 00:00:00.000 2012-06-05 00:00:00.000 2 def ups 2012-06-03 00:00:00.000 2012-06-05 00:00:00.000 2 xyz ups 2012-06-03 00:00:00.000 2012-06-05 00:00:00.000 3 xyz ups 2012-06-03 00:00:00.000 2012-06-06 00:00:00.000 3 xyz ups 2012-06-03 00:00:00.000 2012-06-06 00:00:00.000 if see difference between dates jobno 2 3 data repeated 3 times. , jobno 3 2 data repeated twice.
your request little confusing, because 1 doesn't insert views.
however, following insert rows table
insert viewtable(jobno, customer, postalservice, firstdate, seconddate) select dbo.synonym1.jobno, dbo.synonym1.customer, dbo.synonym2.postalservice, dbo.synonym2.firstdate, dbo.synonym2.seconddate dbo.synonym1 inner join dbo.synonym2 on dbo.synonym1.jobno = sbo.synonym2.jobno dbo.synonym2.postalservice = 'ups' , datediff(d, firstdate, seconddate) <> 0 you need define viewtable actual table though.
Comments
Post a Comment