欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SQL SERVER编写存储过程小工具

程序员文章站 2022-08-06 17:30:32
在开发数据库系统的过程中,经常要写很多的存储过程。为了统一格式和简化开发过程,我编写一些存储过程,用来自动生成存储过程。下面就为您简单介绍一下它们。其中一个用于生成inse...
在开发数据库系统的过程中,经常要写很多的存储过程。为了统一格式和简化开发过程,我编写一些存储过程,用来自动生成存储过程。下面就为您简单介绍一下它们。其中一个用于生成insert过程,另一个用于生成update过程。 


sp_geninsert 

该过程运行后,它为给定的表生成一个完整的insert过程。如果原来的表有标识列,您得将生成的过程中的set idntity_insert on 语句手工删除。 

语法如下 

sp_geninsert < table name >,< stored procedure name > 

以northwind 数据库为例 

sp_geninsert 'employees', 'ins_employees' 

最后会生成一个insert存储过程。利用它,您可以作进一步的开发。 


sp_genupdate 

它会为一个表生成update存储过程。语法如下: 

sp_genupdate < table name >,< primary key >,< stored procedure name > 

以northwind 数据库为例 

sp_genupdate 'employees','employeeid','upd_employees' 

运行后生成如下所示的存储过程: 

create procedure upd_employees 

@employeeid int 

@lastname nvarchar(40) , 

@firstname nvarchar(20) , 

@title nvarchar(60) , 

@titleofcourtesy nvarchar(50) , 

@birthdate datetime , 

@hiredate datetime , 

@address nvarchar(120) , 

@city nvarchar(30) , 

@region nvarchar(30) , 

@postalcode nvarchar(20) , 

@country nvarchar(30) , 

@homephone nvarchar(48) , 

@extension nvarchar(8) , 

@phote image , 

@notes ntext , 

@reportsto int , 

@photopath nvarchar(510) 

as 

update employees 

set 

lastname = @lastname, 

firstname = @firstname, 

title = @title, 

titleofcourtesy = @titleofcourtesy, 

birthdate = @birthdate, 

hiredate = @hiredate, 

address = @address, 

city = @city, 

regin = @regin, 

postalcode = @postcode, 

country = @country, 

homephone = @homephone, 

extension = @extension, 

photo = @photo 

notes = @notes, 

reportsto = @reportsto, 

photopath = @photopath 

where employeeid = @employeeid 


使用以上的两个存储过程,节省了我不少时间。特别是在改变了表结构后,重新构造各个存储过程的过程中。您可以改写这两个程序,来自动生成别的存储过程。

sql server编写存储过程小工具
以下是两个存储过程的源程序
/*===========================================================

语法: sp_geninsert <table name>,<stored procedure name>
以northwind 数据库为例
sp_geninsert 'employees', 'ins_employees'

注释:如果您在master系统数据库中创建该过程,那您就可以在您服务器上所有的数据库中使用该过程。

=============================================================*/

create procedure sp_geninsert
@tablename varchar(130),
@procedurename varchar(130)
as
set nocount on

declare @maxcol int,
@tableid int
--itlearner.com
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

sql server编写存储过程小工具
功能:为给定表创建update存储过程
语法: sp_genupdate <table name>,<primary key>,<stored procedure name>
以northwind 数据库为例
sp_genupdate 'employees','employeeid','upd_employees'

注释:如果您在master系统数据库中创建该过程,那您就可以在您服务器上所有的数据库中使用该过程。

===========================================================*/
create procedure sp_genupdate
@tablename varchar(130),
@primarykey varchar(130),
@procedurename varchar(130)
as
set nocount on

declare @maxcol int,
@tableid int
--itlearner.com
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 'update ' + @tablename,@maxcol + 2 as colorder
union
select 'set',@maxcol + 3 as colorder
union
select syscolumns.name + ' = @' + 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 syscolumns.name <> @primarykey and systypes.name <> 'sysname'
union
select 'where ' + @primarykey + ' = @' + @primarykey,(2 * @maxcol) + 4 as colorder
order by colorder


select type from #tempproc order by colorder

drop table #tempproc
/*=======源程序结束=========*/