Microsoft SQL How To
| Microsoft SQL |
|---|
| Microsoft SQL Topics |
| How To · Troubleshooting |
[edit] How to run SQL Server Management Studio as an NT AUTHORITY\System user
Connected to the console aka main display of the remote server, either physically or remotely:
mstsc /admin /v:servername
Use the windows scheduler to start a new command prompt
at 11:10 /interactive c:\windows\system32\cmd.exe
Once the new shell pops up run the SQL Server Management studio:
"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe"
Or run the SQL Server Enterprise Manager:
C:\WINDOWS\system32\mmc.exe /s "C:\Program Files\Microsoft SQL Server\80\Tools\BINN\SQL Server Enterprise Manager.MSC"
[edit] How to monitor distributed transactions
To monitor and resolve MS DTC transaction log in onto the MS SQL server. Open Administrative Tools -> Component Services. Then expand Component Services -> Computers -> My Computer -> Distributed Transactions Coordinator -> Local DTC and click on the Transaction List.
[edit] How to list all indexes in a database
Run the following.
SELECT OBJECT_SCHEMA_NAME(tab.object_id,DB_ID()) AS "Schema", tab.name AS table_name, idx.name AS index_name, col.name AS column_name, idx.type_desc, idx.fill_factor,idxcol.is_descending_key FROM sys.TABLES AS tab INNER JOIN sys.indexes idx ON tab.object_id = idx.object_id INNER JOIN sys.index_columns idxcol ON idx.object_id = idxcol.object_id INNER JOIN sys.all_columns col ON tab.object_id = col.object_id AND idxcol.column_id = col.column_id WHERE tab.is_ms_shipped = 0 AND idx.type_desc <> 'HEAP' AND idx.is_primary_key = 0 AND idx.is_unique = 0 AND idx.is_unique_constraint = 0 ORDER BY tab.name, idx.index_id
Change to your liking.
[edit] How to check locks on a database
A generic lock query
sp_lock
To see what is a behind a specific process number
sp_who2
For a specific object number do
SELECT OBJECT_NAME(...)
For a specific db number do
SELECT DB_NAME(...)
To see what is in a query cache for a process do
DBCC INPUTBUFFER(<process_id here>)
A query from the syslockinfo
SELECT DISTINCT object_name(a.rsc_objid), a.req_spid, b.loginame FROM master.dbo.syslockinfo a (nolock) JOIN master.dbo.sysprocesses b (nolock) ON a.req_spid=b.spid WHERE object_name(a.rsc_objid) IS NOT NULL
The same query except smart - decoding all the values on the fly:
SELECT CONVERT (SMALLINT, l.req_spid) AS 'Process',COALESCE(SUBSTRING (s.loginame, 1, 128), '') AS 'Login' , COALESCE(SUBSTRING (s.hostname, 1, 128), '') AS 'Host', SUBSTRING (DB_NAME(l.rsc_dbid), 1, 128) AS 'DB' , OBJECT_NAME(CONVERT(VARCHAR, l.rsc_objid)) AS 'Table', idx.name AS 'Index' , SUBSTRING (v.name, 1, 4) AS 'Type', SUBSTRING (u.name, 1, 8) AS 'Mode', SUBSTRING (x.name, 1, 5) AS 'Status' FROM master.dbo.syslockinfo l JOIN master.dbo.spt_values v ON l.rsc_type = v.number JOIN master.dbo.spt_values x ON l.req_status = x.number JOIN master.dbo.spt_values u ON l.req_mode + 1 = u.number LEFT JOIN master.dbo.sysprocesses s ON l.req_spid = s.spid LEFT JOIN sys.indexes idx ON l.rsc_indid = idx.index_id AND l.rsc_objid = idx.object_id WHERE v.type = 'LR' AND x.type = 'LS' AND u.type = 'L'
Once you have the table and the index numbers from the previous entry you can find out what transaction caused the locks
SELECT * FROM sys.dm_tran_database_transactions t JOIN sys.syslockinfo s ON s.req_transactionid=t.transaction_id WHERE s.rsc_objid=<table_id here> AND s.rsc_indid=<index_id here>
Once you have the transaction number you could try to find it in the MS SQL transaction log. However it looks like what is used for the transaction id in the transaction log is not the same as the transaction_id' from sys.dm_tran_database_transactions. You could try getting the oldest transaction and then looking around that time to pick up the needed transaction id and using it to search as follows:
DBCC OPENTRAN(itimdb) SELECT top 100 * FROM master.dbo.fn_dblog('96190:8913:21',NULL) SELECT * FROM master.dbo.fn_dblog(NULL,NULL) WHERE [transaction id]='0000:18c48cd6'
[edit] How to get size and number of rows in each table in a database
CREATE TABLE #tmp (tablename varchar(128), rowcnt int, reserved varchar(10), DATA varchar(10), index_size varchar(10), unused varchar(10)) exec sp_MSforeachtable 'insert into #tmp EXEC sp_spaceused ''?'' ' SELECT tablename,rowcnt AS rows_count, CAST(REPLACE(reserved,' KB','') AS INT)/1024 size_in_mb FROM #tmp ORDER BY size_in_mb DESC DROP TABLE #tmp
[edit] How to insert binary data manually
insert into table1(datacolumn) values (convert(binary,'data'));
[edit] How to save content of an sql table from a query
select * into enrole.SCHEDULED_MESSAGE_BACKUP from enrole.SCHEDULED_MESSAGE
This command also creates a backup table (i.e. it should not exist) to copy the content back use this:
delete from enrole.SCHEDULED_MESSAGE insert into enrole.SCHEDULED_MESSAGE select * from enrole.SCHEDULED_MESSAGE_BACKUP
[edit] How to update indexes on SQL
EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?', ' ', 80)"
To check the index fragmentation run
EXEC sp_MSforeachtable @command1="print '?' DBCC SHOWCONTIG ('?')"
or use it on just one table
DBCC SHOWCONTIG ([enrole.PROCESS])
|
|||||