Solved

SQL Query Syntax question

Posted on 2014-12-03
3
158 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 66

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 66

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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

691 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