將資料表中的資料轉換為insert語句,并增加判斷條件

根據網上一個已有的存儲過程修改而來
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tool_GenInsertSQL]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[tool_GenInsertSQL]
GO
/*功能:將資料表中的資料轉換為insert語句,并增加判斷條件
*MSSQL 查詢分析器需調整 
*工具--選項--結果--每個資料行的字元數,增大到1024或更大,避免一行中資料較多,保存時會被截斷問題
* exec dbo.emisGenInsertSQL'ins_d','s_no,in_no','where s_no = ''000001'''
*/
create procedure tool_GenInsertSQL
(
    @TableName varchar(256)--資料表名
    ,@ExistCond varchar(256)--判斷條件中需要用於判斷的字段名稱,若有多個需用 "," 分隔,若不需判斷條件請給空字串
    ,@WhereOrderByClause varchar(1000) = '' --用於資料表的查詢條件和排序'where 1 = 1 order by null'
)
as
begin
    declare @sql varchar(8000)
    declare @sqlValues varchar(8000)
    declare @sqlExist varchar(1000) set @sqlExist=''
    declare @deli varchar(1) set @deli=','--分隔符,若傳入的判斷條件包多字段(多主鍵)
    declare @sqlExistCond varchar(1000) set @sqlExistCond = '(1=1)' --最終的判斷條件 
    declare @tempCond varchar(100)
    if @ExistCond>'' --不等於空才加判斷條件
        begin
	    --組判斷條件開始
	    while(  charindex(@deli,@ExistCond)>0 )
	             begin
		set @tempCond = substring(@ExistCond,1,charindex(@deli,@ExistCond)-1)
		set @sqlExistCond = @sqlExistCond + ' and ['+@tempCond+'] = ''''''+cast ('+@tempCond  +' as varchar)+'''''''
		set @ExistCond = substring(@ExistCond,charindex(@deli,@ExistCond)+1,len(@ExistCond))	
	             end		
	     set @sqlExistCond = @sqlExistCond + ' and ['+@ExistCond+'] = ''''''+cast ('+@ExistCond  +' as varchar)+'''''')'
	     set @sqlExist = 'if not exists (select 1 from ['+@TableName+'] where ' + @sqlExistCond--判斷條件
	     --組判斷條件完成 	
         end
    set @sql = ' ''(''' + char(13) + ','
    set @sqlValues = ' values ('''+ char(13) + ','
    --查出系統表中指定表的列名
    select @sqlValues = @sqlValues + cols + ' + '',' + '''' + char(13) + ','
            ,@sql = @sql + '''[' + name + '],''' + char(13) + ','
    from
    (
        select
            case
                when xtype in (48,52,56,59,60,62,104,106,108,122,127)
                -- 48 tinyint 52 smallint 56 int 59 real 60 money 62 float 104 bit 106 decimal 108 numeric 122 smallmoney 127 bigint
                    then 'case when ' + name + ' is null then ''NULL'' else ' + 'cast(' + name + ' as varchar)' + ' end'
                when xtype in (58,61)
                -- 58 smalldatetime 61 datetime
                    then 'case when ' + name + ' is null then ''NULL'' else ' + ''''''''' + ' + 'cast(' + name + ' as varchar)' + '+''''''''' + ' end'
                when xtype in (167,175)
                --  167 varchar 175 char	
                    then 'case when ' + name + ' is null then ''NULL'' else ' + ''''''''' + ' + 'replace(' + name + ','''''''','''''''''''')' + ' + ''''''''' + ' end'
                when xtype in (231,239)
                --231 nvarchar 239 nchar 
                    then 'case when ' + name + ' is null then ''NULL'' else ' + '''N'''''' + ' + 'replace(' + name + ','''''''','''''''''''')' + ' + ''''''''' + ' end'
                else '''NULL'''
            end as Cols
            ,name
        from syscolumns
        where id = object_id(@TableName)
                and autoval is null --忽略自動增長字段
    ) T
    --組最終sql成查詢語句并執行
    set @sql = 'select ''' + @sqlExist+''',' + char(13) + '''INSERT INTO ''' + char(13) + ','
                 + '''['+ @TableName + ']''' + char(13) + ','
                 + left(@sql,len(@sql)-4) + '''' + char(13) + ','')' + left(@sqlValues,len(@sqlValues)-7) + ','')'''
                 + char(13) + 'from [' + @TableName + ']'
                 + char(13) + @WhereOrderByClause

    print @sql -- print SQL 是完整正确的
    exec (@sql)

end

你可能感兴趣的:(sql,Go)