Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

T-SQL problem - Perl solution?

by Win (Novice)
on Oct 25, 2004 at 10:46 UTC ( [id://402189]=perlquestion: print w/replies, xml ) Need Help??

Win has asked for the wisdom of the Perl Monks concerning the following question:

My MS SQL Server 7 code snippet was something like:

If object_id(“table_name”) is not null begin … end

This does not work for MS SQL Server 2000. So I tried something along the lines of:

if (SELECT COUNT(*) FROM #Experimental_parameters) <> 0 begin DROP TABLE Experimental_parameters end

The problem is that this updated solution requires the existence of the table. So it is not the same thing as the first.

I am activating this with a Perl script. Maybe there is not a T-SQL solution for this, in which case I will need a Perl work around.

Any suggestions would be welcome, even though I know that this is not really a Perl question.

Replies are listed 'Best First'.
Re: T-SQL problem - Perl solution?
by EdwardG (Vicar) on Oct 25, 2004 at 11:10 UTC

    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

     

      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.

        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?

         

Re: T-SQL problem - Perl solution?
by mpeppler (Vicar) on Oct 25, 2004 at 11:24 UTC
    Another alternative:
    if exists (select * from sysobjects where type = 'U' and name = 'table +_name') begin drop table table_name end
    Note that you also need to take the object owne into account - the code above assumes the table is owned by 'dbo'.

    Michael

      Two minor nits;

      • This works for MSSQL 2000, but may break with MSSQL 2005
      • The type column exists for backward compatibility only; xtype is the right column (if you must). They are different.

       

        To be fair that is almost exactly what SQL Server 2000 emits in a new CREATE TABLE template if you have specified that you should create the appropriate DROP TABLE ...

        /J\

        Thanks.

        I'm not really an MS-SQL user - I just apply the Sybase syntax, which usually works (though it looks like MS is moving in a different direction for system tables...)

        Michael

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found