Is There a Select ... into Outfile Equivalent in SQL Server Management Studio

Is there a SELECT ... INTO OUTFILE equivalent in SQL Server Management Studio?

In SSMS, "Query" menu item... "Results to"... "Results to File"

Shortcut = CTRL+shift+F

You can set it globally too

"Tools"... "Options"... "Query Results"... "SQL Server".. "Default destination" drop down

Edit: after comment

In SSMS, "Query" menu item... "SQLCMD" mode

This allows you to run "command line" like actions.

A quick test in my SSMS 2008

:OUT c:\foo.txt
SELECT * FROM sys.objects

Edit, Sep 2012

:OUT c:\foo.txt
SET NOCOUNT ON;SELECT * FROM sys.objects

OUTFILE Microsoft SQL Server equivalent?

This code causes the query results to be dumped into a text file.

EXEC master..xp_cmdshell'bcp "SELECT TOP 5 CUSTOMERID FROM Northwind.dbo.Customers" queryout "c:\text.txt" -c -T -x'

References:

  • Code Samples
  • BCP Utility Reference
  • xp_cmdshell Reference

Select Into Outfile incorrect syntax?


SELECT * INTO OUTFILE "C:\datadump\sqldbdump.txt"
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\r\n'
FROM dbo.alarms_2_2014;

try this one it should work perfectly.

How to save MSSQL query output to .txt or .json file?

As per the original question, which was tagged as MySQL: Your syntax is wrong. As per MySQL Documentation, FROM clause comes after INTO OUTFILE clause. Following should work in MySQL:

SELECT * INTO OUTFILE '/tmp/orders.txt'
FROM A

In order to get Comma-separated values in the text file, you can do the following instead (in MySQL):

SELECT * INTO OUTFILE '/tmp/orders.txt' 
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM A;

For MS SQL Server: There is no INSERT INTO OUTFILE clause available. Instead, a different solution is proposed using SQL Server Management Studio at: https://stackoverflow.com/a/6354143/2469308

SQL Server SELECT INTO @variable?

You cannot SELECT .. INTO .. a TABLE VARIABLE. The best you can do is create it first, then insert into it. Your 2nd snippet has to be

DECLARE @TempCustomer TABLE
(
CustomerId uniqueidentifier,
FirstName nvarchar(100),
LastName nvarchar(100),
Email nvarchar(100)
);
INSERT INTO
@TempCustomer
SELECT
CustomerId,
FirstName,
LastName,
Email
FROM
Customer
WHERE
CustomerId = @CustomerId


Related Topics



Leave a reply



Submit