SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[adminLoadScriptsGrande]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[adminLoadScriptsGrande]
GO


create procedure dbo.adminLoadScriptsGrande 
	@pServerName varchar(60),
	@pDatabaseName varchar(30)
as
/****************************************************************************
Loads all scripts found within a local working folder hierarchy for the 
specified server and database into that database on the local system. 
Notes: Both the local server and the specified server must be member servers
in the project that owns the database and both servers must already be added
to the local dbChangeControl metadata hierarchy (and file system). 
*****************************************************************************/
declare @returnstatus int,
	@error int,
	@procedureName varchar(100)

set nocount on

set @procedureName = db_name() + '.' 
		+ user_name(objectproperty(@@procid,'OwnerId')) 
		+ '.' + object_name(@@procid)

-- roles
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'DatabaseRoles'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- users
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Users'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- user data types
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'UserDefinedDatatypes'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- defaults
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Defaults'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- rules
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Rules'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- tables
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Tables'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- primary keys
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Tables\PrimaryKeys'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- unique keys
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Tables\UniqueKeys'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- dri defaults
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Tables\Defaults'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- checks
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Tables\Checks'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- triggers
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Tables\Triggers'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- indexes
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Tables\Indexes'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- foreign keys
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Tables\ForeignKeys'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- views
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Views'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- user defined functions
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'UserDefinedFunctions'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- stored procedures
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'StoredProcedures'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- views
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'Views'
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

-- permissions
exec @returnstatus = Admin.dbo.adminLoadScripts @pServerName, 
						@pDatabaseName, 
						'????'

set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

return

ErrorHandler:
insert Admin.dbo.ActivityLog (activity)
select @procedureName + ' ' + @pServerName 
	+ ' ' + @pDatabaseName 
	+ ' failed with returnstatus: ' 
	+ cast(@returnstatus as varchar(10)) 
	+ ' error: ' + cast(@error as varchar(10)) 

raiserror(59001,16,1,@procedureName)

return -1


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

