我在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 |
Zurich.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.感谢您的帮助。
您可以在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
这将导致临时文件夹中有3个.csv文件。希望这可以帮助。
请参阅下面的示例。这将做你想要的。
-- 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