Note that there are some explanatory texts on larger screens.

plurals
  1. POINSERT INTO View Dynamically
    primarykey
    data
    text
    <p>I've a view created by referring to two synonyms. The current query I've to harvest my View Table is as below</p> <pre><code>SELECT dbo.synonym1.JobNo , dbo.synonym1.Customer , dbo.synonym2.PostalService , dbo.synonym2.FirstDate , dbo.synonym2.SecondDate FROM dbo.synonym1 INNER JOIN dbo.synonym2 ON dbo.synonym1.JobNo = sbo.synonym2.JobNo WHERE dbo.synonym2.PostalService = 'UPS' </code></pre> <p>Now I would like to find the difference between <code>dbo.synonym2.FirstDate</code> and <code>dbo.synonym2.SecondDate</code> in each row and <code>INSERT</code> the same row again into the View Table as many times as there is a difference.</p> <p>For example if the <code>FirstDate</code> is <code>2012-06-03 00:00:00.000</code> and <code>SecondDate</code> is <code>2012-06-05 00:00:00.000</code> then it should insert the same row twice.</p> <p>I'm using SQL Server 2008 R2 and I'm a newbie to SQL. Please help me out</p> <p>Sample data that gets populated currently with the above query in the view is as below,</p> <pre><code>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 </code></pre> <p>I would need a result as below</p> <pre><code>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 </code></pre> <p>If you see the difference between the dates for JobNo 2 is 3 so data is repeated three times. and for the JobNo 3 is 2 so data is repeated twice.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload