/*
--整理者:永恒de影
--整理時間:2010/06/08
--內(nèi)容:SQL函數(shù)的介紹:
*/
--★★SQL2000查詢出各(某)表字段的屬性:★★★★★★★★★★★★★★★★★★
SELECT
表名 = case when a.colorder=1 then d.name else '' end,
表說明 = case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序號 = a.colorder,
字段名 = a.name,
標識 = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主鍵 = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
類型 = b.name,
占用字節(jié)數(shù) = a.length,
長度 = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小數(shù)位數(shù) = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允許空 = case when a.isnullable=1 then '√'else '' end,
默認值 = isnull(e.text,''),
字段說明 = isnull(g.[value],'')
FROM
syscolumns a
left join
systypes b
on
a.xusertype=b.xusertype
inner join
sysobjects d
on
a.id=d.id and d.xtype='U' and d.name>'dtproperties'
left join
syscomments e
on
a.cdefault=e.id
left join
sysproperties g
on
a.id=g.id and a.colid=g.smallid
left join
sysproperties f
on
d.id=f.id and f.smallid=0
where
d.name='要查詢的表' --如果只查詢指定表,加上此條件
order by
a.id,a.colorder
--★★SQL2005查詢出各(某)表字段的屬性:★★★★★★★★★★★★★★★★★★
-- ========================================================================
-- 表結(jié)構(gòu)信息查詢
-- 鄒建 2005.08(引用請保留此信息)
-- ========================================================================
SELECT
TableName=CASE WHEN C.column_id=1 THEN O.name ELSE N'' END,
TableDesc=ISNULL(CASE WHEN C.column_id=1 THEN PTB.[value] END,N''),
Column_id=C.column_id,
ColumnName=C.name,
PrimaryKey=ISNULL(IDX.PrimaryKey,N''),
[IDENTITY]=CASE WHEN C.is_identity=1 THEN N'√'ELSE N'' END,
Computed=CASE WHEN C.is_computed=1 THEN N'√'ELSE N'' END,
Type=T.name,
Length=C.max_length,
Precision=C.precision,
Scale=C.scale,
NullAble=CASE WHEN C.is_nullable=1 THEN N'√'ELSE N'' END,
[Default]=ISNULL(D.definition,N''),
ColumnDesc=ISNULL(PFD.[value],N''),
IndexName=ISNULL(IDX.IndexName,N''),
IndexSort=ISNULL(IDX.Sort,N''),
Create_Date=O.Create_Date,
Modify_Date=O.Modify_date
FROM sys.columns C
INNER JOIN sys.objects O
ON C.[object_id]=O.[object_id]
AND O.type='U'
AND O.is_ms_shipped=0
INNER JOIN sys.types T
ON C.user_type_id=T.user_type_id
LEFT JOIN sys.default_constraints D
ON C.[object_id]=D.parent_object_id
AND C.column_id=D.parent_column_id
AND C.default_object_id=D.[object_id]
LEFT JOIN sys.extended_properties PFD
ON PFD.class=1
AND C.[object_id]=PFD.major_id
AND C.column_id=PFD.minor_id
-- AND PFD.name='Caption' -- 字段說明對應(yīng)的描述名稱(一個字段可以添加多個不同name的描述)
LEFT JOIN sys.extended_properties PTB
ON PTB.class=1
AND PTB.minor_id=0
AND C.[object_id]=PTB.major_id
-- AND PFD.name='Caption' -- 表說明對應(yīng)的描述名稱(一個表可以添加多個不同name的描述)
LEFT JOIN -- 索引及主鍵信息
(
SELECT
IDXC.[object_id],
IDXC.column_id,
Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')
WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,
PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END,
IndexName=IDX.Name
FROM sys.indexes IDX
INNER JOIN sys.index_columns IDXC
ON IDX.[object_id]=IDXC.[object_id]
AND IDX.index_id=IDXC.index_id
LEFT JOIN sys.key_constraints KC
ON IDX.[object_id]=KC.[parent_object_id]
AND IDX.index_id=KC.unique_index_id
INNER JOIN -- 對于一個列包含多個索引的情況,只顯示第1個索引信息
(
SELECT [object_id], Column_id, index_id=MIN(index_id)
FROM sys.index_columns
GROUP BY [object_id], Column_id
) IDXCUQ
ON IDXC.[object_id]=IDXCUQ.[object_id]
AND IDXC.Column_id=IDXCUQ.Column_id
AND IDXC.index_id=IDXCUQ.index_id
) IDX
ON C.[object_id]=IDX.[object_id]
AND C.column_id=IDX.column_id
-- WHERE O.name=N'要查詢的表' -- 如果只查詢指定表,加上此條件
ORDER BY O.name,C.column_id
--★★SQL2005索引及主鍵信息 :★★★★★★★★★★★★★★★★★★
-- ========================================================================
-- 索引及主鍵信息
-- 鄒建 2005.08(引用請保留此信息)
-- ========================================================================
SELECT
TableId=O.[object_id],
TableName=O.Name,
IndexId=ISNULL(KC.[object_id],IDX.index_id),
IndexName=IDX.Name,
IndexType=ISNULL(KC.type_desc,'Index'),
Index_Column_id=IDXC.index_column_id,
ColumnID=C.Column_id,
ColumnName=C.Name,
Sort=CASE INDEXKEY_PROPERTY(IDXC.[object_id],IDXC.index_id,IDXC.index_column_id,'IsDescending')
WHEN 1 THEN 'DESC' WHEN 0 THEN 'ASC' ELSE '' END,
PrimaryKey=CASE WHEN IDX.is_primary_key=1 THEN N'√'ELSE N'' END,
[UQIQUE]=CASE WHEN IDX.is_unique=1 THEN N'√'ELSE N'' END,
Ignore_dup_key=CASE WHEN IDX.ignore_dup_key=1 THEN N'√'ELSE N'' END,
Disabled=CASE WHEN IDX.is_disabled=1 THEN N'√'ELSE N'' END,
Fill_factor=IDX.fill_factor,
Padded=CASE WHEN IDX.is_padded=1 THEN N'√'ELSE N'' END
FROM sys.indexes IDX
INNER JOIN sys.index_columns IDXC
ON IDX.[object_id]=IDXC.[object_id]
AND IDX.index_id=IDXC.index_id
LEFT JOIN sys.key_constraints KC
ON IDX.[object_id]=KC.[parent_object_id]
AND IDX.index_id=KC.unique_index_id
INNER JOIN sys.objects O
ON O.[object_id]=IDX.[object_id]
INNER JOIN sys.columns C
ON O.[object_id]=C.[object_id]
AND O.type='U'
AND O.is_ms_shipped=0
AND IDXC.Column_id=C.Column_id
--★★SQL Server對大容量內(nèi)存的支持:★★★★★★★★★★★★★★★★★★
/*
32位操作系統(tǒng)有個很大的缺陷,應(yīng)用程序無法訪問大于4G的進程地址空間,因為32位的指針無法保存大于4G的地址空間
如果大于4G,則需要使用地址窗口化擴展插件(AWE),具體操作如下:
1,啟動物理地址擴展
(1)找到C:\boot.ini,并刪除其只讀屬性.
(2)編輯boot.ini,在ARC路徑中添加/PAE參數(shù).例如:
在windows Server 2003 Enterprise Edition 中,編輯后的ARC路徑如下:
muti(0)disk(0)partition(1)windows="windows Server 2003 Enterprise,Edition"/fastdetect/PAE
保存后將其恢復(fù)為只讀模式,然后重新啟動計算機。
如果計算機上的可用物理內(nèi)存超過16G,應(yīng)確保boot.ini文件中沒有/3gb參數(shù)
*/
--★★如何啟動AWE選項:★★★★★★★★★★★★★★★★★★
sp_configure'show advanced options',1
reconfigure
go
sp_configue 'awe enabled',1
reconfigure
go
--★★手動配置內(nèi)存選項:★★★★★★★★★★★★★★★★★★
sp_configure'show advanced options',1
go
reconfigure
go
sp_configure 'min server memory' --服務(wù)器最小內(nèi)存
sp_configure 'max server memory' --服務(wù)器最大內(nèi)存
sp_configure 'index create memory'--創(chuàng)建索引占用的內(nèi)存
sp_configure 'min memory per query'--每次查詢占用的最小內(nèi)存
--★★獲取磁盤讀寫情況:★★★★★★★★★★★★★★★★★★
select
@@total_read as '讀取磁盤的次數(shù)',
@@total_write as '寫入磁盤的次數(shù)',
@@total_error as '磁盤寫入錯誤數(shù)',
getdate() as '當前時間'
--★★獲取數(shù)據(jù)庫文件的I/O統(tǒng)計信息:★★★★★★★★★★★★★★★★★★
select * from fn_virtualfilestats(null,null)
--★★獲取I/O工作情況:★★★★★★★★★★★★★★★★★★
select
@@id_busy,--SQL自上次啟動以來的用于執(zhí)行輸入和輸出操作的時間
@@timeticks, --每個時鐘周期對應(yīng)的微秒數(shù)
@@id_busy*@@timeticks as 'I/O 操作毫秒數(shù)',
getdate() as '當前時間'
--★★查看SQL SEVER CPU活動,工作情況:★★★★★★★★★★★★★★★★★★
select
@@cpu_busy,--自上次啟動以來的工作時間
@@timeticks, --每個時鐘周期對應(yīng)的微秒數(shù)
@@cpu_busy*cast(@@timeticks as float)/1000 as 'cpu工作時間(秒)',
@@idie*cast(@@timeticks as float)/1000 as 'CPU空閑時間(秒)'
getdate() as '當前時間'
--★★獲取網(wǎng)絡(luò)數(shù)據(jù)包統(tǒng)計信息:★★★★★★★★★★★★★★★★★★
select
getdate() as '當前時間',
@@pack_received as'輸入數(shù)據(jù)包數(shù)量',
@@pack_sent as '輸出數(shù)據(jù)包數(shù)量',
@@packet_error as '錯誤包數(shù)量'
--★★查看服務(wù)器工作狀態(tài):★★★★★★★★★★★★★★★★★★
create function fgetsstatus(
@servername varchar(50) --服務(wù)器名
,@userid varchar(50)='sa' --用戶名,如果為nt驗證方式,則為空
,@password varchar(50)='' --密碼
) returns varchar(20)
as
begin
declare @re varchar(20),@ire int --返回狀態(tài)
declare @srvid int --定義服務(wù)器、數(shù)據(jù)庫集id
declare @err int,@src varchar(255), @desc varchar(255) --錯誤處理變量
--★★創(chuàng)建sqldmo對象 :★★★★★★★★★★★★★★★★★★
exec @err=sp_oacreate 'sqldmo.sqlserver',@srvid output
if @err >0 goto lberr
--★★連接服務(wù)器 :★★★★★★★★★★★★★★★★★★
if isnull(@userid,'')='' --如果是 Nt驗證方式
begin
exec @err=sp_oasetproperty @srvid,'loginsecure',1
if @err >0 goto lberr
exec @err=sp_oamethod @srvid,'connect',null,@servername
end
else
exec @err=sp_oamethod @srvid,'connect',null,@servername,@userid,@password
if @err >0 goto lberr
--★★獲取服務(wù)器狀態(tài) :★★★★★★★★★★★★★★★★★★
exec @err=sp_oagetproperty @srvid,'Status',@ire output
if @err >0 goto lberr
set @re=case @ire when 0 then '未知'
when 1 then '運行...'
when 2 then '暫停'
when 3 then '停止...'
when 4 then '正在啟動...'
when 5 then '正在啟動停止...'
when 6 then '連接...'
when 7 then '正在暫停...' end
return(@re)
lberr:
exec sp_oageterrorinfo NULL, @src out, @desc out
declare @errb varbinary(4)
set @errb=cast(@err as varbinary(4))
exec master..xp_varbintohexstr @errb,@re out
set @re='錯誤號: '+@re
+char(13)+'錯誤源: '+@src
+char(13)+'錯誤描述: '+@desc
return(@re)
end
go
select dbo.fgetsstatus('192.168.102.208','sa','sa')
--------------------
--運行...
--★★獲取服務(wù)器狀態(tài) :★★★★★★★★★★★★★★★★★★
if object_id('tb')is not null drop table tb
go
create table tb(
表名 sysname,
記錄數(shù) int,
保留空間 nvarchar(10),
使用空間 varchar(10),
索引使用空間 varchar(10),
未用空間 varchar(10))
exec sp_MSForEachTable @command1=N'insert tb exec sp_spaceused ''?'''
select * from tb
--★★查看服務(wù)器版本 :★★★★★★★★★★★★★★★★★★
SELECT
SERVERPROPERTY('productversion'),
SERVERPROPERTY ('productlevel'),
SERVERPROPERTY ('edition')
--★★查看數(shù)據(jù)庫脫機時間 :★★★★★★★★★★★★★★★★★★
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
go
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE
GO
select a.name,a.database_id,a.create_date,b.physical_name into #a
from sys.databases a left join sys.master_files b on
a.database_id=b.database_id where has_dbaccess(a.name)>1 and b.type=1
create table #b(info varchar(500))
declare @string varchar(max)
set @string=''
select @string=@string+'insert into #b exec xp_cmdshell''dir '+ physical_name +''''+char(13)+char(10) from #a
execute(@string)
select a.name,substring(b.info,0,20) as 脫機時間,a.database_id,a.create_date,a.physical_name
from #a a left join #b b on
REVERSE(substring(REVERSE(physical_name),0,charindex('\',REVERSE(physical_name))))
=REVERSE(substring(REVERSE(info),0,charindex(' ',REVERSE(info))))
drop table #a,#b
go
EXEC sp_configure 'xp_cmdshell', 0
RECONFIGURE
go
EXEC sp_configure 'show advanced options', 0
RECONFIGURE
go
/*
1. 查看數(shù)據(jù)庫的版本
select @@version
2.查看數(shù)據(jù)庫所在機器操作系統(tǒng)參數(shù) .
exec master..xp_msver ..
3. 查看數(shù)據(jù)庫啟動的參數(shù) !
sp_configure
4.查看數(shù)據(jù)庫啟動時間 .
select convert(varchar(30),login_time,120) from master..sysprocesses where spid=1
查看數(shù)據(jù)庫服務(wù)器名和實例名 .
print ''Server Name...............: '' + convert(varchar(30),@@SERVERNAME) .
print ''Instance..................: '' + convert(varchar(30),@@SERVICENAME) ...
5. 查看所有數(shù)據(jù)庫名稱及大小
sp_helpdb 。
重命名數(shù)據(jù)庫用的SQL
sp_renamedb ''old_dbname'', ''new_dbname''
6. 查看所有數(shù)據(jù)庫用戶登錄信息 ..
sp_helplogins
查看所有數(shù)據(jù)庫用戶所屬的角色信息
sp_helpsrvrolemember !
修復(fù)遷移服務(wù)器時孤立用戶時,可以用的fix_orphan_user腳本或者LoneUser過程 .
更改某個數(shù)據(jù)對象的用戶屬主
sp_changeobjectowner [@objectname =] ''object'', [@newowner =] ''owner'' .
注意: 更改對象名的任一部分都可能破壞腳本和存儲過程。
把一臺服務(wù)器上的數(shù)據(jù)庫用戶登錄信息備份出來可以用add_login_to_aserver腳本
7. 查看鏈接服務(wù)器 ...
sp_helplinkedsrvlogin
查看遠端數(shù)據(jù)庫用戶登錄信息 。
sp_helpremotelogin ..
8.查看某數(shù)據(jù)庫下某個數(shù)據(jù)對象的大小 !
sp_spaceused @objname
還可以用sp_toptables過程看最大的N(默認為50)
*/
--★★查看作業(yè)執(zhí)行情況 :★★★★★★★★★★★★★★★★★★
select category = jc.name,
category_id = jc.category_id,
job_name = j.name,
job_enabled = j.enabled,
last_run_time = cast(js.last_run_date as varchar(10)) + '-' + cast(js.last_run_time as varchar(10)),
last_run_duration = js.last_run_duration,
last_run_status = js.last_run_outcome,
last_run_msg = js.last_outcome_message + cast(nullif(js.last_run_outcome,1) as varchar(2)),
job_created = j.date_created,
job_modified = j.date_modified
from msdb.dbo.sysjobs j
inner join msdb.dbo.sysjobservers js
on j.job_id = js.job_id
inner join msdb.dbo.syscategories jc
on j.category_id = jc.category_id
where j.enabled = 1
and js.last_run_outcome in (0,1,3,5) -- 0:Fail 1:Succ 3:Cancel 5:First run
and jc.category_id not between 10 and 20 -- repl
--★★查詢數(shù)據(jù)庫db中表tb的所有索引的隨片情況 :★★★★★★★★★★★★★★★★★★
use db
go
select
a.index_id,---索引編號
b.name,---索引名稱
avg_fragmentation_in_percent---索引的邏輯碎片
from
sys.dm_db_indx_physical_stats(db_id(),object_id(N'create.consume'),null,null,null) as a
join
sys.indexes as b
on
a.object_id=b.object_id
and
a.index_id=b.index_id
go
--★★用戶成員權(quán)限:★★★★★★★★★★★★★★★★★★
USE pubs
--創(chuàng)建角色 r_test
EXEC sp_addrole 'r_test'
--授予 r_test 對 jobs 表的所有權(quán)限
GRANT ALL ON jobs TO r_test
--授予角色 r_test 對 titles 表的 SELECT 權(quán)限
GRANT SELECT ON titles TO r_test
--添加登錄 l_test,設(shè)置密碼為pwd,默認數(shù)據(jù)庫為pubs
EXEC sp_addlogin 'l_test','pwd','pubs'
--為登錄 l_test 在數(shù)據(jù)庫 pubs 中添加安全賬戶 u_test
EXEC sp_grantdbaccess 'l_test','u_test'
--添加 u_test 為角色 r_test 的成員
EXEC sp_addrolemember 'r_test','u_test'
--拒絕安全賬戶 u_test 對 titles 表的 SELECT 權(quán)限
DENY SELECT ON titles TO u_test
/*--完成上述步驟后,用 l_test 登錄,可以對jobs表進行所有操作,但無法對titles表查詢,雖然角色 r_test 有titles表的select權(quán)限,但已經(jīng)在安全賬戶中明確拒絕了對titles的select權(quán)限,所以l_test無titles表的select權(quán)限--*/
--從數(shù)據(jù)庫 pubs 中刪除安全賬戶
EXEC sp_revokedbaccess 'u_test'
--刪除登錄 l_test
EXEC sp_droplogin 'l_test'
--刪除角色 r_test
EXEC sp_droprole 'r_test'
12下一頁閱讀全文