SQL Server 2008
Goal: To generate scripts for creating sprocs that are already in the DB. Must be one script per file.
I know I can simply right click the database and 'Tasks>Generate Scripts', but that doesn't script the sproc in the template I want.
I need the sproc to be scripted in the same template you get when you right click the sproc from the object explorer and 'Script Stored Procedure As>DROP and CREATE'.
It's true you get a similar version of this through 'Tasks>Generate Scripts' but the main difference is the 'Tasks>Generate Scripts' method creates the script via the dbo.sp_executesql
command because you cannot nest a CREATE PROCEDURE
inside of an IF
block
Tasks>Generate produces this:
USE someDB
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'somesproc') AND type in (N'P', N'PC'))
DROP PROCEDURE someSproc
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'someSproc') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
CREATE PROCEDURE
AS
BEGIN
END
'
END
GO
But I require this (as found from right clicking the sproc):
USE someDB
GO
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'someSproc') AND type in (N'P', N'PC'))
DROP PROCEDURE someSproc
GO
USE someDB
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE someSproc
AS
BEGIN
SET NOCOUNT ON;
END
GO
Any ideas?
USE someDB
between each statement? I assume it's ok to ignore permissions and dependency order, that kind of thing? – Aaron Bertrand Aug 26 '11 at 16:57USE someDB
. And yes, those two can be ignored. – Justin Self Aug 26 '11 at 17:58