• 导出表数据的存储过程


    创建一个导出表数据的存储过程

    CREATE   PROC [DBO].[SPGENINSERTSQL] (@TABLENAME VARCHAR(256))
    AS
    BEGIN
    DECLARE @SQL VARCHAR(8000)
    DECLARE @SQLVALUES VARCHAR(8000)
    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
    SET @SQL ='SELECT ''INSERT INTO ['+ @TABLENAME + ']' + LEFT(@SQL,LEN(@SQL)-1)+') ' + LEFT(@SQLVALUES,LEN(@SQLVALUES)-4) + ')'' FROM '+@TABLENAME
    PRINT @SQL
    EXEC (@SQL)
    END
  • 相关阅读:
    JAVA第六次作业
    20194672自动生成四则运算题第一版报告
    20194672自动生成四则运算第一版报告
    第四次博客作业--结对项目
    第9次作业--接口及接口回调
    第8次作业--继承
    软件工程第三次作业——关于软件质量保障初探
    第7次作业——访问权限、对象使用
    第6次作业--static关键字、对象
    Java输出矩形的面积和周长
  • 原文地址:https://www.cnblogs.com/liwenyan/p/2541758.html
Copyright © 2020-2023  润新知