How can this error be solved? The code produces an error when @count reaches 1:
There is already an object named ‘did2’ in the database.
I read that I am not allowed to use select into twice in a loop (for whatever reason, in PL/SQL this is a very standard pattern).
BEGIN
DECLARE @count INT
SET @count = 0
WHILE (@count < 200)
BEGIN
DECLARE @did2 DATETIME
SELECT DATEADD(ss, @count, '01.01.2002 00:00:00') as did2 into did2 -- throws an error the 2nd time
INSERT INTO [DbPriceHistorTesty].[dbo].[QuoteHistories]
([Id]
,[ContractId]
,[Open]
,[Close]
,[Min]
,[Max]
,[SenderId]
,[CreatedAt])
VALUES (
@did2
,1
,1
,1
,1
,1
,'1'
,GETDATE())
SET @count = (@count + 1)
END
END
You are creating a table
did2for every rep.You probably want this instead.
You should also consider changing your dateformat to YYYY-MM-DDTHH:MM:SS.
That way you will not be dependent on local datetime settings.