Quick tips or notes that probably reflects 20 percent of knowledge that usually does 80 percent of job.
Monday, July 30, 2012
Finding out last running queries
select * from sys.sysprocesses p cross apply sys.dm_exec_sql_text(p.sql_handle) t
Friday, July 27, 2012
Find permissions of current user
In SQL Server 2005 or above, to find current user's permissions granted.
SELECT
dp.NAME AS principal_name,
dp.type_desc AS principal_type_desc,
o.NAME AS object_name,
p.permission_name,
p.state_desc AS permission_state_desc
from
sys.database_principals dp
join
sys.database_permissions p
on
p.grantee_principal_id = dp.principal_id
left join
sys.all_objects o
on
p.major_id = o.OBJECT_ID
--where dp.name=CURRENT_USER
order by principal_name
Note: powerful user can view other users permissions.
SELECT
dp.NAME AS principal_name,
dp.type_desc AS principal_type_desc,
o.NAME AS object_name,
p.permission_name,
p.state_desc AS permission_state_desc
from
sys.database_principals dp
join
sys.database_permissions p
on
p.grantee_principal_id = dp.principal_id
left join
sys.all_objects o
on
p.major_id = o.OBJECT_ID
--where dp.name=CURRENT_USER
order by principal_name
Note: powerful user can view other users permissions.
Monday, July 23, 2012
View blocking, SQL Server 2005 and above
with blocked
as
(
--first, the processes being blocked
select * from sys.sysprocesses where blocked>0
--second, blocked processes being blocked further
union all
select p.* from sys.sysprocesses p
inner join blocked b
on p.spid=b.blocked
)
select getdate() checkTime,b.*,t.text into #blockChain from blocked b cross apply sys.dm_exec_sql_text (b.sql_handle) t;
select getdate() checkTime,l.* from sys.dm_tran_locks l join #blockChain b on l.request_session_id=b.spid
where blocked=0
drop table #blockChain
--use dbcc inputbuffer (spid) to check individual spid's last execution.
--sp_lock spid
Another query copied from the Net:
SELECT L.request_session_id AS SPID,
DB_NAME(L.resource_database_id) AS DatabaseName,
O.Name AS LockedObjectName,
P.object_id AS LockedObjectId,
L.resource_type AS LockedResource,
L.request_mode AS LockType,
ST.text AS SqlStatementText,
ES.login_name AS LoginName,
ES.host_name AS HostName,
TST.is_user_transaction as IsUserTransaction,
AT.name as TransactionName,
CN.auth_scheme as AuthenticationMethod
FROM sys.dm_tran_locks L
JOIN sys.partitions P ON P.hobt_id = L.resource_associated_entity_id
JOIN sys.objects O ON O.object_id = P.object_id
JOIN sys.dm_exec_sessions ES ON ES.session_id = L.request_session_id
JOIN sys.dm_tran_session_transactions TST ON ES.session_id = TST.session_id
JOIN sys.dm_tran_active_transactions AT ON TST.transaction_id = AT.transaction_id
JOIN sys.dm_exec_connections CN ON CN.session_id = ES.session_id
CROSS APPLY sys.dm_exec_sql_text(CN.most_recent_sql_handle) AS ST
WHERE resource_database_id = db_id()
and L.request_session_id in ()
ORDER BY L.request_session_id
Thursday, July 12, 2012
Move user databases
SQL server 2000:
1.backup database and restore with MOVE satement.
2. detach and attach
sp_detach_db 'db'
--move db files to target locations
EXEC sp_attach_db @dbname = N'db',
@filename1 = N'G:\database\db.mdf',
@filename2 = N'G:\database\log.LDF'
SQL 2005 or above:
besides the way used for SQL server 2000, 'alter database' can also be used for this purpose.
alter database db set offline;
go
--Move the file or files to the new location.
alter database db modify file (name='db',filename='g:\database\db\db.mdf')
go
alter database db modify file (name='db_log',filename='g:\database\db\db_log.LDF')
go
alter database db set online;
go
1.backup database and restore with MOVE satement.
2. detach and attach
sp_detach_db 'db'
--move db files to target locations
EXEC sp_attach_db @dbname = N'db',
@filename1 = N'G:\database\db.mdf',
@filename2 = N'G:\database\log.LDF'
SQL 2005 or above:
besides the way used for SQL server 2000, 'alter database' can also be used for this purpose.
alter database db set offline;
go
--Move the file or files to the new location.
alter database db modify file (name='db',filename='g:\database\db\db.mdf')
go
alter database db modify file (name='db_log',filename='g:\database\db\db_log.LDF')
go
alter database db set online;
go
Wednesday, July 11, 2012
Move tempdb to another hard drive
USE master
GO
alter database tempdb modify file(name='tempdev',filename='E:\Database\tempdb\tempdb.mdf')
go
alter database tempdb modify file(name='templog',filename='E:\Database\tempdb\templog.ldf')
go
GO
alter database tempdb modify file(name='tempdev',filename='E:\Database\tempdb\tempdb.mdf')
go
alter database tempdb modify file(name='templog',filename='E:\Database\tempdb\templog.ldf')
go
Restart sql server to make the change, and delete old files.
Tuesday, July 10, 2012
Dimensional model
The dimensional model is often implemented on top of the relational model using a star schema,
consisting of one highly normalized table containing the facts, and
surrounding denormalized tables containing each dimension.
An alternative physical implementation, called a snowflake schema, normalizes multi-level hierarchies within a dimension into multiple tables.
An alternative physical implementation, called a snowflake schema, normalizes multi-level hierarchies within a dimension into multiple tables.
Statement used most recently
All SQL 2000 related.
SQL Trace:
sp_trace_create
sp_trace_setevent
sp_trace_setfilter
exec sp_trace_setstatus 1, 0
exec sp_trace_setstatus 1, 2
SELECT * FROM ::fn_trace_getinfo(default)
Check table size and index usage:
sp_spaceused(tblname)
dbcc showcontig() with tableresults
(2005:sys.dm_db_index_physical_stats)
dbcc show_statistics('ppcprogressivemsgarchive','PK_PPCProgressiveMsgArchive')
Check resouce:
dbcc traceon(3604)
dbcc page(7,1,232323) for 7:1:232323
Processes:
select * from master..processes
sp_who2
dbcc inputbuffer(spid)
SQL Trace:
sp_trace_create
sp_trace_setevent
sp_trace_setfilter
exec sp_trace_setstatus 1, 0
exec sp_trace_setstatus 1, 2
SELECT * FROM ::fn_trace_getinfo(default)
Check table size and index usage:
sp_spaceused(tblname)
dbcc showcontig() with tableresults
(2005:sys.dm_db_index_physical_stats)
dbcc show_statistics('ppcprogressivemsgarchive','PK_PPCProgressiveMsgArchive')
Check resouce:
dbcc traceon(3604)
dbcc page(7,1,232323) for 7:1:232323
Processes:
select * from master..processes
sp_who2
dbcc inputbuffer(spid)
Subscribe to:
Posts (Atom)