Wednesday, June 27, 2007

MS SQL

Some SQL queries that I find useful

Reset 'sa' password from query analyzer

Sp_password @new = 'mynewpassword', @loginame = 'sa'


Search for a string in stored_procedures

SELECT ROUTINE_NAME, ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%STRING_TO_SEARCH%'
AND ROUTINE_TYPE='PROCEDURE'

Get name of the store proceture containting the specified string
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE [text] LIKE '%STRING_TO_SEARCH%'
AND OBJECTPROPERTY(id, 'IsProcedure') = 1
GROUP BY OBJECT_NAME(id)


When you restore a database backup to another server, if you experience a problem with orphaned users refer to http://support.microsoft.com/kb/274188/