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

MSSQL
SQL Server服务器内存升级后的烦恼
SQL Server数据库超级管理员账号防护
微软证实最新的关键SQL Server漏洞
在SQL Server 2000数据仓库中使用分区
深入探讨SQL Server 2008商务智能(BI)
正确理解SQL Server四类数据仓库建模方法
SQL Server中读取XML文件的简单做法
教程:打造SQL Server2000的安全策略
Windows存储 SQL行溢出 差异备份及疑问
保护SQL服务器的安全 用户识别问题
SQL Server数据在不同数据库中的应用
关于SQL Server中索引使用及维护简介
在SQL Server中使用CLR调用.NET方法
50种方法巧妙优化SQL Server数据库
比较SQL Server约束和DML触发器
从IIS到SQL Server数据库安全
在SQL server2005数据库下创建计划任务
SQL Server保障数据一致性的法宝
数据库备份过程中经常遇到的九种情况
轻松解决ORA-12560: TNS 协议适配器错误

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


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

SQLServer编写存储过程小工具
功能:为给定表创建Update存储过程
语法:sp_GenUpdate<TableName>,<PrimaryKey>,<StoredProcedureName>
以northwind数据库为例
sp_GenUpdate'Employees','EmployeeID','UPD_Employees'

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


/*===========================================================*/
CREATEproceduresp_GenUpdate
@TableNamevarchar(130),
@PrimaryKeyvarchar(130),
@ProcedureNamevarchar(130)
as
setnocounton


declare@maxcolint,
@TableIDint
'knowsky.com
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'UPDATE'+@TableName,@maxcol+2ascolorder
union
select'SET',@maxcol+3ascolorder
union
selectsyscolumns.name+'=@'+syscolumns.name
+casewhencolorder<@maxcolthen','
whencolorder=@maxcolthen''
end
astype,
colorder+@maxcol+3ascolorder
fromsyscolumns
joinsystypesonsyscolumns.xtype=systypes.xtype
[email protected]<>@PrimaryKeyandsystypes.name<>'sysname'
union
select'WHERE'+@PrimaryKey+'=@'+@PrimaryKey,(2*@maxcol)+4ascolorder
orderbycolorder


selecttypefrom#tempprocorderbycolorder


droptable#tempproc
/*=======源程序结束=========*/