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

本文介绍了一个SQL Server的存储过程,用于根据指定的对象名称和类型导出SQL定义脚本到文件。支持导出所有匹配对象到单一文件或每个对象到独立文件。
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]


Consolidate compiler generated dependencies of target cpp_node [ 50%] Building CXX object CMakeFiles/cpp_node.dir/src/cpp_node.cpp.o /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp: In function ‘int main(int, char**)’: /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:6:47: error: expected ‘,’ or ‘;’ before ‘{’ token 6 | auto node = std::make_shared<rclcpp::Node>{"cpp_node"}; | ^ In file included from /opt/ros/humble/include/rclcpp/rclcpp/client.hpp:40, from /opt/ros/humble/include/rclcpp/rclcpp/callback_group.hpp:24, from /opt/ros/humble/include/rclcpp/rclcpp/any_executable.hpp:20, from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategy.hpp:25, from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategies.hpp:18, from /opt/ros/humble/include/rclcpp/rclcpp/executor_options.hpp:20, from /opt/ros/humble/include/rclcpp/rclcpp/executor.hpp:37, from /opt/ros/humble/include/rclcpp/rclcpp/executors/multi_threaded_executor.hpp:25, from /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:21, from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:155, from /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:1: /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:7:23: error: request for member ‘get_logger’ in ‘* node’, which is of non-class type ‘std::shared_ptr<rclcpp::Node>()’ 7 | RCLCPP_INFO(node->get_logger(),"你好C++节点!"); | ^~~~~~~~~~ /opt/ros/humble/include/rclcpp/rclcpp/logging.hpp:517:40: note: in definition of macro ‘RCLCPP_INFO’ 517 | ::std::is_convertible_v<decltype(logger), ::rclcpp::Logger>, \ | ^~~~~~ /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:7:23: error: request for member ‘get_logger’ in ‘* node’, which is of non-class type ‘std::shared_ptr<rclcpp::Node>()’ 7 | RCLCPP_INFO(node->get_logger(),"你好C++节点!"); | ^~~~~~~~~~ /opt/ros/humble/include/rclcpp/rclcpp/logging.hpp:517:40: note: in definition of macro ‘RCLCPP_INFO’ 517 | ::std::is_convertible_v<decltype(logger), ::rclcpp::Logger>, \ | ^~~~~~ /opt/ros/humble/include/rclcpp/rclcpp/logging.hpp:517:14: error: template argument 1 is invalid 517 | ::std::is_convertible_v<decltype(logger), ::rclcpp::Logger>, \ | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:7:5: note: in expansion of macro ‘RCLCPP_INFO’ 7 | RCLCPP_INFO(node->get_logger(),"你好C++节点!"); | ^~~~~~~~~~~ In file included from /opt/ros/humble/include/rclcpp/rclcpp/logging.hpp:24, from /opt/ros/humble/include/rclcpp/rclcpp/client.hpp:40, from /opt/ros/humble/include/rclcpp/rclcpp/callback_group.hpp:24, from /opt/ros/humble/include/rclcpp/rclcpp/any_executable.hpp:20, from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategy.hpp:25, from /opt/ros/humble/include/rclcpp/rclcpp/memory_strategies.hpp:18, from /opt/ros/humble/include/rclcpp/rclcpp/executor_options.hpp:20, from /opt/ros/humble/include/rclcpp/rclcpp/executor.hpp:37, from /opt/ros/humble/include/rclcpp/rclcpp/executors/multi_threaded_executor.hpp:25, from /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:21, from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:155, from /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:1: /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:7:23: error: request for member ‘get_logger’ in ‘* node’, which is of non-class type ‘std::shared_ptr<rclcpp::Node>()’ 7 | RCLCPP_INFO(node->get_logger(),"你好C++节点!"); | ^~~~~~~~~~ /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:7:5: note: in expansion of macro ‘RCLCPP_INFO’ 7 | RCLCPP_INFO(node->get_logger(),"你好C++节点!"); | ^~~~~~~~~~~ /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:7:23: error: request for member ‘get_logger’ in ‘* node’, which is of non-class type ‘std::shared_ptr<rclcpp::Node>()’ 7 | RCLCPP_INFO(node->get_logger(),"你好C++节点!"); | ^~~~~~~~~~ /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:7:5: note: in expansion of macro ‘RCLCPP_INFO’ 7 | RCLCPP_INFO(node->get_logger(),"你好C++节点!"); | ^~~~~~~~~~~ /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:8:17: error: no matching function for call tospin(std::shared_ptr<rclcpp::Node> (*&)())’ 8 | rclcpp::spin(node); | ~~~~~~~~~~~~^~~~~~ In file included from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:155, from /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:1: /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:45:1: note: candidate: ‘void rclcpp::spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr)’ 45 | spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr); | ^~~~ /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:45:60: note: no known conversion for argument 1 from ‘std::shared_ptr<rclcpp::Node> (*)()’ to ‘rclcpp::node_interfaces::NodeBaseInterface::SharedPtr’ {aka ‘std::shared_ptr<rclcpp::node_interfaces::NodeBaseInterface>’} 45 | spin(rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_ptr); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~ /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:49:1: note: candidate: ‘void rclcpp::spin(rclcpp::Node::SharedPtr)’ 49 | spin(rclcpp::Node::SharedPtr node_ptr); | ^~~~ /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:49:30: note: no known conversion for argument 1 from ‘std::shared_ptr<rclcpp::Node> (*)()’ to ‘rclcpp::Node::SharedPtr’ {aka ‘std::shared_ptr<rclcpp::Node>’} 49 | spin(rclcpp::Node::SharedPtr node_ptr); | ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~ In file included from /usr/include/x86_64-linux-gnu/c++/11/bits/c++allocator.h:33, from /usr/include/c++/11/bits/allocator.h:46, from /usr/include/c++/11/memory:64, from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:153, from /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:1: /usr/include/c++/11/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = rclcpp::Node; _Args = {}; _Tp = rclcpp::Node]’: /usr/include/c++/11/bits/alloc_traits.h:516:17: required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = rclcpp::Node; _Args = {}; _Tp = rclcpp::Node; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<rclcpp::Node>]’ /usr/include/c++/11/bits/shared_ptr_base.h:519:39: required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {}; _Tp = rclcpp::Node; _Alloc = std::allocator<rclcpp::Node>; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ /usr/include/c++/11/bits/shared_ptr_base.h:650:16: required from ‘std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = rclcpp::Node; _Alloc = std::allocator<rclcpp::Node>; _Args = {}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ /usr/include/c++/11/bits/shared_ptr_base.h:1342:14: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<rclcpp::Node>; _Args = {}; _Tp = rclcpp::Node; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]’ /usr/include/c++/11/bits/shared_ptr.h:409:59: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::allocator<rclcpp::Node>; _Args = {}; _Tp = rclcpp::Node]’ /usr/include/c++/11/bits/shared_ptr.h:862:14: required from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = rclcpp::Node; _Alloc = std::allocator<rclcpp::Node>; _Args = {}]’ /usr/include/c++/11/bits/shared_ptr.h:878:39: required from ‘std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = rclcpp::Node; _Args = {}]’ /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:6:22: required from here /usr/include/c++/11/ext/new_allocator.h:162:11: error: no matching function for call to ‘rclcpp::Node::Node()’ 162 | { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); } | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /opt/ros/humble/include/rclcpp/rclcpp/executors/single_threaded_executor.hpp:28, from /opt/ros/humble/include/rclcpp/rclcpp/executors.hpp:22, from /opt/ros/humble/include/rclcpp/rclcpp/rclcpp.hpp:155, from /home/cyt/chapt2/demo_cpp_pkg/src/cpp_node.cpp:1: /opt/ros/humble/include/rclcpp/rclcpp/node.hpp:1294:3: note: candidate: ‘rclcpp::Node::Node(const rclcpp::Node&, const string&)’ 1294 | Node( | ^~~~ /opt/ros/humble/include/rclcpp/rclcpp/node.hpp:1294:3: note: candidate expects 2 arguments, 0 provided /opt/ros/humble/include/rclcpp/rclcpp/node.hpp:101:12: note: candidate: ‘rclcpp::Node::Node(const string&, const string&, const rclcpp::NodeOptions&)’ 101 | explicit Node( | ^~~~ /opt/ros/humble/include/rclcpp/rclcpp/node.hpp:101:12: note: candidate expects 3 arguments, 0 provided /opt/ros/humble/include/rclcpp/rclcpp/node.hpp:89:12: note: candidate: ‘rclcpp::Node::Node(const string&, const rclcpp::NodeOptions&)’ 89 | explicit Node( | ^~~~ /opt/ros/humble/include/rclcpp/rclcpp/node.hpp:89:12: note: candidate expects 2 arguments, 0 provided make[2]: *** [CMakeFiles/cpp_node.dir/build.make:76:CMakeFiles/cpp_node.dir/src/cpp_node.cpp.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:137:CMakeFiles/cpp_node.dir/all] 错误 2 make: *** [Makefile:146:all] 错误 2
最新发布
09-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值