30.06.2013 Views

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SQL Server Team-based Development - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9: Searching DDL and Build Scripts<br />

USE MASTER<br />

SET ANSI_NULLS ON<br />

GO<br />

SET QUOTED_IDENTIFIER ON<br />

GO<br />

IF EXISTS (SELECT 1 FROM sys.objects WHERE name LIKE 'sp_ScriptFor')<br />

DROP PROCEDURE sp_ScriptFor<br />

go<br />

CREATE PROCEDURE [dbo].[sp_ScriptFor]<br />

@Identifier NVARCHAR(776)<br />

/**<br />

summary: ><br />

This procedure returns an object build script as a single row, single column<br />

result. Unlike the built-in OBJECT_DEFINITION, it also does tables.<br />

It copies the SMO style where possible but it uses the more intuitive<br />

way of representing referential constraints and includes the documentation<br />

as comments that was, for unknown reasons, left out by Microsoft.<br />

You call it with the name of the table, either as a string, a valid table name,<br />

or as a schema-qualified table name in a string.<br />

Author: Phil Factor<br />

Revision: 1.1 dealt properly with heaps<br />

date: 20 Apr 2010<br />

example:<br />

– code: sp_ScriptFor 'production.TransactionHistory'<br />

example:<br />

– code: sp_ScriptFor 'HumanResources.vEmployee'<br />

example:<br />

– code: execute phone..sp_ScriptFor 'holidays'<br />

example:<br />

– code: execute AdventureWorks..sp_ScriptFor TransactionHistory<br />

example:<br />

– code: sp_ScriptFor 'HumanResources.uspUpdateEmployeeHireInfo'<br />

returns: ><br />

single row, single column result Build_Script.<br />

**/<br />

--sp_helptext sp_help 'jobcandidate'<br />

AS<br />

DECLARE @Script VARCHAR(MAX)<br />

DECLARE @dbname SYSNAME<br />

DECLARE @PrimaryKeyBuild VARCHAR(MAX)<br />

IF CHARINDEX ('.',@identifier)=0<br />

SELECT @Identifier=QUOTENAME(Object_Schema_name(s.object_id))<br />

+'.'+QUOTENAME(s.name)<br />

FROM sys.objects s WHERE s.name LIKE @identifier<br />

288

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!