Hello all,The place I work at is finally getting around to upgrading from SQL 2005 to SQL 2012. Unfortunately, the legacy system I inherited has hundreds of objects with the deprecated RAISERROR syntax:RAISERROR {error number} {error message}I have scripted out a method to detect the objects (within a margin of error) that have this issue and will post it below, but the reason for this post is that I am looking for the path of least resistance to script out rollback scripts as a starting point for the team. We are starting down the path of powershell but the SMO doesn't seem to have a clean way to get to ALTERs. We can solve this with a replace, but it's just not as clean as we hoped. Furthermore, it looks like the USE [database] and ansi settings have to be concatenated in. Just wondering if anyone knows of a quicker method to take a list of objects and generate scripts. I was thinking there has to be an API that SSMS uses to generate scripts that could be tapped into?In any case, here's my RAISERROR detection script in case it helps anyone else. For those that don't know, MS says that upgrade advisor won't be fixed to include these these because the tool was not built to handle this type of pattern detection:https://connect.microsoft.com/SQLServer/feedback/details/694484/denali-ctp3-upgrade-advisor-misses-deprecated-raiserror-syntaxI am using the REGEX CLR function at http://www.codeproject.com/Articles/19502/A-T-SQL-Regular-Expression-Library-for-SQL-Server and there are some caveats, but it seems to work pretty good:Use Master;/*Currently, the RegEx CLR functions are installed on MB1 in MasterRegEx CLR can be found at http://www.codeproject.com/Articles/19502/A-T-SQL-Regular-Expression-Library-for-SQL-ServerNote that this will still find the RAISERROR pattern in a string provided there is no concatenation between elementsThe number of false positives or missed objects should be very low and able to be handled on a case by case basis*/GOIF OBJECT_ID('tempdb.dbo.#tblObjectInfo') IS NOT NULL DROP TABLE #tblObjectInfo;CREATE TABLE #tblObjectInfo ( DatabaseName SYSNAME, ObjectID INT, SchemaName SYSNAME, ObjectName SYSNAME, ObjectType CHAR(2), ObjectText NVARCHAR(MAX), PRIMARY KEY (DatabaseName,ObjectID) );DECLARE @SQL NVARCHAR(MAX);SET @SQL ='use ; INSERT #tblObjectInfo ( ObjectID, DatabaseName, SchemaName, ObjectName, ObjectType, ObjectText )SELECT o.Object_ID, DB_NAME() AS DatabaseName, s.name AS SchemaName, o.Name AS ObjectName, o.type AS ObjectType, --m.definition AS ObjectText -- Uncomment for raw sp deinitions -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- -- The below line will replace all COMMENTS in object definitions with empty string -- This allows our global search to avoid returning false positives -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- Master.dbo.ufn_RegExReplace(m.definition,''(--.*)|(((/\*)+?[\w\W]+?(\*/)+))'', '''', 1) AS ObjectTextFROM sys.all_sql_modules AS mJOIN sys.all_objects AS o ON m.Object_ID = o.Object_IDJOIN sys.schemas AS s ON o.schema_id = s.schema_idWHERE o.is_ms_shipped=0-------------------------------------------------------- custom filter------------------------------------------------------AND m.definition LIKE ''%RAISERROR%''AND (o.name NOT LIKE ''sp[_]%'' OR CONVERT(VARBINARY(MAX),o.name) <> CONVERT(VARBINARY(MAX),LOWER(o.name)));';EXEC dbo.sp_msforeachDB @SQL;SELECT t.DatabaseName, t.SchemaName, t.ObjectNameFROM #tblObjectInfo AS tWHERE Master.dbo.ufn_RegExIsMatch(t.ObjectText, '\bRAISERROR[\t\n ]+[@0-9]', 1) = 1ORDER BY t.DatabaseName, t.SchemaName, t.ObjectName;
Thanks in advance for any help.