Solved

How to insert multiple records from a select query result?

Posted on 2009-05-12
2
187 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
[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 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

635 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