Philip Howe’s SQL Server Blog

Not just another WordPress.com weblog

Detecting Deadlocks

Posted by philiphowe on June 17, 2009

To capture deadlock info, run the following through SQL Profiler.

  1. Create a new trace, using a blank template
  2. Add the Locks:Deadlock graph event from the Locks category.  An additional tab appears on the Trace Properties window, named Event Extraction Settings.
  3. Save the Deadlock XML Events Separately by selecting the check box.  The deadlock information will be written to a separate file.

Posted in Administration | Tagged: , , | Leave a Comment »

Explain Plan

Posted by philiphowe on March 26, 2009

To show the text version of an explain plan, instead of that horrible GUI version, use the following command.  However, be aware that this will then ONLY show the execution plan and not the actual rows the query returns:

SET showplan_text ON

SET showplan_text OFF

Posted in SQL | Tagged: , | Leave a Comment »

Column Names for a Table via SQL

Posted by philiphowe on March 19, 2009

 

To get a long list of columns for use in an insert statement:

DECLARE @c varchar(4000), @t varchar(128)
SET @c = ''
SET @t='myTable'

SELECT @c = @c + c.name + ', '
 FROM syscolumns c INNER JOIN sysobjects o ON o.id = c.id
 WHERE o.name = @t
 ORDER BY colid
SELECT @c

Which gives the following result:

field1, field2, field3,

Posted in SQL | Leave a Comment »

Total records in each table

Posted by philiphowe on March 19, 2009

To find total records in all user tables: 

CREATE TABLE #TableRecordCount(
     TableName char(100),
     TableCount int )

DECLARE table_cursor CURSOR for
     select name
     from sysobjects
     where type=’U’

DECLARE @table_name sysname
DECLARE @num_recs int
DECLARE @count_sql as nvarchar(128)
DECLARE @total_recs int

SET @total_recs = 0

OPEN table_cursor
FETCH next FROM table_cursor INTO @table_name
WHILE @@FETCH_STATUS=0
BEGIN
     set @count_sql = ’select @num_recs = count(*) from ‘ + @table_name
     exec sp_executesql @count_sql,N’@num_recs int output’,@num_recs OUTPUT
     insert into #TableRecordCount select @table_name, @num_recs
     set @total_recs = @total_recs + @num_recs
     fetch next from table_cursor into @table_name
END
CLOSE table_cursor
DEALLOCATE table_cursor

insert into #TableRecordCount select ‘===Total===’, @total_recs

select *
from #TableRecordCount
order by 2 desc

DROP TABLE #TableRecordCount

 

(This was started from the code at http://www.cryer.co.uk/brian/sqlserver/howto_count_total_records.htm).

Posted in SQL | Tagged: , , | Leave a Comment »

Create a Temporary Table

Posted by philiphowe on March 19, 2009

Temporary tables can be local to your seeion or global.

A local temporary table is identified by a single hash (#) at the start of the name. A local temporary table is visible only to the user who created it and is destroyed automatically when that user disconnects.

A global temporary table is denoted by a name starting with two hashes (##). A global temporary table is visible to all users and is deleted automatically when the last user who has referenced the table disconnects.

An example of creating a local temporary table:

create table #myLocalTemp
(
    Field1    int,
    Field2    varchar(20)
)

An example of creating a global temporary table:

create table ##myGlobalTemp
(
    Field1    int,
    Field2    varchar(20)
)

Posted in Uncategorized | Tagged: , , , | Leave a Comment »