Is there a way I can specify output file format in SQLCMD mode so that I can read it back using BULK INSERT
? I want to do something like this:
:CONNECT SERVER1
:OUT 'E:\test'
SELECT TOP 100 ID, NAME
FROM DB1.dbo.TABLE1
GO
:CONNECT SERVER2
:OUT 'E:\test2'
CREATE TABLE #TEMP(ID INT, NAME VARCHAR(100))
BULK
INSERT #TEMP
FROM 'E:\test'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
SELECT *
FROM DB2.dbo.TABLE2
WHERE ID IN (SELECT ID FROM #TEMP)
-- Other queries for #TEMP
GO
One way to get the data bulk inserted in a somewhat automated fashion is to use an intermediate table.
(using your original scenario)
Here's a good example for using sqlcmd to generate a CSV file:
http://www.siusic.com/wphchen/sqlcmd-example-to-output-in-csv-format-350.html