SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

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


create procedure dbo.dmoScriptDatabaseGrande
	@pDatabaseName varchar(255),
	@pTempFolder varchar(255) = null,
        @pInstanceName varchar(30) = null  
as
/**********************************************************************************
Wraper to script all database objects and all table objects in a database
***********************************************************************************/
-- common
declare @returnstatus int,
	@error int,
	@procedureName varchar(100)

set nocount on

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

exec @returnstatus = Admin.dbo.dmoScriptDatabase @pDatabaseName, 
						@pTempFolder, 
						@pInstanceName

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

exec @returnstatus = Admin.dbo.dmoScriptTables @pDatabaseName, 
						@pTempFolder, 
						@pInstanceName
set @error = @@error
if @error <> 0 or @returnstatus <> 0
	goto ErrorHandler

return

ErrorHandler:
insert Admin.dbo.ActivityLog (activity)
select @procedureName + ' ' + @pDatabaseName 
	+ ' ' + isNull(@pTempFolder,'') 
	+ ' ' + isNull(@pInstanceName,'') 
	+ ' 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

