What is the point of “Initial Catalog” in a SQL Server connection string?

If the user name that is in the connection string has access to more then one database you have to specify the database you want the connection string to connect to. If your user has only one database available then you are correct that it doesn’t matter. But it is good practice to put this … Read more

Declare Variable for a Query String

It’s possible, but it requires using dynamic SQL. I recommend reading The curse and blessings of dynamic SQL before continuing… DECLARE @theDate varchar(60) SET @theDate=””‘2010-01-01” AND ”2010-08-31 23:59:59”’ DECLARE @SQL VARCHAR(MAX) SET @SQL = ‘SELECT AdministratorCode, SUM(Total) as theTotal, SUM(WOD.Quantity) as theQty, AVG(Total) as avgTotal, (SELECT SUM(tblWOD.Amount) FROM tblWOD JOIN tblWO on tblWOD.OrderID = tblWO.ID … Read more

Find stored procedure by name

You can use: select * from sys.procedures where name like ‘%name_of_proc%’ if you need the code you can look in the syscomments table select text from syscomments c inner join sys.procedures p on p.object_id = c.object_id where p.name like ‘%name_of_proc%’ Edit Update: you can can also use the ansi standard version SELECT * FROM INFORMATION_SCHEMA.ROUTINES … Read more

How to query for Xml values and attributes from table in SQL Server?

Actually you’re close to your goal, you just need to use nodes() method to split your rows and then get values: select s.SqmId, m.c.value(‘@id’, ‘varchar(max)’) as id, m.c.value(‘@type’, ‘varchar(max)’) as type, m.c.value(‘@unit’, ‘varchar(max)’) as unit, m.c.value(‘@sum’, ‘varchar(max)’) as [sum], m.c.value(‘@count’, ‘varchar(max)’) as [count], m.c.value(‘@minValue’, ‘varchar(max)’) as minValue, m.c.value(‘@maxValue’, ‘varchar(max)’) as maxValue, m.c.value(‘.’, ‘nvarchar(max)’) as Value, … Read more

Unable to begin a distributed transaction

Found it, MSDTC on the remote server was a clone of the local server. From the Windows Application Events Log: Event Type: Error Event Source: MSDTC Event Category: CM Event ID: 4101 Date: 9/19/2011 Time: 1:32:59 PM User: N/A Computer: ASITESTSERVER Description: The local MS DTC detected that the MS DTC on ASICMSTEST has the … Read more

How to determine total number of open/active connections in ms sql server 2005

This shows the number of connections per each DB: SELECT DB_NAME(dbid) as DBName, COUNT(dbid) as NumberOfConnections, loginame as LoginName FROM sys.sysprocesses WHERE dbid > 0 GROUP BY dbid, loginame And this gives the total: SELECT COUNT(dbid) as TotalConnections FROM sys.sysprocesses WHERE dbid > 0 If you need more detail, run: sp_who2 ‘Active’ Note: The SQL … Read more

How can you tell what Tables are taking up the most space in a SQL Server 2005 Database?

Try this script – it will list the number of rows and the space used by data rows (and the total space used) for all tables in your database: SELECT t.NAME AS TableName, i.name AS indexName, SUM(p.rows) AS RowCounts, SUM(a.total_pages) AS TotalPages, SUM(a.used_pages) AS UsedPages, SUM(a.data_pages) AS DataPages, (SUM(a.total_pages) * 8) / 1024 AS TotalSpaceMB, … Read more