SqlServer常用語句整理(後續更新)

2021-07-24 07:57:29 字數 3305 閱讀 8372

sysobjecs中物件型別(xtype):

af = aggregate function (clr)

c = 約束

d = 預設值約束

f = 外來鍵約束

l = log

fn = 標量值函式

fs = assembly (clr) 標量值函式

ft = assembly (clr) 錶值函式

if = in-lined table-function

it = internal table

p = 儲存過程

pc = assembly (clr) 儲存過程

pk = primary key constraint (type is k)

rf = replication filter stored procedure

s = 系統表

sn = synonym

sq = service queue

ta = assembly (clr) dml trigger

tf = table function

tr = sql dml trigger

tt = table type

u = 使用者表

uq = unique constraint (type is k)

v = 試圖

x = 擴充套件儲存過程

--批量修改表名為小寫

declare @sql varchar(300)--,@rowcount varchar(10),@dyncnum int

declare @tablename varchar(100)

declare cursor1 cursor for

select name from sysobjects where xtype = 'u'

order

by name

open cursor1

fetch

next

from cursor1 into @tablename

while @@fetch_status=0

begin

set @sql='sp_rename '''+@tablename+''','''+lower(@tablename)+'''' -- 此為修改為小寫,如果修改為大寫「upper」

exec(@sql)

fetch

next

from cursor1 into @tablename

endclose cursor1

deallocate cursor1

--批量修改欄位名為小寫

declare @sql

varchar(300)

declare @tablecolumnname varchar(100), @columnname varchar(100)

declare cursor1 cursor

forselect b.name+'.['+a.name+']',a.name from syscolumns a ,sysobjects b where a.id = object_id(b.name) and b.xtype = 'u'

and a.xtype <>189

and a.xtype <>34

and a.xtype <>35

and a.xtype <>36

open cursor1

fetch

next

from cursor1 into @tablecolumnname,@columnname

while @@fetch_status=0

begin

set @sql='sp_rename '''+@tablecolumnname+''','''+lower(@columnname)+''',''column''' -- 此為修改為小寫,如果修改為大寫「upper」

exec(@sql)

fetch

next

from cursor1 into @tablecolumnname,@columnname

endclose cursor1

deallocate cursor1

--批量修改架構名(包括表名和儲存過程名)

declare @name sysname

declare csr1 cursor

forselect table_name from information_schema.tables

open csr1

fetch

next

from csr1 into @name

while (@@fetch_status=0)

begin

set @name='原架構名.'+@name

exec sp_changeobjectowner @name, '新架構名'

fetch

next

from csr1 into @name

endclose csr1

deallocate csr1

--快速查詢表的總記錄數

select

rows

from sysindexes where id= object_id('rpt2014' ) and indid< 2

--非遞迴查詢樹形結構表的所有子節點

with tree as (

select * from dbo .mgrobjtype where id='00000000-a001-0000-0000-000000000000'

union

allselect mgrobjtype.* from dbo .mgrobjtype, tree where tree.id= dbo.mgrobjtype .parentid

)select * from tree

--清除查詢快取

dbcc freeproccache

dbcc dropcleanbuffers

--跨伺服器的資料庫查詢

select * from

opendatasource('sqloledb' , 'data source=172.18.24.245;user id=sa;password=aaa*'). centerobj_xx.dbo .tablelog as a

sqlserver常用語句

刪除主鍵 alter table 表名 drop constraint 主鍵名 新增主鍵 alter table 表名 add constraint 主鍵名 primary key 欄位名1,欄位名2 新增非聚集索引的主鍵 alter table 表名 add constraint 主鍵名 prim...

SQLSERVER常用語句

dbcc cleantable db name table name alter table drop column語句刪除可變長度列或text dbcc dbreindex 重建指定資料庫的乙個或多個索引 dbcc indexdefrag 對錶或檢視上的索引和非聚集索引進行碎片整理 dbcc pi...

SQL Server效能常用語句

檢視各表的資料行數 select o.name,i.rows from sysobjects o,sysindexes i where o.id i.id and o.xtype u and i.indid 2 order by o.name 計算資料庫中各個表每行記錄所占用空間 select fr...