问题
I couldn't find an answer so far. What I want to do is to outfile my sql table into a .txt file or csv. (txt if possible)
What i try is :
SELECT * FROM [mydb] INTO OUTFILE 'd:\scripts\powershell\123.txt';
the return is :
Incorrect syntax near the keyword 'INTO'.
I also tried to select just some of the columns. Also tried to create the text file first...
I use SQL-Server-2012
回答1:
goto your Database right click
DataBase --> Right Click --> Tasks --> ExportData
Sql Server Import/Export Wizard
will open. Provide all the information like DataSource , ServerName and Databases Name, Table Name, Click Next and then provide all the information about the destination .
回答2:
The best option would be the SSIS Import/Export Wizard with a query source and the quickest and easiest way to do that is described in Muhammed Ali's answer.
But if that is not an option, in theory you could achieve this by combining xp_cmdshell with SQLCMD, although I haven't tried it myself.
You can find information on xp_cmdshell here, there is an excellent article by Pinal Dave that describe what you are attempting using SQLCMD from the admin command prompt you can read it here.
You may end up with something along the lines of this:
EXEC master..xp_cmdshell 'SQLCMD -S YourSQLServer -d YourDatabase -U YourUserName -P YourPassword -Q “Your Query” -s “,” -o “C:*Yourfilename*.csv”', no_output
If I get chance this weekend I'll have a play around and come back and expand this answer, if not good luck, I hope this helps.
来源:https://stackoverflow.com/questions/20270674/how-to-use-sql-outfile-command-correctly