我找到了兩個
儲存過程,能自動生成對乙個資料表的插入和更新的
儲存過程,現在奉獻給大家!
插入:create procedure sp_geninsert
@tablename varchar(130),
@procedurename varchar(130)
asset 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 ' '
endas 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 ' '
endas 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 ' '
endas 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)
asset 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 ' '
endas 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 ' '
endas 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
自動生成對錶進行插入和更新的儲存過程的儲存過程
我找到了兩個儲存過程,能自動生成對乙個資料表的插入和更新的儲存過程,現在奉獻給大家!插入 create procedure sp geninsert tablename varchar 130 procedurename varchar 130 asset nocount on declare ma...
自動生成表的新增更新資料的儲存過程
這兩天利用了一些時間寫了乙個自動生成表的新增更新資料儲存過程的儲存過程,今天終於完成了,以後大家都方便多了,一勞永逸了,以前同事反映有時候表的字段過多,寫起儲存過程來費時 費力 費神,而且一不留神還容易出錯 還記得會員系統剛測試的時候,以前的那個同事就因為模糊大意把 儲存過程中更新時的條件忘加了,在...
mysql中生成時間維度的儲存過程(儲存過程示例)
本文主要記錄在bi和資料分析過程中碰到的生成時間維度的問題,另外也是乙個mysql的儲存過程基礎示例 包含 儲存過程基本語法 變數定義 while迴圈 異常處理 以下儲存過程生成了以當前日期為基準前後3650天的日期記錄 sql如下 create table dim date id int 8 no...