自动生成存储过程一

昨天领导让我把所有对数据的操作放在数据库进行,需要用到很多的存储过程,一想到要写那么多的存储过程,脑袋就两个大,于是在网上查找相关资料,竟然能找到自动生成存储过程的代码,不错,借用过来还真的能省不少时间呢,下面的存储过程Sp_GenInsert就可以帮我生成插入数据的存储过程。

调用sp_GenInsert存储过程的代码:
sp_GenInsert 'Employees', 'INS_Employees'

生成
sp_GenInsert存储过程代码:
CREATE procedure sp_GenInsert
@TableName varchar(130),
@ProcedureName varchar(130)
as
set nocount on

declare @maxcol int,
@TableID int

set @TableID = object_id(@TableName)

select @MaxCol = max(colorder)
from syscolumns
where id = @TableID

select 'Create Procedure ' + rtrim(@ProcedureName) as type,0 as colorder into #TempProc
union
select convert(char(35),'@' + syscolumns.name)
+ rtrim(systypes.name)
+ case when rtrim(systypes.name) in ('binary','char','nchar','nvarchar','varbinary','varchar') then '(' + rtrim(convert(char(4),syscolumns.length)) + ')'
when rtrim(systypes.name) not in ('binary','char','nchar','nvarchar','varbinary','varchar') then ' '
end
+ case when colorder < @maxcol then ','
when colorder = @maxcol then ' '
end
as type,
colorder
from syscolumns
join systypes on syscolumns.xtype = systypes.xtype
where id = @TableID and systypes.name <> 'sysname'
union
select 'AS',@maxcol + 1 as colorder
union
select 'INSERT INTO ' + @TableName,@maxcol + 2 as colorder
union
select '(',@maxcol + 3 as colorder
union
select syscolumns.name
+ case when colorder < @maxcol then ','
when colorder = @maxcol then ' '
end
as type,
colorder + @maxcol + 3 as colorder
from syscolumns
join systypes on syscolumns.xtype = systypes.xtype
where id = @TableID and systypes.name <> 'sysname'
union
select ')',(2 * @maxcol) + 4 as colorder
union
select 'VALUES',(2 * @maxcol) + 5 as colorder
union
select '(',(2 * @maxcol) + 6 as colorder
union
select '@' + syscolumns.name
+ case when colorder < @maxcol then ','
when colorder = @maxcol then ' '
end
as type,
colorder + (2 * @maxcol + 6) as colorder
from syscolumns
join systypes on syscolumns.xtype = systypes.xtype
where id = @TableID and systypes.name <> 'sysname'
union
select ')',(3 * @maxcol) + 7 as colorder
order by colorder


select type from #tempproc order by colorder

drop table #tempproc

转载于:https://www.cnblogs.com/yumianhu/archive/2011/11/10/3713005.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL 自动生成存储过程工具是一种可以根据数据库结构和业务需求自动生成存储过程的工具。它通常由开发人员使用,可以大大简化存储过程的编过程,并提高开发效率。 这种工具一般具有以下功能: 1. 数据库分析:工具可以通过读取数据库的元数据信息,包括表、列、索引等信息,来分析数据库的结构。然后根据分析结果,生成相应的存储过程。 2. 模板生成:工具提供存储过程模板,开发人员可以根据业务需求选择相应的模板,然后工具根据模板自动生成存储过程的代码。模板一般包括基本的数据库操作,例如插入、更新、删除等。 3. 参数设置:开发人员可以设置存储过程的输入参数和输出参数,以满足不同的业务需求。参数设置一般包括参数的名称、数据类型、长度等。 4. 代码风格:工具可以根据开发人员的喜好或公司的规范,生成符合相应的代码风格的存储过程。这样可以保证代码的一致性和可读性。 使用SQL 自动生成存储过程工具可以帮助开发人员节省大量的时间和精力,减少编存储过程的错误风险。而且,由于自动生成存储过程是基于数据库结构和业务需求,因此可以更好地保证存储过程的正确性和可用性。 需要注意的是,虽然SQL 自动生成存储过程工具可以减少存储过程的编工作,但在使用过程中仍然需要开发人员对生成的代码进行必要的检查和修改,以确保满足实际需求和安全性要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值