SQL-生成Insert语句的存储过程

直接上代码,把代码复制到SQL管理器,直接运行。

View Code
USE [SSC_IDS]
GO
/****** 对象: StoredProcedure [SSC].[pCOMM_CREATE_INSERT] 脚本日期: 05/20/2011 10:41:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
DESC:CREATE INSERT SQL FOR TABLE
PARAs:@pTableName--Table Name
@pWhere--Filter (eg.'where 1=1')
@pCreateDel--Create Delete Sql
REMARK:timestamp,image,text,ntext,sql_variant 暂不处理
*/


create PROC [SSC].[pCOMM_CREATE_INSERT]
@pTableName sysname,
@pWhere varchar(500)='',
@pCreateDel bit=0 --是否生成删除语句
AS

declare @lColumn varchar(max)
declare @lColumnData varchar(max)
declare @lColumnData1 varchar(max)
declare @lSql varchar(max)
declare @lSql1 varchar(max)
declare @lXtype tinyint
declare @lName sysname
declare @lObjectId int
declare @lObjectName sysname
declare @lIdent int

set nocount on

set @lObjectId=object_id(@pTableName)

if @lObjectId is null -- 對象是否存在
begin
print 'The object not exists'
return
end

if OBJECTPROPERTY(@lObjectId,'IsTable') <> 1 -- 對象是否是table
begin
print 'The object is not table'
return
end

set @lObjectName=rtrim(object_name(@lObjectId))

if @lObjectName is null or charindex(@lObjectName,@pTableName)=0 --此判断不严密
begin
print 'object not in current database'
return
end


select @lIdent=status&0x80 from syscolumns where id=@lObjectId and status&0x80=0x80

if @lIdent is not null
print 'SET IDENTITY_INSERT '+@pTableName+' ON'

declare syscolumns_cursor cursor for
select c.name,c.xtype from syscolumns c where c.id=@lObjectId
order by c.colid
open syscolumns_cursor
set @lColumn=''
set @lColumnData=''
set @lColumnData1=''

fetch next from syscolumns_cursor into @lName,@lXtype
while @@fetch_status <>-1
begin
if @@fetch_status<>-2
begin
if @lXtype not in(189,34,35,99,98) --image,text,ntext,sql_variant不处理
begin
set @lColumn=@lColumn+case when len(@lColumn)=0 then'' else ','end+@lName
if len(@lColumnData)<3500
set @lColumnData=@lColumnData+case when len(@lColumnData)=0 then '' else ','','','end
+case when @lXtype in(167,175) then '''''''''+'+'replace(rtrim('+@lName+'),'''''''','''''''''''')'+'+''''''''' --varchar,char
when @lXtype in(231,239) then '''N''''''+'+'replace(rtrim('+@lName+'),'''''''','''''''''''')'+'+''''''''' --nvarchar,nchar
when @lXtype=61 then '''''''''+convert(char(23),'+@lName+',121)+''''''''' --datetime
when @lXtype=58 then '''''''''+convert(char(16),'+@lName+',120)+''''''''' --smalldatetime
when @lXtype=36 then '''''''''+convert(char(36),'+@lName+')+''''''''' --uniqueidentifier
else @lName end
else
set @lColumnData1=@lColumnData1+case when len(@lColumnData)=0 then '' else ','','','end
+case when @lXtype in(167,175) then '''''''''+'+'replace(rtrim('+@lName+'),'''''''','''''''''''')'+'+''''''''' --varchar,char
when @lXtype in(231,239) then '''N''''''+'+'replace(rtrim('+@lName+'),'''''''','''''''''''')'+'+''''''''' --nvarchar,nchar
when @lXtype=61 then '''''''''+convert(char(23),'+@lName+',121)+''''''''' --datetime
when @lXtype=58 then '''''''''+convert(char(16),'+@lName+',120)+''''''''' --smalldatetime
when @lXtype=36 then '''''''''+convert(char(36),'+@lName+')+''''''''' --uniqueidentifier
else @lName end

end
end

fetch next from syscolumns_cursor into @lName,@lXtype

end

close syscolumns_cursor
deallocate syscolumns_cursor

if (@pCreateDel=1) /*需要产生对应的删除语句*/
Select 'Delete From '+@pTableName+' '+@pWhere +char(13)+'GO'

--set @lSql='set nocount on select ''insert '+@pTableName+'('+@lColumn+') values(''as ''--'','+@lColumnData+','')'' from '+@pTableName+' '+@pWhere
set @lSql='set nocount on select ''INSERT '+@pTableName+'('+@lColumn+') VALUES(''as ''--'','+@lColumnData
set @lSql1=@lColumnData1+','')'' from '+@pTableName+' '+@pWhere
print '--'+@lSql + @lSql1
exec(@lSql+@lSql1)

if @lIdent is not null
print 'SET IDENTITY_INSERT '+@pTableName+' OFF'

 

调用方式

EXEC SSC.pCOMM_CREATE_INSERT
@pTableName = '', --表名
@pWhere = '', --按条件生成
@pCreateDel = '' --是否生产删除语句



转载于:https://www.cnblogs.com/CaesarX/archive/2012/01/18/Inserter.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值