Solved

SQL Query Syntax question

Posted on 2014-12-03
3
156 Views
Last Modified: 2014-12-04
Hey all,

What is a cleaner way to do this query.  I am inserting 2 records here into this temp table but I am not wanting the union if possible as I am just increasing the date by + 1.

  INSERT INTO #TempCDCInsertData
               (
                     PortfolioId,
                     MVDate,
                     Amount,
                     AmountUSD,
                     ReconcileFlag,
                     ISOCurrencyCode,
                     start_lsn,
                     tran_end_time,
                     operation,
                     CDCInstanceID
               )
               SELECT
                     PortfolioId,
                     MVDate + 1,
                     Amount,
                     AmountUSD,
                     ReconcileFlag,
                     ISOCurrencyCode,
                     start_lsn,
                     tran_end_time,
                     operation,
                     CDCInstanceID
               FROM #TempCDCInsertData
               UNION ALL
               SELECT
                     PortfolioId,
                     MVDate + 2,
                     Amount,
                     AmountUSD,
                     ReconcileFlag,
                     ISOCurrencyCode,
                     start_lsn,
                     tran_end_time,
                     operation,
                     CDCInstanceID
               FROM #TempCDCInsertData
0
Comment
Question by:sbornstein2
  • 2
3 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40479346
I don't know about 'cleaner', but...

Declare @x int = 1 

WHILE @x <= 2
begin

INSERT INTO #TempCDCInsertData
               (
                     PortfolioId, 
                     MVDate, 
                     Amount,
                     AmountUSD, 
                     ReconcileFlag,
                     ISOCurrencyCode, 
                     start_lsn, 
                     tran_end_time, 
                     operation, 
                     CDCInstanceID
               )
               SELECT 
                     PortfolioId, 
                     DATEADD(day, @x, MVDate ) , 
                     Amounnt, 
                     AmountUSD, 
                     ReconcileFlag,
                     ISOCurrencyCode, 
                     start_lsn, 
                     tran_end_time, 
                     operation, 
                     CDCInstanceID
 FROM #TempCDCInsertData

SET @x = @x + 1

end

Open in new window

0
 

Author Closing Comment

by:sbornstein2
ID: 40480695
thanks
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40480705
Yep that's useful whenever you need to insert/update/whatever @x number of rows.

Thanks for the grade, this gets me to 7 million.  Good luck with your project.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question