Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to check if a table exists in a MS SQL server in VB

Hi,
I want to dynamically create a table named employee, for example in SQL server
in VB code. Before I create the table, how can I check if the table already
exists or not in the database. Any help would be appreciated.
[232 byte] By [David] at [2007-11-9 21:07:53]
# 1 Re: How to check if a table exists in a MS SQL server in VB
First, this is a SQL discussion group, not SQL Server.

Second, why would you do this?

Third, If you generate table creation code in SQL Server it will have the
check statement.

Mark

"David" <dapengc@u.washing.edu> wrote:
>
>Hi,
>I want to dynamically create a table named employee, for example in SQL
server
>in VB code. Before I create the table, how can I check if the table already
>exists or not in the database. Any help would be appreciated.
MarkN at 2007-11-11 23:55:29 >
# 2 Re: How to check if a table exists in a MS SQL server in VB
If its not too late.

Use the following stored procedure and pass the table name to it. Check the
return value and you'll get a 1 if the table exists and a 0 if it doesn't.

create procedure Table_Exists
@tbl varchar(50)
as
return (select count(*) from sysobjects where type = 'U' and name = @tbl)
go

"David" <dapengc@u.washing.edu> wrote:
>
>Hi,
>I want to dynamically create a table named employee, for example in SQL
server
>in VB code. Before I create the table, how can I check if the table already
>exists or not in the database. Any help would be appreciated.
KevinV at 2007-11-11 23:56:30 >
# 3 Re: How to check if a table exists in a MS SQL server in VB
"xx" <s_m_g_m@hotmail.com> wrote in message news:3cda8cba$1@10.1.10.29...
>
> You can check int the following way:
>
> "SELECT name FROM sysObjects WHERE name like 'employee'"
>
> if recordset has result then the table exists...

SELECT name FROM sysobjects WHERE name = 'employee' AND OBJECTPROPERTY(id,
'IsUserTable') = 1

If you don't use the OBJECTPROPERTY bit, an object may exist named
'employee' that isn't a table.

--
Colin McGuigan
Colin McGuigan at 2007-11-11 23:57:34 >
# 4 Re: How to check if a table exists in a MS SQL server in VB
You can check int the following way:

"SELECT name FROM sysObjects WHERE name like 'employee'"

if recordset has result then the table exists...

"MarkN" <m@n.com> wrote:
>
>First, this is a SQL discussion group, not SQL Server.
>
>Second, why would you do this?
>
>Third, If you generate table creation code in SQL Server it will have the
>check statement.
>
>Mark
>
>"David" <dapengc@u.washing.edu> wrote:
>>
>>Hi,
>>I want to dynamically create a table named employee, for example in SQL
>server
>>in VB code. Before I create the table, how can I check if the table already
>>exists or not in the database. Any help would be appreciated.
>
xx at 2007-11-11 23:58:33 >
# 5 Re: How to check if a table exists in a MS SQL server in VB
MArk, why are you such a pompous, sarcastic ****?!

"MarkN" <m@n.com> wrote:
>
>First, this is a SQL discussion group, not SQL Server.
>
>Second, why would you do this?
>
>Third, If you generate table creation code in SQL Server it will have the
>check statement.
>
>Mark
>
>"David" <dapengc@u.washing.edu> wrote:
>>
>>Hi,
>>I want to dynamically create a table named employee, for example in SQL
>server
>>in VB code. Before I create the table, how can I check if the table already
>>exists or not in the database. Any help would be appreciated.
>
Eliot Pister at 2007-11-11 23:59:43 >