In SQL Server, how can I tell if a SQL Server table exists? Here is a detailed description of the method, for your reference, to help you understand the SQL Server table a little bit
How to determine the existence of a table or database in SQL Server, but in actual use, you need to determine the status State bit:
Some of these status bits can be set by the user using sp_dboption (read only, dbo use only, single user, and so on):
1 = autoclose; use sp_dboption settings. The database is completely shut down and its resources are released after the last user logs off.
4 = select into/bulkcopy; use sp_dboption settings. Allows the use of the Select into statement and fast bulk copy.
8 = trunc. Log on chkpt; use sp_dboption settings. If the database is in log truncation mode, the checkpoint truncates the non-active portion of the log. This option can only be set for the master database. Torn page detection, use sp_dboption settings. You can detect torn pages.
= Loading.
* = Pre recovery.
= Recovering.
Recovered = not.
offline = n; use sp_dboption settings. The database will be offline.
1024x768 = read Only; use sp_dboption settings. Users can only read data in the database and cannot modify it.
2048 = dbo using only; sp_dboption settings. Only the database owner can use the database.
4096 = Single user; use sp_dboption settings. Only one user can access the database at a time.
32768 = emergency mode.
4194304 = autoshrink.
1073741824 = cleanly shutdown.
Multiple bits can be opened at the same time.
For example: Determine if a database is offline
SELECT * from master.dbo.sysdatabases where name= ' pubs ' and status<>512
Determine whether the Table object exists in SQL Server:
Select COUNT (*) from sysobjects where id = object_id (' database name '). Owner. Table name ')
if exists
(SELECT COUNT (*) from sysobjects where id = object_id (' database name. Owner. Table name '))
print ' exists '
Else
print ' does not exist '
In SQL Server, determine whether the fields in the table exist:
if exists (SELECT * from syscolumns where name= ' colname1 ' and id=object_id (' database name. Owner. Table name '))
print ' exists '
Else
print ' does not exist '
colname1 field exists on behalf of table tablename1
Cases:
SELECT * from syscolumns where name= ' Test ' and id=object_id (' Dbo.test ')
In Access, determine whether the Table object exists:
In fact, the Access database also has a system table, which holds the object name
Select Count (*) as Qty from Msysobjects Where ((msysobjects.name) like ' table name ');
Copy CodeThe code is as follows:
Whether the library exists
if exists (select * from Master.. sysdatabases where name=n ' library name ')
print ' exists '
Else
print ' NOT EXISTS '
---------------
--Determine if the name of the table to be created exists
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Table name] ') and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
--Delete Table
drop table [dbo]. [Table name]
GO
---------------
Whether the-----column exists
IF col_length (' table name ', ' column name ') is NULL
PRINT ' NOT EXISTS '
ELSE
PRINT ' exists '
ALTER TABLE name DROP constraint default value name
Go
ALTER TABLE table name drop column name
Go
-----
--Determine if you want to create a temporary table exists
If object_id (' tempdb.dbo. #Test ') is not Null
Begin
print ' exists '
End
Else
Begin
print ' does not exist '
End
---------------
--Determine if the stored procedure name to be created exists
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Stored procedure name] ') and OBJECTPROPERTY (ID, N ' isprocedure ') = 1)
--Delete stored procedures
drop procedure [dbo]. [Stored procedure name]
GO
---------------
--Determine if the name of the view you want to create exists
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ View name] ') and OBJECTPROPERTY (ID, N ' isview ') = 1)
--Delete View
Drop view [dbo]. [View name]
GO
---------------
--Determine if the name of the function to be created exists
if exists (select * from sysobjects where xtype= ' fn ' and name= ' function name ')
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Function name] ') and xtype in (n ' FN ', n ' IF ', n ' TF '))
--delete function
Drop function [dbo]. [function name]
GO
If Col_length (' Table name ', ' column name ') is null
print ' does not exist '
Select 1 from sysobjects where ID in (select ID from syscolumns where name= ' column name ') and name= ' table name '
SQL Server various judgments exist (table name, function, stored procedure, etc.)