我在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.感谢您的帮助。
答案 0 :(得分:1)
您可以在SQL中执行此操作,不需要使用SSIS,它也可以使用,但是您可以在SQL中执行此操作。
首先确保' xp_cmdshell'已启用。
> [2] + [4]
"24"
> + [2] + [4]
"24"
> (+[2]) + (+[4])
6
> +[2] + [4,5]
"24,5"
> // which, incidentally, looks like a european-formatted number, but I see
> // that that's just an accident (still another possibility for confusion)
使用游标和bcp.exe实用程序,您可以获得所需的结果
-- 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
这将导致临时文件夹中有3个.csv文件。 希望这会有所帮助。
答案 1 :(得分:0)
请参阅下面的示例。这将做你想要的。
-- 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