Solved

How to insert multiple records from a select query result?

Posted on 2009-05-12
2
182 Views
Last Modified: 2012-05-06
There are 30 exchanges reference rates each day needs to carry down to next day. I use the following SQL to select the yesterday's result and hope to insert them to the same table but with different date.

However, it just insert the last record of previous day.

How should I write the SP?


ALTER PROCEDURE [dbo].[SP_Update_Exchanges] 

@fordate datetime

AS

BEGIN

BEGIN TRAN TRAN_SP_Ex

Declare @currencyid int

Declare @currency nvarchar(50)

Declare @referencerate decimal(18,6)

Declare @buy decimal(18,6)

Declare @sell decimal(18,6)

 

SET NOCOUNT ON;

select * from exchanges where systemdate = @fordate

if @@rowcount <> 1

      begin

            SELECT @currencyid=currencyid, @currency=currency, 

            @referencerate=referencerate, @buy=buy, @sell=sell from exchanges 

            where systemdate = datediff(day,1,@fordate)

 

            insert into exchanges (currencyid,currency,referencerate,buy,sell,systemdate) 

            values (@currencyid,@currency,@referencerate,@buy,@sell,@fordate)

      end

IF @@ERROR <> 0

	ROLLBACK TRAN TRAN_SP_Ex

ELSE

	COMMIT TRAN TRAN_SP_Ex

END

Open in new window

0
Comment
Question by:techques
2 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 50 total points
ID: 24371552
Try this
ALTER PROCEDURE [dbo].[SP_Update_Exchanges] 

@fordate datetime

AS

BEGIN

BEGIN TRAN TRAN_SP_Ex
 

 

SET NOCOUNT ON;

                 insert into exchanges (currencyid,currency,referencerate,buy,sell,systemdate) 

             SELECT currencyid, currency, 

            referencerate, buy, sell,@fordate from exchanges 

            where systemdate = datediff(day,1,@fordate)

    

IF @@ERROR <> 0

	ROLLBACK TRAN TRAN_SP_Ex

ELSE

	COMMIT TRAN TRAN_SP_Ex

END

Open in new window

0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24371739
this should work for you...



ALTER PROCEDURE [dbo].[SP_Update_Exchanges] 

@fordate datetime

AS

BEGIN

BEGIN TRAN TRAN_SP_Ex

 

 

SET NOCOUNT ON;

                 insert into exchanges (currencyid,currency,referencerate,buy,sell,systemdate) 

             SELECT currencyid, currency, 

            referencerate, buy, sell,@fordate from exchanges 

            where systemdate = dateadd(dd,-1,@fordate)

    

IF @@ERROR <> 0

        ROLLBACK TRAN TRAN_SP_Ex

ELSE

        COMMIT TRAN TRAN_SP_Ex

END

Open in new window

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why does this keep coming up NULL? 2 44
Error message when scheduling a job using a linked Server 12 55
Insert statement is inserting duplicate records 15 58
Truncate vs Delete 63 102
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now