一些有用的T-sql语句(一)1. 查看数据库的版本     
一些有用的T-sql语句(一)   
select @@version 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   常见的几种SQL SERVER打补丁后的版本号: 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)    
8.00.194   Microsoft SQL Server 2000  
一些有用的T-sql语句(一)    
8.00.384   Microsoft SQL Server 2000 SP1  
一些有用的T-sql语句(一)    
8.00.532   Microsoft SQL Server 2000 SP2  
一些有用的T-sql语句(一)    
8.00.760   Microsoft SQL Server 2000 SP3  
一些有用的T-sql语句(一)    
8.00.818   Microsoft SQL Server 2000 SP3 w/ Cumulative Patch MS03-031  
一些有用的T-sql语句(一)    
8.00.2039  Microsoft SQL Server 2000 SP4   
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)
2. 查看数据库所在机器操作系统参数     
一些有用的T-sql语句(一)   
exec master..xp_msver 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)
3. 查看数据库启动的参数         
一些有用的T-sql语句(一)   sp_configure 
一些有用的T-sql语句(一)         
一些有用的T-sql语句(一)
4. 查看数据库启动时间         
一些有用的T-sql语句(一)   
select convert(varchar(30),login_time,120from master..sysprocesses where spid=1 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   查看数据库服务器名和实例名 
一些有用的T-sql语句(一)   
print 'Server Name一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)' + convert(varchar(30),@@SERVERNAME)         
一些有用的T-sql语句(一)   
print 'Instance一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)' + convert(varchar(30),@@SERVICENAME)       
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
5. 查看所有数据库名称及大小 
一些有用的T-sql语句(一)   sp_helpdb 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   重命名数据库用的SQL 
一些有用的T-sql语句(一)   sp_renamedb 
'old_dbname''new_dbname' 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)
6. 查看所有数据库用户登录信息 
一些有用的T-sql语句(一)   sp_helplogins 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   查看所有数据库用户所属的角色信息     
一些有用的T-sql语句(一)   sp_helpsrvrolemember 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   修复迁移服务器时孤立用户时,可以用的fix_orphan_user脚本或者LoneUser过程 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   更改某个数据对象的用户属主 
一些有用的T-sql语句(一)   sp_changeobjectowner 
[@objectname =] 'object'[@newowner =] 'owner' 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   注意: 更改对象名的任一部分都可能破坏脚本和存储过程。 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   把一台服务器上的数据库用户登录信息备份出来可以用add_login_to_aserver脚本 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   查看某数据库下,对象级用户权限 
一些有用的T-sql语句(一)   sp_helprotect 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)
7. 查看链接服务器         
一些有用的T-sql语句(一)   sp_helplinkedsrvlogin 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   查看远端数据库用户登录信息     
一些有用的T-sql语句(一)   sp_helpremotelogin 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)
8.查看某数据库下某个数据对象的大小 
一些有用的T-sql语句(一)   sp_spaceused 
@objname 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)   还可以用sp_toptables过程看最大的N(默认为50)个表 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)   查看某数据库下某个数据对象的索引信息 
一些有用的T-sql语句(一)   sp_helpindex 
@objname 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   还可以用SP_NChelpindex过程查看更详细的索引情况 
一些有用的T-sql语句(一)   SP_NChelpindex 
@objname 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   clustered索引是把记录按物理顺序排列的,索引占的空间比较少。  
一些有用的T-sql语句(一)   对键值DML操作十分频繁的表我建议用非clustered索引和约束,fillfactor参数都用默认值。 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)   查看某数据库下某个数据对象的的约束信息 
一些有用的T-sql语句(一)   sp_helpconstraint 
@objname 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)
9.查看数据库里所有的存储过程和函数 
一些有用的T-sql语句(一)   
use @database_name 
一些有用的T-sql语句(一)   sp_stored_procedures 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)   查看存储过程和函数的源代码 
一些有用的T-sql语句(一)   sp_helptext 
'@procedure_name' 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   查看包含某个字符串
@str的数据对象名称 
一些有用的T-sql语句(一)   
select distinct object_name(id) from syscomments where text like '%@str%' 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)   创建加密的存储过程或函数在AS前面加WITH ENCRYPTION参数 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)   解密加密过的存储过程和函数可以用sp_decrypt过程 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)       
10.查看数据库里用户和进程的信息 
一些有用的T-sql语句(一)   sp_who 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)  查看SQL Server数据库里的活动用户和进程的信息 
一些有用的T-sql语句(一)   sp_who 
'active' 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)  查看SQL Server数据库里的锁的情况 
一些有用的T-sql语句(一)   sp_lock 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   进程号1
--50是SQL Server系统内部用的,进程号大于50的才是用户的连接进程. 
一些有用的T-sql语句(一)

