SQL的各種判斷

2021-06-09 11:20:26 字數 2094 閱讀 3441

刪除帶約束(預設值)的表的某一列

--獲取字段上面的約束名稱,組合成一句字串,用exec執行
if exists(select  1 from sysobjects  where id in (select cdefault from syscolumns where name='[列名]' and  id=object_id('[表名]'))) 

begin

declare @name varchar(100)

select @name=name from sysobjects where id in (select cdefault from syscolumns where name='[列名]' and id=object_id('[表名]'))

exec ('alter table [表名] drop constraint '+@name)

end--刪除列

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

alter table [表名] drop column [列名]

--判斷某列是否存在預設值,不存在則新增預設值

if not exists (select a.name as defaultname,b.name as tablename,* from sysobjects a

inner join sysobjects b on a.parent_obj = b.id

inner join syscolumns c on b.id=object_id('[表名]') and c.name='[列名]'

where a.xtype = 'd' and b.xtype = 'u' and b.name = '[表名]')

alter table [表名]

add default (getdate()) for [列名]

資料庫是否存在if exists(select * from master..sysdatabases where name=n'庫名')print 'exists'elseprint '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 nullbeginprint '存在'endelsebeginprint '不存在'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 nullprint '不存在'if not exists(select 1 from syscolumns where name='列名' and id=object_id('表名'))

各種判斷的集錦

注 prototype 原型 property 屬性 proto 是js變數都有的,而prototype是js函式特有的 typeof 判斷資料型別 undefined 未定義 number 數字 string 字串 boolean 邏輯 function 函式 object object arra...

sql的各種問題

1.case遇到符合條件的表示式就返回,即只有乙個返回值。例如 select sal,case whenempno 7369 then when sal 1000 then 低 when sal 2000 then 較低 when sal 3000 then 中 when sal 4000 then...

linux shell中if的各種判斷

shell程式設計中使用到得if語句內判斷引數 b當file存在並且是塊檔案時返回真 c當file存在並且是字元檔案時返回真 d當pathname存在並且是乙個目錄時返回真 e當pathname指定的檔案或目錄存在時返回真 f當file存在並且是正規檔案時返回真 g當由pathname指定的檔案或目...