HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Delete from multiple tables using temp table variable

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
tablesdeletetempusingmultiplefromvariabletable

Problem

I'm previewing the data I'm about to delete, but this SQL looks a little redundant. Is there a better way to write this?

declare @history table(pid int)
insert into @history select pid from plans p where p.pidSynergy = 'P0022' and p.pid != 2885

select * from forecast_FTEs where pid in (select pid from @history)
select * from forecast_phases where pid in (select pid from @history)
select * from budget_FTEs where pid in (select pid from @history)
select * from budget_phases where pid in (select pid from @history)
select * from plans where pid in (select pid from @history)

Solution

p is a cryptic table alias and that != is not ANSI compliant, you should try and use <> instead. Also, the select * from is inefficient because it has to go back to the information schema and look up every column, you should try and only select the columns you really need.

Context

StackExchange Code Review Q#58053, answer score: 3

Revisions (0)

No revisions yet.