一些有用的T-sql语句(一)   spid是进程编号,dbid是数据库编号,objid是数据对象编号 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)   查看进程正在执行的SQL语句 
一些有用的T-sql语句(一)   
dbcc inputbuffer () 
一些有用的T-sql语句(一)            
一些有用的T-sql语句(一)  推荐大家用经过改进后的sp_who3过程可以直接看到进程运行的SQL语句 
一些有用的T-sql语句(一)   sp_who3 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)  检查死锁用sp_who_lock过程 
一些有用的T-sql语句(一)   sp_who_lock     
一些有用的T-sql语句(一)        
一些有用的T-sql语句(一)       
11.查看和收缩数据库日志文件的方法 
一些有用的T-sql语句(一)        
一些有用的T-sql语句(一)          查看所有数据库日志文件大小           
一些有用的T-sql语句(一)          
dbcc sqlperf(logspace) 
一些有用的T-sql语句(一)  
一些有用的T-sql语句(一)          如果某些日志文件较大,收缩简单恢复模式数据库日志,收缩后
@database_name_log的大小单位为M 
一些有用的T-sql语句(一)   
backup log @database_name with no_log 
一些有用的T-sql语句(一)   
dbcc shrinkfile (@database_name_log5
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)       
12.分析SQL Server SQL 语句的方法: 
一些有用的T-sql语句(一)        
一些有用的T-sql语句(一)   
set statistics time {on | off
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)   
set statistics io {on | off
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)           图形方式显示查询执行计划 
一些有用的T-sql语句(一)            
一些有用的T-sql语句(一)           在查询分析器
->查询->显示估计的评估计划(D)-Ctrl-L 或者点击工具栏里的图形 
一些有用的T-sql语句(一)            
一些有用的T-sql语句(一)           文本方式显示查询执行计划 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)   
set showplan_all {on | off
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)   
set showplan_text { on | off } 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)   
set statistics profile { on | off } 
一些有用的T-sql语句(一)    
一些有用的T-sql语句(一)  
一些有用的T-sql语句(一)       
13.出现不一致错误时,NT事件查看器里出3624号错误,修复数据库的方法 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)  先注释掉应用程序里引用的出现不一致性错误的表,然后在备份或其它机器上先恢复然后做修复操作 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)  
alter database [@error_database_name] set single_user 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)  修复出现不一致错误的表 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)  
dbcc checktable('@error_table_name',repair_allow_data_loss) 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)  或者可惜选择修复出现不一致错误的小型数据库名 
一些有用的T-sql语句(一)   
一些有用的T-sql语句(一)  
dbcc checkdb('@error_database_name',repair_allow_data_loss) 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)  
alter database [@error_database_name] set multi_user 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)  CHECKDB 有3个参数: 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)  repair_allow_data_loss 包括对行和页进行分配和取消分配以改正分配错误、结构行或页的错误, 
一些有用的T-sql语句(一)  以及删除已损坏的文本对象,这些修复可能会导致一些数据丢失。 
一些有用的T-sql语句(一)  修复操作可以在用户事务下完成以允许用户回滚所做的更改。 
一些有用的T-sql语句(一)  如果回滚修复,则数据库仍会含有错误,应该从备份进行恢复。 
一些有用的T-sql语句(一)  如果由于所提供修复等级的缘故遗漏某个错误的修复,则将遗漏任何取决于该修复的修复。 
一些有用的T-sql语句(一)  修复完成后,请备份数据库。  
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)  repair_fast 进行小的、不耗时的修复操作,如修复非聚集索引中的附加键。 
一些有用的T-sql语句(一)  这些修复可以很快完成,并且不会有丢失数据的危险。  
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)  repair_rebuild 执行由 repair_fast 完成的所有修复,包括需要较长时间的修复(如重建索引)。 
一些有用的T-sql语句(一)  执行这些修复时不会有丢失数据的危险。  
一些有用的T-sql语句(一) MS-SQL数据库开发常用汇总 1.按姓氏笔画排序:
一些有用的T-sql语句(一)
Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as 
一些有用的T-sql语句(一)
2.数据库加密:
一些有用的T-sql语句(一)
select encrypt('原始密码')
一些有用的T-sql语句(一)
select pwdencrypt('原始密码')
一些有用的T-sql语句(一)
select pwdcompare('原始密码','加密后密码'= 1--相同;否则不相同 encrypt('原始密码')
一些有用的T-sql语句(一)
select pwdencrypt('原始密码')
一些有用的T-sql语句(一)
select pwdcompare('原始密码','加密后密码'= 1--相同;否则不相同
一些有用的T-sql语句(一)
3.取回表中字段:
一些有用的T-sql语句(一)
declare @list varchar(1000),@sql nvarchar(1000
一些有用的T-sql语句(一)
select @list=@list+','+b.name from sysobjects a,syscolumns b where a.id=b.id and a.name='表A'
一些有用的T-sql语句(一)
set @sql='select '+right(@list,len(@list)-1)+' from 表A' 
一些有用的T-sql语句(一)
exec (@sql)
一些有用的T-sql语句(一)
4.查看硬盘分区:
一些有用的T-sql语句(一)
EXEC master..xp_fixeddrives
一些有用的T-sql语句(一)
5.比较A,B表是否相等:
一些有用的T-sql语句(一)
if (select checksum_agg(binary_checksum(*)) from A)
一些有用的T-sql语句(一)
=
一些有用的T-sql语句(一)(
select checksum_agg(binary_checksum(*)) from B)
一些有用的T-sql语句(一)
print '相等'
一些有用的T-sql语句(一)
else
一些有用的T-sql语句(一)
print '不相等'
一些有用的T-sql语句(一)
6.杀掉所有的事件探察器进程:
一些有用的T-sql语句(一)
DECLARE hcforeach CURSOR GLOBAL FOR SELECT 'kill '+RTRIM(spid) FROM master.dbo.sysprocesses
一些有用的T-sql语句(一)
WHERE program_name IN('SQL profiler',N'SQL 事件探查器')
一些有用的T-sql语句(一)
EXEC sp_msforeach_worker '?'
一些有用的T-sql语句(一)
7.记录搜索:
一些有用的T-sql语句(一)开头到N条记录
一些有用的T-sql语句(一)
Select Top N * From 表
一些有用的T-sql语句(一)
-------------------------------
一些有用的T-sql语句(一)
N到M条记录(要有主索引ID)
一些有用的T-sql语句(一)
Select Top M-* From 表 Where ID in (Select Top M ID From 表) Order by ID Desc
一些有用的T-sql语句(一)
----------------------------------
一些有用的T-sql语句(一)
N到结尾记录
一些有用的T-sql语句(一)
Select Top N * From 表 Order by ID Desc
一些有用的T-sql语句(一)
8.如何修改数据库的名称:
一些有用的T-sql语句(一)sp_renamedb 
'old_name''new_name' 
一些有用的T-sql语句(一)
9:获取当前数据库中的所有用户表
一些有用的T-sql语句(一)
select Name from sysobjects where xtype='u' and status>=0
一些有用的T-sql语句(一)
10:获取某一个表的所有字段
一些有用的T-sql语句(一)
select name from syscolumns where id=object_id('表名')
一些有用的T-sql语句(一)
11:查看与某一个表相关的视图、存储过程、函数
一些有用的T-sql语句(一)
select a.* from sysobjects a, syscomments b where a.id = b.id and b.text like '%表名%'
一些有用的T-sql语句(一)
12:查看当前数据库中所有存储过程
一些有用的T-sql语句(一)
select name as 存储过程名称 from sysobjects where xtype='P'
一些有用的T-sql语句(一)
13:查询用户创建的所有数据库
一些有用的T-sql语句(一)
select * from master..sysdatabases D where sid not in(select sid from master..syslogins where name='sa')
一些有用的T-sql语句(一)或者
一些有用的T-sql语句(一)
select dbid, name AS DB_NAME from master..sysdatabases where sid <> 0x01
一些有用的T-sql语句(一)
14:查询某一个表的字段和数据类型
一些有用的T-sql语句(一)
select column_name,data_type from information_schema.columns
一些有用的T-sql语句(一)
where table_name = '表名' 
一些有用的T-sql语句(一)
[n].[标题]:
一些有用的T-sql语句(一)
Select * From TableName Order By CustomerName 
一些有用的T-sql语句(一)
[n].[标题]:
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)快速获取表test的记录总数 : 
一些有用的T-sql语句(一)
select rows from sysindexes where id = object_id('test'and indid in (0,1)
一些有用的T-sql语句(一)提取数据库内所有表的字段详细说明的SQL语句 :
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
SELECT 
一些有用的T-sql语句(一)(
case when a.colorder=1 then d.name else '' end) N'表名'
一些有用的T-sql语句(一)a.colorder N
'字段序号'
一些有用的T-sql语句(一)a.name N
'字段名'
一些有用的T-sql语句(一)(
case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then ''else '' 
一些有用的T-sql语句(一)
end) N'标识'
一些有用的T-sql语句(一)(
case when (SELECT count(*
一些有用的T-sql语句(一)
FROM sysobjects 
一些有用的T-sql语句(一)
WHERE (name in 
一些有用的T-sql语句(一)(
SELECT name 
一些有用的T-sql语句(一)
FROM sysindexes 
一些有用的T-sql语句(一)
WHERE (id = a.id) AND (indid in 
一些有用的T-sql语句(一)(
SELECT indid 
一些有用的T-sql语句(一)
FROM sysindexkeys 
一些有用的T-sql语句(一)
WHERE (id = a.id) AND (colid in 
一些有用的T-sql语句(一)(
SELECT colid 
一些有用的T-sql语句(一)
FROM syscolumns 
一些有用的T-sql语句(一)
WHERE (id = a.id) AND (name = a.name))))))) AND 
一些有用的T-sql语句(一)(xtype 
= 'PK'))>0 then '' else '' end) N'主键'
一些有用的T-sql语句(一)b.name N
'类型'
一些有用的T-sql语句(一)a.length N
'占用字节数'
一些有用的T-sql语句(一)
COLUMNPROPERTY(a.id,a.name,'PRECISION'as N'长度'
一些有用的T-sql语句(一)
isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0as N'小数位数'
一些有用的T-sql语句(一)(
case when a.isnullable=1 then ''else '' end) N'允许空'
一些有用的T-sql语句(一)
isnull(e.text,'') N'默认值'
一些有用的T-sql语句(一)
isnull(g.[value],''AS N'字段说明' 
一些有用的T-sql语句(一)
FROM syscolumns a 
一些有用的T-sql语句(一)
left join systypes b 
一些有用的T-sql语句(一)
on a.xtype=b.xusertype 
一些有用的T-sql语句(一)
inner join sysobjects d 
一些有用的T-sql语句(一)
on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' 
一些有用的T-sql语句(一)
left join syscomments e 
一些有用的T-sql语句(一)
on a.cdefault=e.id 
一些有用的T-sql语句(一)
left join sysproperties g 
一些有用的T-sql语句(一)
on a.id=g.id AND a.colid = g.smallid 
一些有用的T-sql语句(一)
order by object_name(a.id),a.colorder
一些有用的T-sql语句(一)获取表结构
[把 'sysobjects' 替换 成 'tablename' 即可] 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
SELECT CASE IsNull(I.name, ''
一些有用的T-sql语句(一)
When '' Then '' 
一些有用的T-sql语句(一)
Else '*' 
一些有用的T-sql语句(一)
End as IsPK, 
一些有用的T-sql语句(一)
Object_Name(A.id) as t_name, 
一些有用的T-sql语句(一)A.name 
as c_name, 
一些有用的T-sql语句(一)
IsNull(SubString(M.text1254), ''as pbc_init, 
一些有用的T-sql语句(一)T.name 
as F_DataType, 
一些有用的T-sql语句(一)
CASE IsNull(TYPEPROPERTY(T.name, 'Scale'), ''
一些有用的T-sql语句(一)
WHEN '' Then Cast(A.prec as varchar
一些有用的T-sql语句(一)
ELSE Cast(A.prec as varchar+ ',' + Cast(A.scale as varchar
一些有用的T-sql语句(一)
END as F_Scale, 
一些有用的T-sql语句(一)A.isnullable 
as F_isNullAble 
一些有用的T-sql语句(一)
FROM Syscolumns as A 
一些有用的T-sql语句(一)
JOIN Systypes as T 
一些有用的T-sql语句(一)
ON (A.xType = T.xUserType AND A.Id = Object_id('sysobjects') ) 
一些有用的T-sql语句(一)
LEFT JOIN ( SysIndexes as I 
一些有用的T-sql语句(一)
JOIN Syscolumns as A1 
一些有用的T-sql语句(一)
ON ( I.id = A1.id and A1.id = object_id('sysobjects'and (I.status & 0x800= 0x800 AND A1.colid <= I.keycnt) ) 
一些有用的T-sql语句(一)
ON ( A.id = I.id AND A.name = index_col('sysobjects', I.indid, A1.colid) ) 
一些有用的T-sql语句(一)
LEFT JOIN SysComments as M 
一些有用的T-sql语句(一)
ON ( M.id = A.cdefault and ObjectProperty(A.cdefault, 'IsConstraint'= 1 ) 
一些有用的T-sql语句(一)
ORDER BY A.Colid ASC
一些有用的T-sql语句(一)四种方法取表里n到m条纪录:
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
1.
一些有用的T-sql语句(一)
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入
一些有用的T-sql语句(一)
set rowcount n
一些有用的T-sql语句(一)
select * from 表变量 order by columnname desc
一些有用的T-sql语句(一)
2.
一些有用的T-sql语句(一)
select top n * from (select top m * from tablename order by columnname) a order by columnname desc
一些有用的T-sql语句(一)
3.如果tablename里没有其他identity列,那么:
一些有用的T-sql语句(一)
select identity(int) id0,* into #temp from tablename
一些有用的T-sql语句(一)取n到m条的语句为:
一些有用的T-sql语句(一)
select * from #temp where id0 >=and id0 <= m
一些有用的T-sql语句(一)如果你在执行 
select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
一些有用的T-sql语句(一)
exec sp_dboption 你的DB名字,'select into/bulkcopy',true
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
4.如果表里有identity属性,那么简单:
一些有用的T-sql语句(一)
select * from tablename where identitycol between n and m 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
--------------------------------------------------------------------------------
一些有用的T-sql语句(一)

一些有用的T-sql语句(一)数据库管理常用SQL
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
1. 查看数据库的版本 
一些有用的T-sql语句(一)
select @@version 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
2. 查看数据库所在机器操作系统参数 
一些有用的T-sql语句(一)
exec master..xp_msver 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
3. 查看数据库启动的参数 
一些有用的T-sql语句(一)sp_configure 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
4. 查看数据库启动时间 
一些有用的T-sql语句(一)
select convert(varchar(30),login_time,120from master..sysprocesses where spid=1 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)查看数据库服务器名和实例名 
一些有用的T-sql语句(一)
print 'Server Name一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)' + convert(varchar(30),@@SERVERNAME
一些有用的T-sql语句(一)
print 'Instance一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)一些有用的T-sql语句(一)' + convert(varchar(30),@@SERVICENAME
一些有用的T-sql语句(一)
5. 查看所有数据库名称及大小 
一些有用的T-sql语句(一)sp_helpdb 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)重命名数据库用的SQL 
一些有用的T-sql语句(一)sp_renamedb 
'old_dbname''new_dbname' 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
6. 查看所有数据库用户登录信息 
一些有用的T-sql语句(一)sp_helplogins 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)查看所有数据库用户所属的角色信息 
一些有用的T-sql语句(一)sp_helpsrvrolemember 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)修复迁移服务器时孤立用户时,可以用的fix_orphan_user脚本或者LoneUser过程 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)更改某个数据对象的用户属主 
一些有用的T-sql语句(一)sp_changeobjectowner 
[@objectname =] 'object'[@newowner =] 'owner' 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)注意: 更改对象名的任一部分都可能破坏脚本和存储过程。 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)把一台服务器上的数据库用户登录信息备份出来可以用add_login_to_aserver脚本 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
7. 查看链接服务器 
一些有用的T-sql语句(一)sp_helplinkedsrvlogin 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)查看远端数据库用户登录信息 
一些有用的T-sql语句(一)sp_helpremotelogin 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
8.查看某数据库下某个数据对象的大小 
一些有用的T-sql语句(一)sp_spaceused 
@objname 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)还可以用sp_toptables过程看最大的N(默认为50)个表 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)查看某数据库下某个数据对象的索引信息 
一些有用的T-sql语句(一)sp_helpindex 
@objname 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)还可以用SP_NChelpindex过程查看更详细的索引情况 
一些有用的T-sql语句(一)SP_NChelpindex 
@objname 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)clustered索引是把记录按物理顺序排列的,索引占的空间比较少。 
一些有用的T-sql语句(一)对键值DML操作十分频繁的表我建议用非clustered索引和约束,fillfactor参数都用默认值。 
一些有用的T-sql语句(一)查看某数据库下某个数据对象的的约束信息 
一些有用的T-sql语句(一)sp_helpconstraint 
@objname 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
9.查看数据库里所有的存储过程和函数 
一些有用的T-sql语句(一)
use @database_name 
一些有用的T-sql语句(一)sp_stored_procedures 
一些有用的T-sql语句(一)查看存储过程和函数的源代码 
一些有用的T-sql语句(一)sp_helptext 
'@procedure_name' 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)查看包含某个字符串
@str的数据对象名称 
一些有用的T-sql语句(一)
select distinct object_name(id) from syscomments where text like '%@str%' 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)创建加密的存储过程或函数在AS前面加WITH ENCRYPTION参数 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)解密加密过的存储过程和函数可以用sp_decrypt过程 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
10.查看数据库里用户和进程的信息 
一些有用的T-sql语句(一)sp_who 
一些有用的T-sql语句(一)查看SQL Server数据库里的活动用户和进程的信息 
一些有用的T-sql语句(一)sp_who 
'active' 
一些有用的T-sql语句(一)查看SQL Server数据库里的锁的情况 
一些有用的T-sql语句(一)sp_lock 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)进程号1
--50是SQL Server系统内部用的,进程号大于50的才是用户的连接进程. 
一些有用的T-sql语句(一)
spid是进程编号,dbid是数据库编号,objid是数据对象编号 
一些有用的T-sql语句(一)查看进程正在执行的SQL语句 
一些有用的T-sql语句(一)
dbcc inputbuffer () 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)推荐大家用经过改进后的sp_who3过程可以直接看到进程运行的SQL语句 
一些有用的T-sql语句(一)sp_who3 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)检查死锁用sp_who_lock过程 
一些有用的T-sql语句(一)sp_who_lock 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
11.收缩数据库日志文件的方法 
一些有用的T-sql语句(一)收缩简单恢复模式数据库日志,收缩后
@database_name_log的大小单位为M 
一些有用的T-sql语句(一)
backup log @database_name with no_log 
一些有用的T-sql语句(一)
dbcc shrinkfile (@database_name_log5
一些有用的T-sql语句(一)
12.分析SQL Server SQL 语句的方法: 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
set statistics time {on | off
一些有用的T-sql语句(一)
set statistics io {on | off
一些有用的T-sql语句(一)图形方式显示查询执行计划 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)在查询分析器
->查询->显示估计的评估计划(D)-Ctrl-L 或者点击工具栏里的图形 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)文本方式显示查询执行计划 
一些有用的T-sql语句(一)
set showplan_all {on | off
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
set showplan_text { on | off } 
一些有用的T-sql语句(一)
set statistics profile { on | off } 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
13.出现不一致错误时,NT事件查看器里出3624号错误,修复数据库的方法 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)先注释掉应用程序里引用的出现不一致性错误的表,然后在备份或其它机器上先恢复然后做修复操作 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
alter database [@error_database_name] set single_user 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)修复出现不一致错误的表 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
dbcc checktable('@error_table_name',repair_allow_data_loss) 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)或者可惜选择修复出现不一致错误的小型数据库名 
一些有用的T-sql语句(一)
一些有用的T-sql语句(一)
dbcc checkdb('@error_database_name',repair_allow_data_loss) 
一些有用的T-sql语句(一)
alter database [@error_database_name] set multi_user 
一些有用的T-sql语句(一)CHECKDB 有3个参数: 
一些有用的T-sql语句(一)repair_allow_data_loss 包括对行和页进行分配和取消分配以改正分配错误、结构行或页的错误, 
一些有用的T-sql语句(一)以及删除已损坏的文本对象,这些修复可能会导致一些数据丢失。 
一些有用的T-sql语句(一)修复操作可以在用户事务下完成以允许用户回滚所做的更改。 
一些有用的T-sql语句(一)如果回滚修复,则数据库仍会含有错误,应该从备份进行恢复。 
一些有用的T-sql语句(一)如果由于所提供修复等级的缘故遗漏某个错误的修复,则将遗漏任何取决于该修复的修复。 
一些有用的T-sql语句(一)修复完成后,请备份数据库。 
一些有用的T-sql语句(一)repair_fast 进行小的、不耗时的修复操作,如修复非聚集索引中的附加键。 
一些有用的T-sql语句(一)这些修复可以很快完成,并且不会有丢失数据的危险。 
一些有用的T-sql语句(一)repair_rebuild 执行由 repair_fast 完成的所有修复,包括需要较长时间的修复(如重建索引)。 
一些有用的T-sql语句(一)执行这些修复时不会有丢失数据的危险。 
一些有用的T-sql语句(一)

相关文章: