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

MSSQL
SQL Server:SQL Server Maintenance Plan Wizard
SQL Server:SQL mai的配置和使用,你有所研究吗?
SQLServer:数据库的定时作业设置(经典)
SQL Server:小编浅谈SQL全文本检索方法
SQL Server:小编经验谈设计数据库表和字段
SQL Server:小编浅谈数据库完整性之约束
SQL Server:容易忽视的动态约束
SQL Server:时态数据库的时间间隔
SQL Server:浅谈数据库的安全性
SQL Server:不得不看的数据库设计技巧(看了终身受益)
SQL Server:数据库中的快照
SQL Server:数据库管理中关系代数的语法
SQL Server:关系代数中的语义
验证SQL保留字
精妙的SQL语句
SQL Server 数据库管理常用的SQL和T-SQL语句
SQL SERVER 与ACCESS、EXCEL的数据转换
SQL SERVER的数据类型
未公开的SQL Server口令的加密函数
SQl 语句(常见)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 44 ::
收藏到网摘: 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