[置顶] 分享:sp_get_object_denifiction_to_file 获取存储过程函数的定义语句并生成文件

USE [master]

GO

/****** Object:  StoredProcedure [dbo].[sp_get_object_denifiction_cmd]    Script Date: 05/07/2013 14:34:55 ******/

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF OBJECT_ID('[dbo].[sp_get_object_denifiction_to_file]') IS NOT NULL

DROP PROC [dbo].[sp_get_object_denifiction_to_file] 

GO 

--------------存储过程



CREATE   PROCEDURE [dbo].[sp_get_object_denifiction_to_file]

    @object NVARCHAR(128) = '' ,			--对象名

    @object_type NVARCHAR(2) = 'P',			--对象类型

	@file NVARCHAR(128) = 'ALL' ,			--获取文件方式,ALL一个文件全部将对象语句保存下来,SINGLE将对象分别保存到单独文件

	@file_type nvarchar(128) = '.sql',		--保存格式

    @path VARCHAR(255) = 'D:\DBGetObjectSQL'  --保存的文件夹路径

AS

/*

EXEC [dbo].[sp_get_object_denifiction_cmd] '','P','ALL','.sql','D:\DBGetObjectSQL'

EXEC [dbo].[sp_get_object_denifiction_cmd] '','P','SINGLE','.sql','D:\DBGetObjectSQL'

EXEC [dbo].[sp_get_object_denifiction_cmd] '','FN','SINGLE','.sql','D:\DBGetObjectSQL'



*/

IF @object_type NOT IN ('P','FN') 

BEGIN 

	SELECT ' @object_type 只能是P或FN ' return_result 

	RETURN 

END

IF UPPER(@file) NOT IN('ALL','SINGLE')

BEGIN 

	SELECT ' @file 只能是ALL或SINGLE' return_result 

	RETURN 

END

EXEC xp_create_subdir @path; --创建文件夹

DECLARE @newline CHAR(2) ,

		@bcp_sql VARCHAR(1000) 



SET  @newline = CHAR(13) + CHAR(10) ;

IF UPPER(@file) = 'ALL'

BEGIN

	IF OBJECT_ID('tempdb.dbo.tmp_get_object_denifiction_all') IS NULL

	CREATE TABLE tempdb.dbo.tmp_get_object_denifiction_all  ( definition NVARCHAR(MAX)) 



	INSERT INTO  tempdb.dbo.tmp_get_object_denifiction_all  ( definition )

	SELECT    'USE ['+DB_NAME() + ']' + @newline 

			+ 'GO' + @newline 

			+ 'IF OBJECT_ID(''' + name + ''',''' + @object_type + ''') IS NOT NULL ' +@newline

			+ 'DROP ' + CASE WHEN @object_type = 'P' THEN ' PROCEDURE ' ELSE ' FUNCTION '  END +'[' + name + ']' + @newline 

			+ 'GO' + @newline

			+ 'SET ANSI_NULLS ON ' + @newline 

			+ 'GO' + @newline

			+ 'SET QUOTED_IDENTIFIER ON ' + @newline 

			+ 'GO' + @newline 

			+ '/************** object: '  + a.name + '    script_datetime: ' + CONVERT(VARCHAR,GETDATE(),120) + '****************/'  + @newline

			+ definition + @newline 

			+ 'GO' + @newline 

			+ CASE WHEN a.is_ms_shipped = 1 THEN  ' EXEC sp_MS_marksystemobject ''[' + name + ']''' + @newline + 'GO' + @newline ELSE '' END  AS definition 

	FROM    sys.objects a

			JOIN sys.sql_modules b ON a.object_id = b.object_id

	WHERE   type = @object_type AND a.name LIKE @object + '%' AND b.definition IS NOT NULL ;

	--bcp tempdb.dbo.tmp_get_object_denifiction_all  out c:\currency1.txt -c -T --使用信任连接

	SET @bcp_sql = 'bcp  tempdb.dbo.tmp_get_object_denifiction_all out ' + @path + '\' + db_name() + '.' + @object_type + '.ALL_OBJECT'+ @file_type +' -c -T'

	EXEC xp_cmdshell @bcp_sql;

	IF OBJECT_ID('tempdb.dbo.tmp_get_object_denifiction_all') IS NOT NULL 

	DROP TABLE tempdb.dbo.tmp_get_object_denifiction_all ;



END



IF UPPER(@file) = 'SINGLE'

BEGIN



	IF object_id('tempdb.dbo.tmp_get_object_denifiction_single') IS NULL 

	CREATE TABLE tempdb.dbo.tmp_get_object_denifiction_single ( definition NVARCHAR(MAX))

	

	DECLARE @object_denifiction NVARCHAR(MAX)

	DECLARE C CURSOR FAST_FORWARD FOR    

	SELECT    'USE ['+DB_NAME() + ']' + @newline 

			+ 'GO' + @newline 

			+ 'IF OBJECT_ID(''' + name + ''',''' + @object_type + ''') IS NOT NULL ' +@newline

			+ 'DROP ' + CASE WHEN @object_type = 'P' THEN ' PROCEDURE ' ELSE ' FUNCTION '  END +'[' + name + ']' + @newline 

			+ 'GO' + @newline

			+ 'SET ANSI_NULLS ON ' + @newline 

			+ 'GO' + @newline

			+ 'SET QUOTED_IDENTIFIER ON ' + @newline 

			+ 'GO' + @newline 

			+ definition + @newline 

			+ 'GO' + @newline 

			+ CASE WHEN a.is_ms_shipped = 1 THEN  'EXEC sp_MS_marksystemobject ''[' + name + ']''' + @newline + 'GO' + @newline ELSE '' END ,

			a.name 

	FROM    sys.objects a

			JOIN sys.sql_modules b ON a.object_id = b.object_id

	WHERE   type = @object_type AND a.name LIKE @object + '%' AND b.definition IS NOT NULL 



	OPEN C 

	FETCH NEXT FROM c INTO @object_denifiction,@object 

	WHILE @@FETCH_STATUS = 0

	BEGIN 

		INSERT INTO tempdb.dbo.tmp_get_object_denifiction_single ( definition )

		SELECT @object_denifiction 

		SET @bcp_sql = 'bcp  tempdb.dbo.tmp_get_object_denifiction_single out ' + @path + '\' + db_name() + '.' + @object_type + '.' + @object + @file_type +' -c -T'

		EXEC xp_cmdshell @bcp_sql;

		TRUNCATE TABLE tempdb.dbo.tmp_get_object_denifiction_single ;

		FETCH NEXT FROM c INTO @object_denifiction,@object 

	END 

	CLOSE C;

	DEALLOCATE C;

	IF OBJECT_ID( 'tempdb.dbo.tmp_get_object_denifiction_single' ) IS NOT NULL 

	DROP TABLE tempdb.dbo.tmp_get_object_denifiction_single ;





END 

GO

EXEC sp_MS_marksystemobject [sp_get_object_denifiction_to_file]



你可能感兴趣的:(object)