Hi list!

My problem is, how to check if a table exists in MS SQL-Server.
Creating the same table forces a sql error message. I only can catch the FALSE
at the odbc_exec function, when executing the SQL-command "SELECT * INTO table-name ....".

Any better ideas?

Andreas

    select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = <your table name>

    If it exists, you'll get one row back, if not, none.

      Oh, that's in 7.0 by the way; I don't have access to earlier versions, so it may not work in those...

        Thanks, but it doesn't work in my version 6.0.
        I get the message: Msg 208, Level 16, State 1
        Invalid object name 'INFORMATION_SCHEMA.TABLES'.

          Okay, just been looking through tho oh so well written backwards compatibility docs; try:

          exec sp_tables <your table name>

          Again, returns one row if it exists, none if it doesn't.

            Rob,

            it works in the query analyzer of the enterprise manager, but it doesn't in the php script.

            I get this message:
            Warning: SQL error: [Microsoft][ODBC SQL Server Driver][SQL
            Server]Cannot open a cursor on a stored procedure that has anything
            other than a single select statement in it , SQL state 37000 in
            SQLExecDirect in ./DP_get_SN_4_boardtype.php on line 123

            I think I work with my first solution, it's not so comfortable, but it's working and I
            can't spent a lot of time for debuging.

            Thanks again

            Andreas from Bavaria

              Write a Reply...