Every now and then I generate SQL scripts for setting up test databases or for deployment purposes. Typically I use the "Publish to provider" command that is available in Visual Studio. That often serves med well, but it has one feature that I don't like about it: if I choose to script only Stored Procedures, it will generate a script that will also drop and create all tables that these procedures talk to (which I find a bit on the dangerous side). Furthermore, it will not do this in a certain order (such as drop all tables, drop all procedures, create tables, create procedures), but mix the handling of different object types through the file. This makes it rather hard to clean the file by hand. This is a sample of what they look like:
/****** Object: ForeignKey [FK_SomeKey] Script Date: 03/25/2010 17:25:14 ******/
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_SomeKey]') AND parent_object_id = OBJECT_ID(N'[dbo].[SomeTable]'))
ALTER TABLE [dbo].[SomeTable] DROP CONSTRAINT [FK_SomeKey]
GO
/****** Object: ForeignKey [FK_SomeOtherKey] Script Date: 03/25/2010 17:25:14 ******/
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_SomeOtherKey]') AND parent_object_id = OBJECT_ID(N'[dbo].[SomeOtherTable]'))
ALTER TABLE [dbo].[SomeOtherTable] DROP CONSTRAINT [FK_SomeOtherKey]
GO
/****** Object: StoredProcedure [dbo].[SomeProcedure] Script Date: 03/25/2010 17:25:15 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SomeProcedure]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SomeProcedure]
GO
/****** Object: View [dbo].[SomeView] Script Date: 03/25/2010 17:25:15 ******/
IF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[SomeView]'))
DROP VIEW [dbo].[SomeView]
GO
/****** Object: StoredProcedure [dbo].[SomeOtherProcedure] Script Date: 03/25/2010 17:25:15 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SomeOtherProcedure]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SomeOtherProcedure]
GO
/****** Object: UserDefinedFunction [dbo].[SomeFunction] Script Date: 03/25/2010 17:25:15 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SomeFunction]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[SomeFunction]
GO
So, say we want to clean the script so that only stuff that is related to stored procedures remain. If the file is big, that is a lot of editing to do. Luckily, the Visual Studio editor supports regular expressions, and this is one of those occasions where regular expressions are extremely useful. Unfortunately, the regex syntax is somewhat special in Visual Studio, but it is still usable. The following expression will locate all script blocks that are not related to stored procedures:
/\*+ Object\::b:b~(StoredProcedure)(.*\n)#GO\n
So, by using that regex and replacing all matches with an empty string, only blocks related to stored procedures will remain.