This article is a mirror article of machine translation, please click here to jump to the original article.

View: 14919|Reply: 1

[Source] SQL Server determines whether there are databases, tables, columns, and views

[Copy link]
Posted on 4/18/2015 9:45:43 AM | | |

1 Determine whether the database exists
if exists (select * from sys.databases where name = 'database name')  
  drop database [database name]
2 Judge whether the table exists
if exists (select * from sysobjects where id = object_id(N'[table name]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)  
  drop table [table name]
3 Determine whether a stored procedure exists
if exists (select * from sysobjects where id = object_id(N'[stored procedure name]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)  
  drop procedure [stored procedure name]

4 Determine whether a temporary table exists
if object_id('tempdb.. #临时表名') is not null   
  drop table #临时表名
5 Determine whether the view exists
- Determine if there is an attempt to 'MyView52'
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA. VIEWS WHERE TABLE_NAME = N'MyView52')
PRINT 'Exists'
else
PRINT 'does not exist'
6 Determine whether a function exists
-- Determine if the function name to be created exists   
  if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ Function name]') and xtype in (N'FN', N'IF', N'TF'))   
  drop function [dbo]. [Function Name]
7 Obtain the information of the object created by the user
SELECT [name],[id],crdate FROM sysobjects where xtype='U'

   
8 Determine whether a column exists
if exists(select * from syscolumns where id=object_id('table name') and name='column name')  
  alter table table name drop column column name
9 Determine whether the column is self-incremented
if columnproperty(object_id('table'),'col','IsIdentity')=1  
  print 'self-added'  
else  
  print 'not self-added'
  
SELECT * FROM sys.columns WHERE object_id=OBJECT_ID('table name') AND is_identity=1
10 Determine whether an index exists in the table

if exists(select * from sysindexes where id=object_id('table name') and name='index name')   
  print 'exists'   
else   
  print 'does not exist'
11 View objects in the database
SELECT * FROM sys.sysobjects WHERE name='object name' SELECT * FROM sys.sysobjects WHERE name='object name'




Previous:Ckeditor and Ckfinder Integration Upload Image Function (.net Version)
Next:For security reasons, the file cannot be viewed. Please contact your system administrator and check the CKFinder profile.
Posted on 4/24/2018 2:22:04 PM |
Collection, very practical.                     
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com