'deletions from SQL table results in many more rows removed than expected
I have a table of stock symbols in a database called FINALDB, some of which are also in the NYSE table. Lets say that i only have 3 distinct ticker symbols there.
I want to reference all rows in the FINALDB.NYSE table which match these 3 tickers and remove those rows and write them to a table in a different database. what is happening is that the rows are in fact getting removed, but I am getting 3x the rows expected. It's as if is is doing the deletion 3 times because there are three tickers in the [FINALDB].[dbo].STOCKS table. if I have 50 tickers in that table that match tickers in the NYSE table, I am getting 50x the amount of rows deleted as I am expecting. it's performing a delete operation for each occurrence of a ticker in the other table. What am I doing wrong?
Thanks
This is my code:
DECLARE @currentDate date = CONVERT(date, GETDATE())
DELETE FROM FINALDB.dbo.NYSE
OUTPUT DELETED.Ticker, DELETED.Exchange,
DELETED.[Date], DELETED.[Open],
DELETED.[High], DELETED.[Low],
DELETED.[Close], DELETED.[Volume],
GETDATE() AS Delete_Date
INTO [STAGEDB].[dbo].Deleted_Data_Type5
FROM FINALDB.dbo.NYSE
INNER JOIN [FINALDB].[dbo].STOCKS ON [FINALDB].[dbo].STOCKS.[Ticker] = [FINALDB].[dbo].NYSE.[Ticker]
AND [FINALDB].[dbo].STOCKS.[Exchange] = [FINALDB].[dbo].NYSE.[Exchange]
AND [FINALDB].[dbo].STOCKS.[Date] = @currentDate
Solution 1:[1]
thanks for the suggestion of changing it to a SELECT to compare results. I failed to include the top part of the script, which runs in python. for ex in exchanges:
This was resulting in the Stock exchange (singapore) being referenced 3 times, one for each occurrence of the symbol in the table. Name: Combined, dtype: object 0 SG 1 SG 2 SG
So, it was doing the delete operation over and over (how there was anything left to delete on subsequent runs escapes me)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bob |
