将mater库中的系统存储过程批量生成*.sql文件 通用且非常实用
最近因为系统需要,需要将master库的所有和业务相关的存储过程批量生成 存储过程名.sql 文件,默认是不可以导出的
大家都知道系统存储过程是无法用工具导出的(大家可以试试 >任务>生成SQL脚本)因为系统存储过程一般是不让开发人员修改的。
需要知识:
1、xp_cmdshell命令的使用
2、sp_MS_marksystemobject 标记系统存储过程的方法
3、dos 命令,如 type,>> 等
4、bcp 命令的使用
代码如下:
use master
go
if OBJECT_ID('pr_procToSql') is not null drop proc pr_procToSql
go
create proc pr_procToSql
(
@服务器名 varchar(100)
,@用户名 varchar(100)
,@密码 varchar(100)
,@path varchar(200)
,@database varchar(200)
,@sysproc int='0' --是否标记为系统函数 1:是,0:否
,@proc_name varchar(100)='' --默认是所有,可以模糊搜索
,@savetype varchar(200)='.sql' --默认保存为sql脚本
)
as
/*
版本:v1
作者:达摩
日期:2012-04-13
功能:
1\将master库的系统存储过程批量生成文件(系统存储过程无法自动导出)
2\可以将所有类型的存储过程导出
3\可以标记上系统存储过程
调用:
exec pr_procToSql '.','sa','H4ymH@$RTd','e:\tom\master\','master','1',‘'
exec pr_procToSql '.','sa','a123456','e:\sql\','agt_trad','','pr_','.sql'
*/
set nocount on
declare @sp nvarchar(500),@s nvarchar(2000),@row int,@id int,@s_add varchar(2000)
set @s=' use '+@database
exec(@s)
if object_id('tempdb..#t') is not null drop table tempdb..#t
create table tempdb..#t(name varchar(2000)
, id int IDENTITY(1,1) not null
)
exec('
insert into tempdb..#t(name)
select name
--into TEMPDB..#T
from '+@database+'..sysobjects where xtype=''p'' and name like '''+@proc_name+'%''
')
select @row=COUNT(*) from tempdb..#t
print '共生成['+cast(@row as varchar)+']个存储过程'
set @id=1
while @row>=@id
begin
select top 1 @sp=name from tempdb..#T where id=@id
if OBJECT_ID('tempdb..test') is not null drop table tempdb..test
--增加use master go
set @s_add='echo use ['+@database+']>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
set @s_add='echo GO>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
set @s_add='echo IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].['+@sp+']'') AND type in (N''P'', N''PC''))>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
set @s_add='echo DROP PROCEDURE [dbo].['+@sp+']>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
set @s_add='echo GO>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
set @s_add='echo SET ANSI_NULLS ON>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
set @s_add='echo GO>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
set @s_add='echo SET QUOTED_IDENTIFIER ON>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
set @s_add='echo GO>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
select @s='
select text into tempdb..test
from '+@database+'..syscomments
where id=OBJECT_ID('''+@database+'..'+@sp+''')
'
exec(@s)
--select * from tempdb..test
select @s='exec xp_cmdshell '+'''bcp tempdb..test out '+@path+@sp+cast(@id as varchar)+@savetype+' -c -S '+@服务器名+' -U '+@用户名+' -P '+@密码+''''
exec(@s)
--将前面加上use master 信息追加到 最前面
set @s_add='type '+@path+@sp+CAST(@id as varchar)+@savetype+'>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
set @s_add='echo GO>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
if @sysproc='1'
begin
--在最后面加上标记为系统存储过程
set @s_add='echo exec sp_MS_marksystemobject ''['+@sp+']''>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
set @s_add='echo GO>>'+@path+@sp+@savetype
exec xp_cmdshell @s_add
print '标记第['+cast(@id as varchar)+']个为系统存储过程:'+@sp
end
set @s_add='del '+@path+@sp+CAST(@id as varchar)+@savetype
exec xp_cmdshell @s_add
print '生成第['+cast(@id as varchar)+']个存储过程:'+@sp
delete from tempdb..#T where id=@id
set @id=@id+1
end
此存储过程可以完善的功能
1、生成视图
2、生成函数
3、生成指定库的表结构
4、生成指定库的约束,用于批量生成升级脚本
5、用于生成数据库中升级的脚本
欢迎大家帮我想想,还有别的办法吗?希望加QQ282329611交流。
生成结果如图:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Title: Steps and Precautions for Implementing Batch Updates by Oracle Stored Procedures In Oracle database, stored procedures are a set of SQL statements designed to improve database performance, reuse code, and enhance security. Stored procedures can be used to update data in batches. This article will introduce how to use Oracle stored procedures to implement batch updates and provide specific code examples. Step 1: Create a stored procedure First, we need to create a stored procedure to implement batch update operations. The following is how to create a stored procedure

MySQL methods for deleting stored procedures include using the DROP PROCEDURE statement, using MySQL Workbench, and using command line tools. Detailed introduction: 1. Use the DROP PROCEDURE statement. The steps are to first open the MySQL client or use any tool that supports MySQL, then connect to your MySQL database, and finally execute the following SQL statement to delete the stored procedure; 2. Use MySQL Workbench to delete Stored procedures and so on.

Stored procedures in Oracle database are a specific type of stored procedures used to execute a series of SQL statements and data operations in the database. In actual database development work, sometimes we need to determine whether a certain table exists in the database, so that we can do some judgment and logical processing in the storage process. Below we will introduce how to implement the method of determining whether a table exists in Oracle database, and provide specific code examples. First, we can use the system table user_tables or all_t

Implementation Principles and Applications of Golang Stored Procedures Stored procedures are precompiled programs that are stored in relational databases and can be called by applications. They can effectively reduce the cost of network transmission of data and improve the execution efficiency of the database. Although Golang does not directly support stored procedures, you can simulate the functions of stored procedures by using SQL statements. This article will introduce the principles and applications of implementing stored procedures in Golang, and provide specific code examples. 1. The implementation principle of Golang stored procedure is in Gol

Performance Optimization Strategies for Batch Updates of Oracle Stored Procedures In Oracle database, a stored procedure is a database object used to process data logic or perform specific tasks. It can provide certain performance optimization strategies, especially when updating data in batches. Updating data in batches usually involves a large number of row-level operations. In order to improve performance and efficiency, we can adopt some strategies and techniques to optimize the performance of stored procedures. The following will introduce some performance optimization strategies for batch updates of Oracle stored procedures and provide specific code examples.

Title: Detailed comparison and advantage analysis of Oracle stored procedures and functions. In Oracle database, stored procedures and functions are two important database objects. They can both be used to encapsulate a series of SQL statements and logic to improve the efficiency and complexity of data operations. Usability. This article will compare the characteristics of Oracle stored procedures and functions in detail, as well as their respective advantages, and provide specific code examples. Stored procedure A stored procedure is a set of SQL statements and PL/SQL code logic that are pre-written and stored in the database.

How to write custom stored procedures and functions in MySQL using C# Introduction: MySQL is a widely used open source database management system, and C# is a commonly used object-oriented programming language. During the development process, we often need to use database stored procedures and functions to improve code reusability and performance. This article will introduce how to use C# to write custom stored procedures and functions in a MySQL database, and provide specific code examples. 1. Stored procedures A stored procedure is a set of SQL statements that perform specific tasks.

How to write custom triggers and stored procedures in MySQL using PHP Introduction: When developing applications, we often need to perform some operations at the database level, such as inserting, updating, or deleting data. MySQL is a widely used relational database management system, and PHP is a popular server-side scripting language. This article will introduce how to write custom triggers and stored procedures in MySQL using PHP, and provide specific code examples. 1. What are triggers and stored procedure triggers (Trigg
