Removing quotes added to column names from Excel import SQL Server 2008(从 Excel 导入 SQL Server 2008 中删除添加到列名的引号)
问题描述
我注意到当我使用 SSMS 将 Excel 电子表格导入 SQL Server 时添加了引号.我在某处读到过,无论出于何种原因,Excel 都需要这样做.一旦在 SQL Server 中,列名周围的这些引号就没有用了,我想有一种编程方式来删除它们.我试图做的最接近的事情是 EXEC sp_rename 'Table.["withquotes"]', NewColumnName, 'replace(Table.["withquotes",'"',''].我想遍历表中的所有列名,并在这些列名包含引号的任何地方使用 replace 函数.有没有一种典型的惯用方法这样做吗?
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 Excel to do this. Once in SQL Server, these quotes around the column names are useless and I'd like to have a programmatic way to remove them. The closest thing, which doesn't work, that I have tried to make is EXEC sp_rename 'Table.["withquotes"]', NewColumnName, 'replace(Table.["withquotes",'"','']. I'd like to loop through all of the column names in a table and use the replace function wherever a those column names contain quotation marks. Is there a typical, idiomatic way to do this?
推荐答案
我相信这应该会有所帮助...
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
这篇关于从 Excel 导入 SQL Server 2008 中删除添加到列名的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 Excel 导入 SQL Server 2008 中删除添加到列名的引
基础教程推荐
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
