SQL批量新增主鍵指令碼

2022-03-08 21:38:01 字數 4623 閱讀 4346

--

sql批量新增主鍵指令碼

----

實現功能:執行指令碼後資料庫中的所有資料表都會有乙個主鍵

--新增規則1:主鍵名稱為id(可自己修改),資料型別為整形自增一

--新增規則2:如果資料表已有主鍵,則不新增

--新增規則3:如果資料表沒主鍵但已存在自增一的標識列,則設定該標識列為主鍵

--新增規則4:如果資料表沒主鍵且已存在id列,則會刪除掉該列,記得要檢查該id列是否包含重要資料,如果有的話,請執行指令碼前改名。

--指令碼**開始

declare

@columnname

nvarchar(250

)set

@columnname='

id'--要新增的主鍵列名,可自己修改

declare

@tablename

nvarchar(250)--

游標中取出的資料表名

declare

@tableid

int--

游標中取出的資料表編號

declare

@identitycolumnname

nvarchar(250)--

資料表的已有標識列名稱

--宣告讀取資料庫所有資料表名和編號的游標

declare mycursor1 cursor

forselect name,id from dbo.sysobjects where

objectproperty(id, '

isusertable

') =

1order

byname --

開啟游標

open

mycursor1

--從游標裡取出資料賦值到我們剛才宣告的資料表名變數和資料表編號變數中

fetch

next

from mycursor1 into

@tablename,@tableid

--如果游標執行成功

while (@@fetch_status=0

) begin

--判斷當前資料表是否存在主鍵,如果資料表已有主鍵則不新增

ifnot

exists (select

*from information_schema.key_column_usage where table_name=''+

@tablename+''

) begin

--如果資料表中已經存在標識列,則將標識列設定為主鍵

ifexists(select

top1

1from sysobjects where

objectproperty(id, '

tablehasidentity

') =

1and

upper(name) =

upper(@tablename

))

begin

select

@identitycolumnname

=name from syscolumns where id =

@tableid

andcolumnproperty(id, name, '

isidentity

') =

1print

'當前資料表['+

@tablename+'

]沒有主鍵但有標識列['+

@identitycolumnname+'

]'exec ('

alter table ['+

@tablename+'

] add constraint pk_'+

@tablename+'

primary key clustered ('+

@identitycolumnname+'

) on [primary]

')

print

'成功設定資料表['+

@tablename+'

]已有標識列['+

@identitycolumnname+'

]為主鍵

'end

else

begin

print

'當前資料表['+

@tablename+'

]沒有主鍵和標識列'if

exists (select

*from syscolumns where id=

object_id(n'['

+@tablename+'

]') and name=''+

@columnname+''

)

begin

--如果已有id列設定有索引,則刪除資料表@tablename中指定字段@columnname對應的所有約束

declare

@constraintname

varchar (250)--

定義當前查詢的約束變數

--宣告讀取資料表中指定字段對應的所有約束列表游標

declare mycursor2 cursor

forselect name from sysobjects left

join sysconstraints on sysconstraints.constid=sysobjects.id where parent_obj=

object_id(''

+@tablename

+'') and colid=(select colid from syscolumns where id=

object_id(''

+@tablename

+'') and

objectproperty(id, n'

isusertable

') =

1and

upper(name)=

upper(@columnname

))

--開啟游標

open

mycursor2

--從游標裡取出資料賦值到約束名稱變數中

fetch

next

from mycursor2 into

@constraintname

--如果游標執行成功

while (@@fetch_status=0

)

begin

--刪除當前找到的約束

exec ('

alter table ['+

@tablename+'

] drop constraint ['+

@constraintname+'

]')

print

'已成功刪除資料表['+

@tablename+'

]字段['+

@columnname+'

]對應的約束['+

@constraintname+'

]'--用游標去取下一條記錄

fetch

next

from mycursor2 into

@constraintname

end--

關閉游標

close

mycursor2

--撤銷游標

deallocate

mycursor2

--如果存在列[id]則先刪除該列

exec ('

alter table ['+

@tablename+'

] drop column '+

@columnname+''

)

--新增資料表的主鍵列id

exec ('

alter table ['+

@tablename+'

] add '+

@columnname+'

[int] identity(1,1) not null primary key')

endelse

begin

--如果存在列[id]則直接新增主鍵列id

exec ('

alter table ['+

@tablename+'

] add '+

@columnname+'

[int] identity(1,1) not null primary key')

endprint

'成功設定資料表['+

@tablename+'

]列['

+@columnname+'

]為主鍵

'end

endelse

begin

print

'當前資料表['+

@tablename+'

]已有主鍵

'end

--用游標去取下一條記錄

fetch

next

from mycursor1 into

@tablename,@tableid

end--

關閉游標

close

mycursor1

--撤銷游標

deallocate

mycursor1

--指令碼**結束

批量新增人員資訊SQL指令碼

來至 gotesting軟體測試聯盟論壇 declare i intset i 1 declare codenum bigint set codenum 1 while i 1000 begin declare sql varchar 8000 set sql set sql insert into...

批量執行SQL指令碼

1.先要 xp cmdshell 這個東東給開啟,開啟方法如下 sp configure show advanced options 1reconfigure gosp configure xp cmdshell 1reconfigure go測試就這樣exec master.dbo.xp cmds...

批量新增刪除使用者指令碼

bin bash 本指令碼用於批量新增 刪除使用者,使用者初始密碼是123456,首次登陸需要修改密碼 格式 class stu.sh 引數1 引數2 引數3 引數1 起始使用者名稱,為純數字編號,長度必須大於6 引數2 新增人數 0 999 引數3 操作 1 新增,2 刪除 例 class stu...