如何使用 OPENROWSET 导出以分号分隔的数据?
How to export data delimited by semicolon using OPENROWSET?
我正在使用以下查询将数据导出到文件中:
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=C:\shared_files;HDR=YES;FMT=Delimited','SELECT * FROM [existing_file.csv]')
SELECT field_A, field_B, field_C FROM myTable
执行后,文件(C:\shared_files\existing_file.csv)中的结果为:
myField_A,myField_B,myField_C
"1","customer 01","address 01"
"2","customer 02","address 02"
"3","customer 03","address 03"
我想获取以分号分隔,不带引号,如下例:
myField_A,myField_B,myField_C
1;customer 01;address 01
2;customer 02;address 02
3;customer 03;address 03
我研究过,但没有找到 OPENROWSET 选项的语法。
https://docs.microsoft.com/pt-br/sql/t-sql/functions/openrowset-transact-sql?view=sql-server-ver15
How to use OpenRowSet to insert data into a blank file?
INSERT INTO OPENROWSET Syntax with Dynamic T-SQL
如有必要,可以更改文件扩展名。
有谁知道如何在上面的 OPENROWSET 中设置选项以获得所需的结果?
我构建的解决方案不需要使用 OPERNROWSET,正如@Aaron Bertrand 为我澄清的那样。
-- in the first line of the file to be created
INSERT INTO some_table_to_get_data
SELECT 'colName_A;colName_B;colName_C' --insert a customized header, using any desired delimiter
-- then insert data, using desired formatting and converting to varchar to concatenate it
INSERT INTO some_table_to_get_data
SELECT
CONVERT(varchar(1000),field_A)
+ ';' + LTRIM(RTRIM(CONVERT(varchar(1000),field_B)))
+ ';' + LTRIM(RTRIM(CONVERT(varchar(1000),field_C)))
+ ';' + LTRIM(RTRIM(CONVERT(VARCHAR, field_D, 103)))
FROM myTable
DECLARE @myFileName VARCHAR(100)
DECLARE @cmd VARCHAR(8000)
SET @myFileName = 'desiredNameForMyFile.txt'
SELECT @cmd = 'sqlcmd -h-1 -W -S myServerName -d myDatabaseName -U myLoginName -P myPasswordName -Q "SET NOCOUNT ON; select dados from dbo.some_table_to_get_data" -o "C:\mySharedPath\' + @myFileName + '" '
--PRINT(@cmd)
EXEC MASTER..xp_cmdshell @cmd
我正在使用以下查询将数据导出到文件中:
INSERT INTO OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=C:\shared_files;HDR=YES;FMT=Delimited','SELECT * FROM [existing_file.csv]')
SELECT field_A, field_B, field_C FROM myTable
执行后,文件(C:\shared_files\existing_file.csv)中的结果为:
myField_A,myField_B,myField_C
"1","customer 01","address 01"
"2","customer 02","address 02"
"3","customer 03","address 03"
我想获取以分号分隔,不带引号,如下例:
myField_A,myField_B,myField_C
1;customer 01;address 01
2;customer 02;address 02
3;customer 03;address 03
我研究过,但没有找到 OPENROWSET 选项的语法。
https://docs.microsoft.com/pt-br/sql/t-sql/functions/openrowset-transact-sql?view=sql-server-ver15
How to use OpenRowSet to insert data into a blank file?
INSERT INTO OPENROWSET Syntax with Dynamic T-SQL
如有必要,可以更改文件扩展名。
有谁知道如何在上面的 OPENROWSET 中设置选项以获得所需的结果?
我构建的解决方案不需要使用 OPERNROWSET,正如@Aaron Bertrand 为我澄清的那样。
-- in the first line of the file to be created
INSERT INTO some_table_to_get_data
SELECT 'colName_A;colName_B;colName_C' --insert a customized header, using any desired delimiter
-- then insert data, using desired formatting and converting to varchar to concatenate it
INSERT INTO some_table_to_get_data
SELECT
CONVERT(varchar(1000),field_A)
+ ';' + LTRIM(RTRIM(CONVERT(varchar(1000),field_B)))
+ ';' + LTRIM(RTRIM(CONVERT(varchar(1000),field_C)))
+ ';' + LTRIM(RTRIM(CONVERT(VARCHAR, field_D, 103)))
FROM myTable
DECLARE @myFileName VARCHAR(100)
DECLARE @cmd VARCHAR(8000)
SET @myFileName = 'desiredNameForMyFile.txt'
SELECT @cmd = 'sqlcmd -h-1 -W -S myServerName -d myDatabaseName -U myLoginName -P myPasswordName -Q "SET NOCOUNT ON; select dados from dbo.some_table_to_get_data" -o "C:\mySharedPath\' + @myFileName + '" '
--PRINT(@cmd)
EXEC MASTER..xp_cmdshell @cmd