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
SQL Error: Ora-00933: SQL Command Not Properly Ended
Nested Select Statement in SQL Server
How to Compare Time in SQL Server
Sqlite Select with Condition on Date
Procedure or Function !!! Has Too Many Arguments Specified
How to Import .SQL Files into SQLite 3
How to Execute an in Lookup in SQL Using Golang
Benefits of Using SQL Ordinal Position Notation
Call Stored Procedure with Table-Valued Parameter from Java
Performance of SQL Server 2005 Query
SQL Update Order of Evaluation
Generate SQL Insert Script from Excel Worksheet
What Is a Simple and Efficient Way to Find Rows with Time-Interval Overlaps in SQL
SQL Use Alias in Where Statement
Why Doesn't SQL Support "= Null" Instead of "Is Null"
Display Names of All Constraints for a Table in Oracle SQL
Select Rows Where Column Value Has Changed
Sql: How to Select Only Groups That Do Not Contain a Certain Value