自动生成对表进行插入和更新的存储过程的存储过程
来源:互联网 发布:武直10知乎 编辑:程序博客网 时间:2024/04/29 20:46
自动生成对表进行插入和更新的存储过程的存储过程
自动生成对表进行插入和更新的存储过程的存储过程 我找到了两个存储过程,能自动生成对一个数据表的插入和更新的存储过程,现在奉献给大家!
插入:
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
更新:
Create procedure sp_GenUpdate
@TableName varchar(130),
@PrimaryKey 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 'UPDATE ' + @TableName,@maxcol + 2 as colorder
union
select 'SET',@maxcol + 3 as colorder
union
select syscolumns.name + ' = @' + 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 syscolumns.name <> @PrimaryKey and systypes.name <> 'sysname'
union
select 'WHERE ' + @PrimaryKey + ' = @' + @PrimaryKey,(2 * @maxcol) + 4 as colorder
order by colorder
select type from #tempproc order by colorder
drop table #tempproc
- 自动生成对表进行插入和更新的存储过程的存储过程
- 自动生成对表进行插入和更新的存储过程的存储过程
- 自动生成对表进行插入和更新的存储过程的存储过程
- 自动生成对表进行插入和更新的存储过程的存储过程
- 自动生成对表进行插入和更新的存储过程的存储过程
- 存储过程:数据的插入和更新
- 自动生成表的更新数据的存储过程
- 自动生成表的添加更新数据的存储过程
- SQL server存储过程:数据的插入和更新
- 实现自动插入产品的存储过程
- 生成插入语句的存储过程
- 根据表结构。生成插入、更新、删除的通用存储过程
- 在SQL存储过程中对表进行多行数据更新,插入
- 自动生成人才编号的存储过程
- 通用的更新存储过程和删除存储过程
- MSSQL生成表数据SQL插入语句的存储过程
- 自动生成存储过程
- 自动生成存储过程
- 个人总结百种网站推广方式(1-30)
- 无限循环的一种表示
- 情
- memcached 官方网站:http://www.danga.com/memcached/ —c++https://github.com/gwhalin/Memcached-Java-Client/downloads--java
- 停机检查
- 自动生成对表进行插入和更新的存储过程的存储过程
- 检查是否为特定值
- JPMorgan Executes CDS Trades Through MarketAxess' Platform
- FireBug 调试JS入门 —如何调试JS
- 不能循环定义
- 怎么赚积分
- 不能循环定义2
- 关于FindControl的问题
- 模拟状态1