SQL SERVER编写存储过程小工具
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
/*=======源程序结束=========*/
上一篇: Win10 Mobile RS2预览版14943上手视频曝光
下一篇: 除了喝酒…你还会干什么?
推荐阅读
-
SQL Server 存储过程 数组参数 (How to pass an array into a SQL Server stored procedure)
-
SQL存储过程初探
-
sql server中千万数量级分页存储过程代码
-
ASP调用SQL SERVER存储程序
-
sql清空表数据后重新添加数据存储过程的示例
-
sql 判断函数、存储过程是否存在的代码整理
-
sql server编写archive通用模板脚本实现自动分批删除数据
-
SQL Server存储过程输入参数使用表值
-
Exec msdb.dbo.sp_send_dbmail 参数详解(SQL Server 存储过程发邮件)
-
sql server2008调试存储过程的完整步骤