Does anyone know what i’m doing wrong here, I have a webpage that gets currency data from a remote source, I get the data and insert it into sql database via a stored procedure. If i put truncate in front of the insert statement, it truncates the table and inserts the last record.
If i remove the truncate, it inserts all the records.
i.e
truncate table tblTablename;
insert into tblTablename
(columns)
values
(data)
The above will insert the last record from 289 records.
If i remove truncate all 289 records are inserted.
I have tried using waitfor, for 1 second but that failed to work either.
I’m not sure what else to do, so any help would be appreciated
In webpage I have a foreach loop
George
/———————- SQL Code —————–
ALTER PROCEDURE [dbo].[atSP_InsertCurrency]
-- Add the parameters for the stored procedure here
@CurrencyCountry VarChar(150),
@CurrencyRate VarChar(150),
@UpdateSuccessFail INT OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
TRUNCATE TABLE [dbo].[at_CurrencyRates];
WAITFOR DELAY '000:00:01'
INSERT INTO [dbo].[at_CurrencyRates]
(
[CurrencyCode],
[CurrencyExchangeRate]
)
VALUES
(
@CurrencyCountry,
@CurrencyRate
)
IF(@@ROWCOUNT > 0)
BEGIN
select @UpdateSuccessFail = '1'
END
ELSE
BEGIN
select @UpdateSuccessFail = '0'
END
END
You need to move
TRUNCATE TABLE [dbo].[at_CurrencyRates];out of the stored procedure if you are calling it 289 times to insert row by row.Every time you call the stored procedure it deletes all the rows from the table so you will always only end up with the one row that you just inserted.
Better would be to alter the stored procedure to do the insert of all required rows in one go rather than just one at a time. You can use a table valued parameter to pass in all of the desired rows then you would just need a
TRUNCATEfollowed by anINSERT [dbo].[at_CurrencyRates] ... SELECT * FROM @TVP.