Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: T-SQL problem - Perl solution?

by EdwardG (Vicar)
on Oct 25, 2004 at 14:57 UTC ( [id://402242]=note: print w/replies, xml ) Need Help??


in reply to Re^3: T-SQL problem - Perl solution?
in thread T-SQL problem - Perl solution?

In this case SQL 2000 produces the exact same execution plan for "SELECT 1" and "SELECT *" -

D:\>copy con: select1.sql set showplan_text on go if exists (select 1 from sysobjects where xtype = 'U' and name = 'jobs +') print 'exists' else print 'doesn''t exist' ^Z 1 file(s) copied. D:\>copy con: selectstar.sql set showplan_text on go if exists (select * from sysobjects where xtype = 'U' and name = 'jobs +') print 'exists' else print 'doesn''t exist' ^Z 1 file(s) copied. D:\>osql -dpubs -n -i selectstar.sql > selectstar.txt D:\>osql -dpubs -n -i select1.sql > select1.txt D:\>diff select1.txt selectstar.txt 3c3 < if exists (select 1 from sysobjects where xtype = 'U' and name = 'j +obs') --- > if exists (select * from sysobjects where xtype = 'U' and name = 'j +obs')

And while Win's SQL does compile without error, it may be a disaster in production. For example, this code (including the code you sanction) starts an infinite loop -

use pubs go create table Experimental_parameters (c char (1) primary key) insert into Experimental_parameters (c) values ('a') go create table another_table (c char (1) references Experimental_paramet +ers (c)) insert into another_table (c) values ('a') go WHILE (exists(select 1 from INFORMATION_SCHEMA.tables where table_name + = 'Experimental_parameters')) begin drop table Experimental_parameters -- fails end go print 'finished' -- never gets here

 

Replies are listed 'Best First'.
Re^5: T-SQL problem - Perl solution?
by monktim (Friar) on Oct 25, 2004 at 15:36 UTC
    Good point on the execution plans EdwardG. This example may be trivial enough that SELECT * is not better than SELECT 1. I should have stated more clearly that it is good practice to use SELECT * in an EXISTS because it can give performance improvements. Your mileage may vary.

    There is no inherent problem with using the WHILE clause just as I posted it. I certainly didn't sanction the code you posted but it is a good example and well worth noting. You never want to get stuck in an infinite loop. Error checking should also always be done.

    DROP TABLE Experimental_parameters IF @@ERROR <> 0 BEGIN --DO SOMETHING HERE (i.e. RAISERROR & RETURN) END
    Update: I just read the OP. I was replying to Re^2: T-SQL problem - Perl solution? which didn't indicate a temp table or code inside the WHILE. My sanctioned code could yield the behavior EdwardG posted based on the OP. The OP had
    if (SELECT COUNT(*) FROM #Experimental_parameters) <> 0 begin DROP TAB +LE Experimental_parameters end
    Error hanling code needs to be used inside the WHILE.

    Good call EdwardG. Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://402242]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found