SQLServer2008語句查詢

2021-07-22 15:29:55 字數 2073 閱讀 9325

1 判斷資料庫是否存在

if exists (select * from sys.databases where name = '資料庫名')  

drop database [資料庫名] 

2 判斷表是否存在

if exists (select * from sysobjects where id = object_id('表名') and objectproperty(id, 'isusertable') = 1)  

drop table [表名] 

3 判斷儲存過程是否存在

if exists (select * from sysobjects where id = object_id('儲存過程名') and objectproperty(id, 'isprocedure') = 1)  

drop procedure [儲存過程名]

4 判斷臨時表是否存在

if object_id('tempdb..#臨時表名') is not null    

drop table #臨時表名

5 判斷檢視是否存在

--判斷是否存在'myview52'這個試圖

if exists (select table_name from information_schema.views where table_name = 'myview52')

print '存在'

else

print '不存在'

6 判斷函式是否存在 

--  判斷要建立的函式名是否存在    

if exists (select * from dbo.sysobjects where id = object_id('[dbo].[函式名]') and xtype in ('fn', 'if', 'tf'))    

drop function [dbo].[函式名] 

7 獲取使用者建立的物件資訊

select [name],[id],crdate from sysobjects where xtype='u' 

/*  

xtype 的表示引數型別,通常包括如下這些  

c = check 約束  

d = 預設值或 default 約束  

f = foreign key 約束  

l = 日誌  

fn = 標量函式  

if = 內嵌表函式  

p = 儲存過程  

pk = primary key 約束(型別是 k)  

rf = 複製篩選儲存過程  

s = 系統表  

tf = 表函式  

tr = 觸發器  

u = 使用者表  

uq = unique 約束(型別是 k)  

v = 檢視  

x = 擴充套件儲存過程  

*/ 

8 判斷列是否存在

if exists(select * from syscolumns where id=object_id('表名') and name='列名')  

alter table 表名 drop column 列名

9 判斷列是否自增列

if columnproperty(object_id('table'),'col','isidentity')=1  

print '自增列'  

else  

print '不是自增列'

select * from sys.columns where object_id=object_id('表名')  and is_identity=1

10 判斷表中是否存在索引

if exists(select * from sysindexes where id=object_id('表名') and name='索引名')    

print  '存在'    

else    

print  '不存在'

11 檢視資料庫中物件

select * from sys.sysobjects where name='物件名'  select * from sys.sysobjects where name='物件名'

Sql Server 2008 收縮日誌

收縮日誌 alter database dnname set recovery with no wait goalter database dnname set recovery 簡單模式 gouse dnname godbcc shrinkfile n dnname log 11,truncate...

徹底解除安裝sql server2008

微軟的開發工具在按裝和解除安裝時都讓人頭疼,只能是裝在c盤,裝在其他盤時最容易出事 在重新按裝的時候一定要把以前的例項解除安裝完才行。要不就會出錯。在解除安裝sql server後,其實還沒有完成,還要把登錄檔資訊完全刪乾淨,下面就將教您徹底刪除sql server登錄檔的方法,供您參考。在解除安裝...

SQL SERVER 2008清除日誌

在sql2008 中清除日誌就必須在簡單模式下進行,等清除動作完畢再調回到完全模式。方案一 完全命令模式 use master goalterdatabase dnname setrecovery with no wait goalterdatabase dnname setrecovery 簡單模...