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

MSSQL
Microsoft SQLServer的版本区别及选择
在SQL Server数据库中为标识(IDENTITY)列插入显式值
访问和更改关系数据,使用MSSQL外联接
一个查看MSSQLServer数据库空间使用情况的存储过程 SpaceUsed
SQL语句去掉重复记录,获取重复记录
复习一下sql server的差异备份
SQL中object_id函数的用法
SQL Server日期计算
找回SQL企业管理器里的SQL连接的密码的方法
mssql数据库系统崩溃后的一般处理步骤与方法
海量数据库的查询优化及分页算法方案
SQL Server连接中三个常见的错误分析
在程序中压缩sql server2000的数据库备份文件的代码
MS SQL SERVER 数据库日志压缩方法与代码
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
将Session值储存于SQL Server中

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


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

SQLServer编写存储过程小工具

以下是两个存储过程的源程序


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

语法:sp_GenInsert<TableName>,<StoredProcedureName>
以northwind数据库为例
sp_GenInsert'Employees','INS_Employees'

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

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

CREATEproceduresp_GenInsert
@TableNamevarchar(130),
@ProcedureNamevarchar(130)
as
setnocounton

declare@maxcolint,
@TableIDint

set@TableID=object_id(@TableName)

select@MaxCol=max(colorder)
fromsyscolumns
whereid=@TableID

select'CreateProcedure'+rtrim(@ProcedureName)astype,0ascolorderinto#TempProc
union
selectconvert(char(35),'@'+syscolumns.name)
+rtrim(systypes.name)
+casewhenrtrim(systypes.name)in('binary','char','nchar','nvarchar','varbinary','varchar')then'('+rtrim(convert(char(4),syscolumns.length))+')'
whenrtrim(systypes.name)notin('binary','char','nchar','nvarchar','varbinary','varchar')then''
end
+casewhencolorder<@maxcolthen','
whencolorder=@maxcolthen''
end
astype,
colorder
fromsyscolumns
joinsystypesonsyscolumns.xtype=systypes.xtype
[email protected]<>'sysname'
union
select'AS',@maxcol+1ascolorder
union
select'INSERTINTO'+@TableName,@maxcol+2ascolorder
union
select'(',@maxcol+3ascolorder
union
selectsyscolumns.name
+casewhencolorder<@maxcolthen','
whencolorder=@maxcolthen''
end
astype,
colorder+@maxcol+3ascolorder
fromsyscolumns
joinsystypesonsyscolumns.xtype=systypes.xtype
[email protected]<>'sysname'
union
select')',(2*@maxcol)+4ascolorder
union
select'VALUES',(2*@maxcol)+5ascolorder
union
select'(',(2*@maxcol)+6ascolorder
union
select


selecttypefrom#tempprocorderbycolorder

droptable#tempproc