常用指令碼範例(SQL Server)

2021-04-13 05:00:20 字數 1569 閱讀 4190

--增加關聯(新增表tablea與表tableb的關係)

if not exists (select * from sysobjects where id = object_id('fk_tablea_tableb'))

begin

alter table [dbo].[tablea] add  

constraint [fk_tablea_tableb] foreign key

([tableb_tablebid]  --tablea中的外建

) references [dbo].[tableb] (

[tablebid]               --tableb的主鍵

)end

go--刪除關聯

if  exists (select * from sysobjects where id = object_id('fk_tablea_tableb'))

begin

alter table [dbo].[tablea] drop constraint [fk_tablea_tableb]

endgo

--增加唯一約束

if not exists (select * from sysobjects where id = object_id('ix_tablea'))

begin

alter table [dbo].[tablea] with nocheck add   constraint [ix_tablea] unique  nonclustered

([tableaid]   --tablea的主鍵  

)  on [primary]

endgo

--增加字段(在tablea中新增名為columnsa的字段)

if (not exists(select * from syscolumns where ([id] = (select [id] from sysobjects where [name] ='tablea') and [name]= 'columnsa')))

begin

alter table tablea add columnsa char(20) null

endgo

--刪除字段

if ( exists(select * from syscolumns where ([id] = (select [id] from sysobjects where [name] ='tablea') and [name] ='columnsa')))

begin

alter table tablea drop column columnsa 

endgo

--修改字段長度

if (exists(select * from syscolumns where ([id] = (select [id] from sysobjects where [name] ='tablea') and [name]= 'columnsa')))

begin

alter table tablea alter column columnsa char(50) null

endgo

SQLServer 建立儲存過程範例

use 資料庫名稱 goset ansi nulls on goset quoted identifier on gocreate procedure dbo 儲存過程名稱 引數名稱1 char 20 引數名稱2 資料型別 長度 as begin 定義變數 declare 變數名稱 資料型別 長度 ...

常用Lambda範例

原文 1.用where 方法進行篩選 using system using system.linq 查詢所有值大於3被索引的元素 2.用orderby 方法進行排序 using system using system.linq 對所有元素按照 10進行降序排列 以上的兩個排序demo都是採用預設的i...

shell指令碼 和 使用範例

file dir1 dir2 dir3 my.file.txt 可以用 分別替換得到不同的值 刪掉第乙個 及其左邊的字串 dir1 dir2 dir3 my.file.txt 刪掉最後乙個 及其左邊的字串 my.file.txt 刪掉第乙個 及其左邊的字串 file.txt 刪掉最後乙個 及其左邊的...