Removing quotes added to column names from Excel import SQL Server 2008

前端 未结 2 2046
清酒与你
清酒与你 2021-01-24 06:50

I\'ve noticed that when I use SSMS to import an Excel spreadsheet into SQL Server quotation marks are added. I\'ve read somewhere that for whatever reason it\'s necessary for E

2条回答
  •  逝去的感伤
    2021-01-24 07:15

    I believe this should help...

    DECLARE @tbl sysname, @col sysname
    DECLARE @cmd nvarchar(max)
    
    DECLARE cCol CURSOR FOR
        SELECT TABLE_NAME, COLUMN_NAME
        FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE COLUMN_NAME LIKE '"%"'
    
    OPEN cCol
    FETCH NEXT FROM cCol INTO @tbl, @col
    WHILE @@fetch_status = 0
    BEGIN
      SET @cmd = 
        N'EXEC sp_rename ''[' + @tbl + '].[' + @col + ']'', ' + 
        '''' + REPLACE(@col, '"', '') + N''', ''COLUMN''' 
    
      --PRINT @cmd
    
      EXEC sp_executeSQL @cmd
    
      FETCH NEXT FROM cCol INTO @tbl, @col
    END
    
    CLOSE cCol 
    DEALLOCATE cCol
    

提交回复
热议问题