存储过程编写小工具[收藏]

 

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*===============源程序開始==================*/  
None.gif
None.gif
CREATE   procedure  sp_GenInsert 
None.gif
@TableName   varchar ( 130 ), 
None.gif
@ProcedureName   varchar ( 130
None.gif
as  
None.gif
set  nocount  on  
None.gif
None.gif
declare   @maxcol   int
None.gif
@TableID   int  
None.gif
None.gif
set   @TableID   =   object_id ( @TableName
None.gif
None.gif
select   @MaxCol   =   max (colorder) 
None.gif
from  syscolumns 
None.gif
where  id  =   @TableID  
None.gif
None.gif
select   ' Create Procedure  '   +   rtrim ( @ProcedureName as  type, 0   as  colorder  into  #TempProc 
None.gif
union  
None.gif
select   convert ( char ( 35 ), ' @ '   +  syscolumns.name) 
None.gif
+   rtrim (systypes.name) 
None.gif
+   case   when   rtrim (systypes.name)  in  ( ' binary ' , ' char ' , ' nchar ' , ' nvarchar ' , ' varbinary ' , ' varchar ' then   ' ( '   +   rtrim ( convert ( char ( 4 ),syscolumns.length))  +   ' ) '  
None.gif
when   rtrim (systypes.name)  not   in  ( ' binary ' , ' char ' , ' nchar ' , ' nvarchar ' , ' varbinary ' , ' varchar ' then   '   '  
None.gif
end  
None.gif
+   case   when  colorder  <   @maxcol   then   ' , '  
None.gif
when  colorder  =   @maxcol   then   '   '  
None.gif
end  
None.gif
as  type, 
None.gifcolorder 
None.gif
from  syscolumns 
None.gif
join  systypes  on  syscolumns.xtype  =  systypes.xtype 
None.gif
where  id  =   @TableID   and  systypes.name  <>   ' sysname '  
None.gif
union  
None.gif
select   ' AS ' , @maxcol   +   1   as  colorder 
None.gif
union  
None.gif
select   ' INSERT INTO  '   +   @TableName , @maxcol   +   2   as  colorder 
None.gif
union  
None.gif
select   ' ( ' , @maxcol   +   3   as  colorder 
None.gif
union  
None.gif
select  syscolumns.name 
None.gif
+   case   when  colorder  <   @maxcol   then   ' , '  
None.gif
when  colorder  =   @maxcol   then   '   '  
None.gif
end  
None.gif
as  type, 
None.gifcolorder 
+   @maxcol   +   3   as  colorder 
None.gif
from  syscolumns 
None.gif
join  systypes  on  syscolumns.xtype  =  systypes.xtype 
None.gif
where  id  =   @TableID   and  systypes.name  <>   ' sysname '  
None.gif
union  
None.gif
select   ' ) ' ,( 2   *   @maxcol +   4   as  colorder 
None.gif
union  
None.gif
select   ' VALUES ' ,( 2   *   @maxcol +   5   as  colorder 
None.gif
union  
None.gif
select   ' ( ' ,( 2   *   @maxcol +   6   as  colorder 
None.gif
union  
None.gif
select   ' @ '   +  syscolumns.name 
None.gif
+   case   when  colorder  <   @maxcol   then   ' , '  
None.gif
when  colorder  =   @maxcol   then   '   '  
None.gif
end  
None.gif
as  type, 
None.gifcolorder 
+  ( 2   *   @maxcol   +   6 as  colorder 
None.gif
from  syscolumns 
None.gif
join  systypes  on  syscolumns.xtype  =  systypes.xtype 
None.gif
where  id  =   @TableID   and  systypes.name  <>   ' sysname '  
None.gif
union  
None.gif
select   ' ) ' ,( 3   *   @maxcol +   7   as  colorder 
None.gif
order   by  colorder 
None.gif
None.gif
None.gif
select  type  from  #tempproc  order   by  colorder 
None.gif
None.gif
drop   table  #tempproc
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*===============源程序結束==================*/
None.gif
None.gif
GO
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*===============源程序開始==================*/  
None.gif
None.gif
CREATE   procedure  sp_GenUpdate 
None.gif
@TableName   varchar ( 130 ), 
None.gif
@PrimaryKey   varchar ( 130 ), 
None.gif
@ProcedureName   varchar ( 130
None.gif
as  
None.gif
set  nocount  on  
None.gif
None.gif
declare   @maxcol   int
None.gif
@TableID   int  
None.gif
None.gif
set   @TableID   =   object_id ( @TableName
None.gif
None.gif
select   @MaxCol   =   max (colorder) 
None.gif
from  syscolumns 
None.gif
where  id  =   @TableID  
None.gif
None.gif
select   ' Create Procedure  '   +   rtrim ( @ProcedureName as  type, 0   as  colorder  into  #TempProc 
None.gif
union  
None.gif
select   convert ( char ( 35 ), ' @ '   +  syscolumns.name) 
None.gif
+   rtrim (systypes.name) 
None.gif
+   case   when   rtrim (systypes.name)  in  ( ' binary ' , ' char ' , ' nchar ' , ' nvarchar ' , ' varbinary ' , ' varchar ' then   ' ( '   +   rtrim ( convert ( char ( 4 ),syscolumns.length))  +   ' ) '  
None.gif
when   rtrim (systypes.name)  not   in  ( ' binary ' , ' char ' , ' nchar ' , ' nvarchar ' , ' varbinary ' , ' varchar ' then   '   '  
None.gif
end  
None.gif
+   case   when  colorder  <   @maxcol   then   ' , '  
None.gif
when  colorder  =   @maxcol   then   '   '  
None.gif
end  
None.gif
as  type, 
None.gifcolorder 
None.gif
from  syscolumns 
None.gif
join  systypes  on  syscolumns.xtype  =  systypes.xtype 
None.gif
where  id  =   @TableID   and  systypes.name  <>   ' sysname '  
None.gif
union  
None.gif
select   ' AS ' , @maxcol   +   1   as  colorder 
None.gif
union  
None.gif
select   ' UPDATE  '   +   @TableName , @maxcol   +   2   as  colorder 
None.gif
union  
None.gif
select   ' SET ' , @maxcol   +   3   as  colorder 
None.gif
union  
None.gif
select  syscolumns.name  +   '  = @ '   +  syscolumns.name 
None.gif
+   case   when  colorder  <   @maxcol   then   ' , '  
None.gif
when  colorder  =   @maxcol   then   '   '  
None.gif
end  
None.gif
as  type, 
None.gifcolorder 
+   @maxcol   +   3   as  colorder 
None.gif
from  syscolumns 
None.gif
join  systypes  on  syscolumns.xtype  =  systypes.xtype 
None.gif
where  id  =   @TableID   and  syscolumns.name  <>   @PrimaryKey   and  systypes.name  <>   ' sysname '  
None.gif
union  
None.gif
select   ' WHERE  '   +   @PrimaryKey   +   '  = @ '   +   @PrimaryKey ,( 2   *   @maxcol +   4   as  colorder 
None.gif
order   by  colorder 
None.gif
None.gif
None.gif
select  type  from  #tempproc  order   by  colorder 
None.gif
None.gif
drop   table  #tempproc
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*===============源程序結束==================*/
None.gif
None.gif
GO
None.gif

转载于:https://www.cnblogs.com/winksky/archive/2006/08/10/473651.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值