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

MSSQL
用SQL语句实现替换字符串
mssql查找备注(text,ntext)类型字段为空的方法
MSSQL数据类型及长度限制详细说明
SQL Server下几个危险的扩展存储过程
一些SQL Server存储过程参数及例子
sql高级技巧几个有用的Sql语句
SQL语句实现SQL Server 2000及Sql Server 2005日志收缩(批量)
用SQL建立索引的方法步骤
MSsql每天自动备份数据库并每天自动清除log的脚本
mssql无数据库日志文件恢复数据库的方法
SQL Server常用管理命令小结
SQL SERVER性能优化综述(很好的总结,不要错过哦)
sqlserver 游标的简单示例
sqlserver只有MDF文件恢复数据库的方法
在SQL Server启动时自动执行存储过程。
在 SQLSERVER 中快速有条件删除海量数据
阿拉伯数字转大写中文_财务常用sql存储过程
SQL Server存储过程的基础说明
列出SQL Server中具有默认值的所有字段的语句
文本、Excel、Access数据导入SQL Server2000的方法

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


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