Solved

SQL Query Syntax question

Posted on 2014-12-03
3
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

730 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