当前位置: 首页 > 图文教程 > 数据库 > MSSQL > SQL Server编写存储过程小工具(二)

MSSQL
MS SQL SERVER2005 XML 最佳实践
SQL Server对文件进行全文检索的查询
教你构建SQL Server可管理安全机制
维护SQL Server的交易日志经验总结
SQL SERVER 2005 EXPRESS不能远程连接的问题
SQL Server与Oracle并行访问本质区别
SQL Server数据库优化其索引的小技巧
分析及解决SQLServer死锁问题
用SQL Server为Web浏览器提供图像
SQL Server SQL Agent服务使用小结
SQL Server 存储过程的分页方案比拼
SQL Server数据库中存储引擎深入探讨
四招解决SQL Server对上亿表的排序和join的问题
SQL Server数据库管理员必须掌握的DBCC命令
如何将sql数据库的文件备份到本地?
如何解决Sybase数据库乱码问题详解
SQL Server:SQLServer中最小函数依赖集
小编谈Transact-SQL中的一些命名规范
谈SQL编写规范
浅谈SQL命名与注释规范

MSSQL 中的 SQL Server编写存储过程小工具(二)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 51 ::
收藏到网摘: n/a

 

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

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