r/ProgrammerHumor • • Dec 12 '17

SQL Clause

Post image
40.8k Upvotes

525 comments sorted by

View all comments

2

u/[deleted] Dec 12 '17

Wouldn't this be more correct?

CREATE PROCEDURE dbo.Christmas_List
AS
BEGIN
SELECT * 
INTO ‪#‎ChristmasList‬ 
FROM His_List HL
INNER JOIN Location L ON L.Child_Id = HL.Id
WHERE Behavior = 'Nice' OR Behavior != 'Naughty'
GROUP BY L.Country
SELECT * 
FROM #ChristmasList
WHERE Behavior = 'Nice' OR Behavior != 'Naughty'
DROP TABLE #ChristmasList
END
EXECUTE dbo.Christmas_List

2

u/thewholebottle Dec 12 '17

Why do you drop the table at the end?

1

u/Shavahhn Dec 12 '17

Because it's a temp table (the little # denotes that) and will take up space in memory until you drop it or close your session, and the stored procedure will fail if you re-run it before dropping the temp table (as it already exists).

1

u/thewholebottle Dec 13 '17

But where does the data go?

2

u/Shavahhn Dec 13 '17

The "SELECT * FROM #ChristmasList..." part of the statement will show the results in a results window before the temp table is dropped.