SQL函式 判斷庫 表 儲存過程等是否存在

2022-08-24 13:12:17 字數 1393 閱讀 4041

---------------

-- 庫是否存在

if exists(select * from master..sysdatabases where name=n'庫名')

print 'exists'

else

print 'not exists'

---------------

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

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

-- 刪除表

drop table [dbo].[表名]

go---------------

--判斷要建立臨時表是否存在

if object_id('tempdb.dbo.#test') is not null

begin

print '存在'

endelse

begin

print '不存在'

end---------------

-- 判斷要建立的儲存過程名是否存在

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

-- 刪除儲存過程

drop procedure [dbo].[儲存過程名]

go---------------

-- 判斷要建立的檢視名是否存在

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[檢視名]') and objectproperty(id, n'isview') = 1)

-- 刪除檢視

drop view [dbo].[檢視名]

go---------------

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

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

-- 刪除函式

drop function [dbo].[函式名]

goif col_length('表名', '列名') is null

print '不存在'

select 1 from sysobjects where id in (select id from syscolumns where name='列名') and name='表名'

Sql判斷資料庫 表 儲存過程 函式是否存在

判斷資料庫是否存在 if exists select from sys.databases where name 資料庫名 drop database 資料庫名 判斷表是否存在 if exists select from sysobjects where id object id n 表名 and ...

sql 儲存過程 函式

1 建立儲存過程 1 語法 create or replace procedure procedure name argument type 型別 變數的說明 begin 執行部分 exception 可選的異常錯誤處理程式 end 建立過程 sql create or replace proced...

sql儲存過程 游標 迴圈表

游標例項 利用游標迴圈表 根據userid賦值 alter procedure cursor eg1 asbegin declare a int,error int declare temp varchar 50 臨時變數,用來儲存游標值 set a 1 set error 0 begin tran...