一个自动生成预制SQL 的存储过程(一)
2011-03-27 18:22
351 查看
前言
我们工作的时候经常会有一些数据是在开发期整理好,到客户那预制到处据库中去,于是乎就有了下面的存储过程。
=======SQL server 版本====================================================
/*exec HRspGenInsertSQL 'HRobjects','HRobjects_NM','2'
@tablename 表名
@tableNM 表内码字段名
@ifExists 是否包含if exists 判断语句 1:包含;0:不包含;2:如果存在先删除,再插入*/
IF EXISTS (SELECT name FROM sysobjects WHERE NAME = 'HRspGenInsertSQL' AND type = 'P')
DROP PROCEDURE HRspGenInsertSQL
GO
Create PROCEDURE HRspGenInsertSQL
(@tablename varchar(400),@tableNM varchar(400),@ifExists char(1))
as
declare @sql varchar(8000)
declare @sqlValues varchar(8000)
declare @sqlInsert varchar(8000)
begin
CREATE TABLE #UsertableData (
Row int IDENTITY(1,1) PRIMARY KEY,
datasql varchar(8000)
)
Declare @NM varchar(36)
execute('Declare cur Cursor for select '+@tableNM+' from '+ @tablename+' order by '+@tableNM + '')
open cur
fetch next from cur into @NM
while (@@fetch_status=0)
begin
set @sql =' ('
set @sqlValues = 'values (''+'
select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + name + ','
from
(select case
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'
when xtype in (58,61)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'
when xtype in (167)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (231)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (175)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
when xtype in (239)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
else '''NULL'''
end as Cols,name
from syscolumns
where id = object_id(@tablename)
) T
if(LTRIM(@ifExists) != '' and LTRIM(@ifExists) = '1')
begin
set @sqlInsert = ' if not exists ( select 1 from '+@tablename+' where '+@tablename+'.'+@tableNM+' = '''+@NM+''') ';
set @sqlInsert = REPLACE(@sqlInsert,'''','''''');
exec('insert into #UsertableData(datasql) values(''' + @sqlInsert +''')')
end
else if(LTRIM(@ifExists) = '2')
begin
set @sqlInsert = ' if exists ( select 1 from '+@tablename+' where '+@tablename+'.'+@tableNM+' = '''+@NM+''') ';
set @sqlInsert = REPLACE(@sqlInsert,'''','''''');
exec('insert into #UsertableData(datasql) values(''' + @sqlInsert +''')')
set @sqlInsert = ' delete from '+@tablename+' where '+@tablename+'.'+@tableNM+' = '''+@NM+''' ';
set @sqlInsert = REPLACE(@sqlInsert,'''','''''');
exec('insert into #UsertableData(datasql) values(''' + @sqlInsert +''')')
insert into #UsertableData(datasql)
values ('go');
end
set @sqlInsert = 'select '+'''INSERT INTO '+@tablename+''+ left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')''
from ' + @tablename +' where '+@tablename+'.'+@tableNM+' = '''+@NM+''' ';
EXEC('insert into #UsertableData(datasql)' + @sqlInsert )
insert into #UsertableData(datasql)
values ('go');
fetch next from cur into @NM
end
close cur;
Deallocate cur
select datasql from #UsertableData order by Row
drop table #UsertableData
END
go
我们工作的时候经常会有一些数据是在开发期整理好,到客户那预制到处据库中去,于是乎就有了下面的存储过程。
=======SQL server 版本====================================================
/*exec HRspGenInsertSQL 'HRobjects','HRobjects_NM','2'
@tablename 表名
@tableNM 表内码字段名
@ifExists 是否包含if exists 判断语句 1:包含;0:不包含;2:如果存在先删除,再插入*/
IF EXISTS (SELECT name FROM sysobjects WHERE NAME = 'HRspGenInsertSQL' AND type = 'P')
DROP PROCEDURE HRspGenInsertSQL
GO
Create PROCEDURE HRspGenInsertSQL
(@tablename varchar(400),@tableNM varchar(400),@ifExists char(1))
as
declare @sql varchar(8000)
declare @sqlValues varchar(8000)
declare @sqlInsert varchar(8000)
begin
CREATE TABLE #UsertableData (
Row int IDENTITY(1,1) PRIMARY KEY,
datasql varchar(8000)
)
Declare @NM varchar(36)
execute('Declare cur Cursor for select '+@tableNM+' from '+ @tablename+' order by '+@tableNM + '')
open cur
fetch next from cur into @NM
while (@@fetch_status=0)
begin
set @sql =' ('
set @sqlValues = 'values (''+'
select @sqlValues = @sqlValues + cols + ' + '','' + ' ,@sql = @sql + name + ','
from
(select case
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
then 'case when '+ name +' is null then ''NULL'' else ' + 'cast('+ name + ' as varchar)'+' end'
when xtype in (58,61)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'
when xtype in (167)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (231)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (175)
then 'case when '+ name +' is null then ''NULL'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
when xtype in (239)
then 'case when '+ name +' is null then ''NULL'' else '+'''N'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as Char(' + cast(length as varchar) + '))+'''''''''+' end'
else '''NULL'''
end as Cols,name
from syscolumns
where id = object_id(@tablename)
) T
if(LTRIM(@ifExists) != '' and LTRIM(@ifExists) = '1')
begin
set @sqlInsert = ' if not exists ( select 1 from '+@tablename+' where '+@tablename+'.'+@tableNM+' = '''+@NM+''') ';
set @sqlInsert = REPLACE(@sqlInsert,'''','''''');
exec('insert into #UsertableData(datasql) values(''' + @sqlInsert +''')')
end
else if(LTRIM(@ifExists) = '2')
begin
set @sqlInsert = ' if exists ( select 1 from '+@tablename+' where '+@tablename+'.'+@tableNM+' = '''+@NM+''') ';
set @sqlInsert = REPLACE(@sqlInsert,'''','''''');
exec('insert into #UsertableData(datasql) values(''' + @sqlInsert +''')')
set @sqlInsert = ' delete from '+@tablename+' where '+@tablename+'.'+@tableNM+' = '''+@NM+''' ';
set @sqlInsert = REPLACE(@sqlInsert,'''','''''');
exec('insert into #UsertableData(datasql) values(''' + @sqlInsert +''')')
insert into #UsertableData(datasql)
values ('go');
end
set @sqlInsert = 'select '+'''INSERT INTO '+@tablename+''+ left(@sql,len(@sql)-1)+') ' + left(@sqlValues,len(@sqlValues)-4) + ')''
from ' + @tablename +' where '+@tablename+'.'+@tableNM+' = '''+@NM+''' ';
EXEC('insert into #UsertableData(datasql)' + @sqlInsert )
insert into #UsertableData(datasql)
values ('go');
fetch next from cur into @NM
end
close cur;
Deallocate cur
select datasql from #UsertableData order by Row
drop table #UsertableData
END
go
相关文章推荐
- 一个自动生成预制SQL 的存储过程(二)
- 自动生成INSERT语句的SQL存储过程
- 得用SQL语句生成某个库下所有存储过程脚本,并每个存为一个文件
- 分享一个自动生成单据的存储过程
- SQL过程自动C#封装,支持从表到基本存储过程生成,
- 自动生成对一个数据表的插入和更新的存储过程
- Sql Server - SQL分页存储过程自动生成
- 用SQL语句生成某个库下所有存储过程脚本,并每个存为一个文件
- 好久没来了。共享一个自动生成编号的存储过程
- 自动生成INSERT语句的SQL存储过程
- 生成表中数据的sql语法的存储过程
- 将表数据生成SQL脚本的存储过程
- 一个SQL存储过程分页和ASP调用
- 将表中的数据自动生成INSERT语句的存储过程,自己收藏一下
- mssql 生成‘批量Insert数据的sql语句’的存储过程
- 根据表或者视图自动生成分页语句的存储过程,适用于Sql Server 2005及以上版本
- 生成sql表数据存储过程脚本
- 自动产生存储过程的sql 脚本
- sql 2005 中如何自动生成不重复的,长度相同字符串作为一个数据表的id
- 一个用来生成流水号的存储过程