自动生成Insert语句


GO
/****** 对象:  StoredProcedure [dbo].[pCOMM_CREATE_INSERT]    脚本日期: 07/01/2009 09:05:11 ******/
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 暂不处理
*/


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

declare @lColumn varchar(2000)
declare @lColumnData varchar(4000)
declare @lColumnData1 varchar(4000)  
declare @lSql varchar(8000)
declare @lSql1 varchar(8000)
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'
 

posted on 2009-07-01 09:06  欧尔  阅读(993)  评论(0编辑  收藏  举报

导航