【问题标题】:Creating multiple CSV files using SQL使用 SQL 创建多个 CSV 文件
【发布时间】:2018-03-01 00:30:06
【问题描述】:

我在 SQL 中有一个 Table1。

ID | Provider | AdviserName   | PolicyNumber |
---|----------|---------------|--------------|
1  | Asgard   | John Smith    | A1           |
2  | Asgard   | Andrew Bailey | A2           |
3  | BT       | Tony Saw      | B1           |
4  | BT       | Greg Lee      | B2           |
5  | BT       | Jenny Main    | B3           |
6  | Zurich   | Beth Bond     | Z1           |
7  | Zurich   | Fang Li       | Z2           |
8  | Zurich   | Garry Low     | Z3           |

我需要将此表导出为 .CSV (Microsoft Excel) 并在每个提供商之间拆分。

Agard.csv

ID | Provider | AdviserName   | PolicyNumber |
---|----------|---------------|--------------|
1  | Asgard   | John Smith    | A1           |
2  | Asgard   | Andrew Bailey | A2           |

BT.csv

ID | Provider | AdviserName   | PolicyNumber |
---|----------|---------------|--------------|
3  | BT       | Tony Saw      | B1           |
4  | BT       | Greg Lee      | B2           |
5  | BT       | Jenny Main    | B3           |

苏黎世.csv

ID | Provider | AdviserName   | PolicyNumber |
---|----------|---------------|--------------|
6  | Zurich   | Beth Bond     | Z1           |
7  | Zurich   | Fang Li       | Z2           |
8  | Zurich   | Garry Low     | Z3           |

我尝试过使用 Foreach 循环容器,但是它不区分提供者。 (顺便说一句,我有 50 多个提供程序要分成不同的 .csv 文件)。

我使用的是 SQL Server 2012。感谢您的帮助。

【问题讨论】:

  • 你能显示你的代码不起作用吗?
  • SELECT DISTINCT * FROM Table1 A LEFT JOIN (SELECT DISTINCT Provider FROM Table1) AS B ON A.Provider = B.Provider ORDER BY A.Provider
  • 我将上述查询放入 SSIS 包中(在执行 SQL 任务中)。

标签: sql-server csv export-to-csv nested-loops


【解决方案1】:

您可以在 SQL 中执行此操作,您不需要使用 SSIS,它也可以,但是您可以在 SQL 中执行此操作。

首先确保启用了“xp_cmdshell”。

-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO

使用游标和 bcp.exe 实用程序可以获得所需的结果

DECLARE @provider VARCHAR(255), @cmd VARCHAR(512)
DECLARE curs CURSOR FOR
    SELECT DISTINCT Provider FROM Test.dbo.ExportProvider          

OPEN curs
FETCH NEXT FROM curs INTO @provider

WHILE @@FETCH_STATUS = 0
BEGIN
    SET @cmd = 'bcp.exe "SELECT Id, Provider, AdviserName, PolicyNumber FROM Test.dbo.ExportProvider WHERE Provider = ''' + @provider + '''" queryout "C:\Temp\' + @provider + '.csv" -c -t, -C RAW -T -S ' + @@SERVERNAME
    EXECUTE xp_cmdShell @cmd
    FETCH NEXT FROM curs INTO @provider
END

CLOSE curs 
DEALLOCATE curs

这将在 temp 文件夹中生成 3 个 .csv 文件。 希望这会有所帮助。

【讨论】:

  • 太棒了!!!它的工作凯文。谢谢你的帮助。我不再需要使用 SSIS 包及其脚本了。谢谢。
【解决方案2】:

请参阅下面的示例。这会做你想做的。

-- DROP TABLE Reporting_Table 
CREATE TABLE Reporting_Table (
       ID    varchar(10),
       IssueDate     Date,
       ExpirationDate    Date,
       Principal     Money,
       Interest  Money,
       Qtrs      Integer,
       Amount     Money)

INSERT INTO Reporting_Table (ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount)
VALUES ('1232949523', '01/01/2017', '12/31/2019', 1000000, .02, Null, Null); 

INSERT INTO Reporting_Table (ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount)
VALUES ('1232949523', '03/05/2017', '12/31/2018', 5200000, .01, Null, Null); 

INSERT INTO Reporting_Table (ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount)
VALUES ('1232949523', '03/31/2017', '06/01/2020', 4900000, .015, Null, Null); 

INSERT INTO Reporting_Table (ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount)
VALUES ('9567323294', '06/30/2017', '05/22/2019', 3500000, .02, Null, Null); 

INSERT INTO Reporting_Table (ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount)
VALUES ('9567323294', '09/17/2017', '11/21/2022', 2000000, .02, Null, Null);

INSERT INTO Reporting_Table (ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount)
VALUES ('9567323294', '01/01/2017', '12/31/2019', 1000000, .02, Null, Null); 

INSERT INTO Reporting_Table (ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount)
VALUES ('9967949523', '03/05/2017', '12/31/2018', 5200000, .01, Null, Null); 

INSERT INTO Reporting_Table (ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount)
VALUES ('9967949523', '03/31/2017', '06/01/2020', 4900000, .015, Null, Null); 

INSERT INTO Reporting_Table (ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount)
VALUES ('9967949523', '06/30/2017', '05/22/2019', 3500000, .02, Null, Null); 

INSERT INTO Reporting_Table (ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount)
VALUES ('9967949523', '09/17/2017', '11/21/2022', 2000000, .02, Null, Null);

SELECT ID, IssueDate, ExpirationDate, Principal, Interest, (Principal * Interest) As Amount
FROM Reporting_Table



select *, ntile(5) over(order by Principal) as tile_nr from Reporting_Table

Select *
From Reporting_Table


ALTER FUNCTION dbo.fxnExample (@Parameter1 NVARCHAR(255))
RETURNS TABLE
AS
RETURN
(
    SELECT ID, IssueDate, ExpirationDate, Principal, Interest, Qtrs, Amount
    FROM Reporting_Table
    WHERE id = @Parameter1
)


-- Usage Example
SELECT * FROM dbo.fxnExample('1232949523')   -- only data from '1'
SELECT * FROM dbo.fxnExample('9567323294')   -- only data from '2'
SELECT * FROM dbo.fxnExample('9967949523')   -- only data from '3'


-- SPLIT A TABLE INTO EQUAL HALFS


select *, ntile(2) over(order by ID) as tile_nr from Reporting_Table

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多