Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: T-SQL problem - Perl solution?

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


in reply to T-SQL problem - Perl solution?

The object_id() function is still available in SQL 2000 and still returns NULL if you supply the name of a non-existent table - try this example -

use pubs go if object_id('jobs') is not null print 'jobs table exists' else print 'jobs table does not exist' go --- jobs table exists

A better way would be to query the database schema, rather than abuse the object_id() function. Something like this:

use pubs go if exists(select 1 from information_schema.tables where table_name = ' +jobs') print 'jobs table exists' else print 'jobs table does not exist' go

 

Replies are listed 'Best First'.
Re^2: T-SQL problem - Perl solution?
by Win (Novice) on Oct 25, 2004 at 13:52 UTC
    Should the following work?
    WHILE (exists(select 1 from INFORMATION_SCHEMA.tables where table_name + = 'Experimental_parameters') ...
      That should work Win. What's nice about using information_schema is that it's ANSI standard so it will work on compliant dBs. Your question pertains directly to T-SQL but I like to code generically when there isn't a performance hit. When I work on other dBs I have to remember less vendor specific stuff. You don't need the SELECT 1 here. SELECT * with an EXISTS can yield internal performance optimizations with MS SQL Server. In this case it's probably trival but I thought I'd mention it.

        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

         

      That's a hard question to give a meaningful answer to; it could work in a limited set of circumstances, but I don't know your intention, or your schema for that matter. For instance, is Experimental_parameters a temporary table?

       

Log In?
Username:
Password:

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

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

    No recent polls found