自动生成对数据表进行插入操作的存储过程的存储过程

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值