sql使用命令(一)

2021-04-27 14:34:09 字數 3819 閱讀 6281

--刪除database下所有非系統儲存過程(僅適用於sql2005以上)

declare @procname varchar(500)

declare cur cursor for 

select [name] from sys.objects where type = 't'

open cur 

fetch next from cur into @procname

while @@fetch_status = 0

begin

if @procname <> 'deletealltables'

exec('drop table ' + @procname)

fetch next from cur into @procname

endclose cur

deallocate cur

go--刪除儲存過程

declare @string varchar(8000)

while exists(select name from sysobjects where type='p' and status>=0)

begin

select @string='drop procedure '+name from sysobjects where type = 'p' and status>=0

--select @string

exec(@string)

endgo

--預設值或default 約束

declare @string varchar(8000)

while exists(select name from sysobjects where xtype='d')

begin

select @string='alter table '+b.name+' drop constraint '+a.name

from (select parent_obj,name from sysobjects where xtype='d') a,

(select id,name from sysobjects where objectproperty(id, n'isusertable') = 1) b

where a.parent_obj=b.id

exec(@string)

endgo

--unique 約束

declare @string varchar(8000)

while exists(select name from sysobjects where xtype='uq')

begin

select @string='alter table '+b.name+' drop constraint '+a.name

from (select parent_obj,name from sysobjects where xtype='uq') a,

(select id,name from sysobjects where objectproperty(id, n'isusertable') = 1) b

where a.parent_obj=b.id

exec(@string)

endgo

--foreign key 約束

declare @string varchar(8000)

while exists(select name from sysobjects where type='f')

begin

select @string='alter table '+b.name+' drop constraint '+a.name

from (select parent_obj,name from sysobjects where type='f') a,

(select id,name from sysobjects where objectproperty(id, n'isusertable') = 1) b

where a.parent_obj=b.id

exec(@string)

endgo

--primary key 約束

declare @string varchar(8000)

while exists(select name from sysobjects where xtype='pk')

begin

select @string='alter table '+b.name+' drop constraint '+a.name

from (select parent_obj,name from sysobjects where xtype='pk') a,

(select id,name from sysobjects where objectproperty(id, n'isusertable') = 1) b

where a.parent_obj=b.id

exec(@string)

endgo

--觸發器

declare @string varchar(8000)

while exists(select name from sysobjects where xtype='tr')

begin

select @string='drop trigger '+name from sysobjects where xtype='tr'

exec(@string)

endgo

--索引

declare @string varchar(8000)

while exists(

select table_name= o.name,index_name= x.name 

from sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk

where o.type in ('u') 

and convert(bit,(x.status & 0x800)/0x800)=0

and x.id = o.id

and o.id = c.id

and o.id = xk.id

and x.indid = xk.indid

and c.colid = xk.colid

and xk.keyno <= x.keycnt

and permissions(o.id, c.name) <> 0

and (x.status&32) = 0 -- no hypothetical indexes

group by o.name,x.name)

begin

select top 1 @string='drop index '+o.name+'.'+ x.name 

from sysobjects o, sysindexes x, syscolumns c, sysindexkeys xk

where o.type in ('u') 

and convert(bit,(x.status & 0x800)/0x800)=0

and x.id = o.id

and o.id = c.id

and o.id = xk.id

and x.indid = xk.indid

and c.colid = xk.colid

and xk.keyno <= x.keycnt

and permissions(o.id, c.name) <> 0

and (x.status&32) = 0 -- no hypothetical indexes

group by o.name,x.name

exec(@string)

endgo

SQL常用命令

sql常用命令 資料的增刪改查 增加資料 插入資料 insert into 表名 字段 字段 values 值,值,值.按需要字段填寫 insert into 表名 values 值,值,值.插入全部字段,自動增長列不寫 刪除資料 記得有外來鍵先刪除主鍵表裡的相應內容 刪除表裡的內容 delete ...

SQL常用命令

a b兩表,找出id欄位中,存在a表,但是不存在b表的資料。a表總共13w資料,去重後大約3w條資料,b表有2w條資料,且b表的id欄位有索引。使用 not in 容易理解,效率低 執行時間為 1.395秒 select 種類,sum 數量 from 表名 group by 種類 order by ...

SQL常用命令

返回某個表的列資訊 exec sp clomuns 表名 檢視某個表的所有資訊 exec sp help 表名 查詢資料庫中所有含有某一列的所有表 select name from sysobjects where id in select id from syscolumns where sysc...