OUTFILE Microsoft SQL Server equivalent? - sql

Is there something like INTO...OUTFILE (from MySQL) in Microsoft SQL Server?

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

You can also use the sqlcmd or osql (in SQL 2000) to store the output from a command line outside of SSMS very like working with mysql from the command line. You just have to provide the correct parameters, including the query you want to run.

It depends on how fancy you want to get. You can use SSMS to generate a CSV A less user-friendly option is a BCP export. Which I see #hamlin11 just suggested

Related

Export to CSV in stored procedure in SQL Server 2008

I have a stored procedure that creates a temp table and has tons of data. I have tried both of the following. I can do it manually with the export utility or results to file settings, but I would like to embed the code in my stored procedure so I can schedule the batch job and forget it. Also I do not want to us SSIS for many reasons.
Popular solution I have found are :
bcp "select * from WHSE.Customer" queryout ExcelTest.csv -t, -c -S . -d Server1 -T
SQLCMD -S . -d Server1 -Q “"select * from WHSE.Customer sp” -s “,” -o “d:\result.csv
In the BCP it get an error on BCP, if I added an EXECUTE in front I get an error:
Incorrect syntax near 'queryout'.
The same goes with the SQLCMD except error on SQLCMD or -S depending on if I use the Execute command.
This seems like it should be simple and I have found TONS of answers, but none actually work. It seems like it should be the most basic thing in the world, SELECT * INTO MYFILE.CSV FROM MYTABLE essentially.
You are seeing "Incorrect syntax near queryout". That is a T-SQL error message. You are ,as you said, running this from within a stored procedure. Both BCP and SQLCMD are command line utilities, meaning you will have to run them from a command (DOS) prompt.
There is a way to execute directly from SQL, but you will have to use: xp_cmdshell Here is a good article on how to use it:
https://www.mssqltips.com/sqlservertip/1633/simple-way-to-export-sql-server-data-to-text-files/
This was written for SQL Server 2005 but should work on 2008 as well.

Running SQL Files with Variety of DDL commands and INSERT operations from a VB6 Code

I have a VB6 application, which restores a BAK file to the Database and runs through some scripts.
As there are large volume of SQL Scripts to run, I decided to move all in to a SQL File and then to run it from the Application.
The SQL File is now filled with lots of CREATE, UPDATE Table statements and also INSERT statements. I use both the SQL Server 2008 R2 and SQL Server 2012.
Is there a way or function to execute the same from the VB6 code?
Thanks in advance for the help.
EDIT 1: The SQL File also have the CREATE Procedures and Triggers
You can run SQL scripts from the command line using
sqlcmd -S myServer\instanceName -i C:\myScript.sql
(see https://msdn.microsoft.com/en-us/library/ms170572.aspx)
In VB6 you can execute command line strings using the Shell command, for example to start the Windows Calculator:
Shell "C:\WINDOWS\System32\calc.exe", vbNormalFocus
For more on the Shell command see: https://msdn.microsoft.com/en-us/library/aa242087.aspx and How do I call a Windows shell command using VB6?
The combination should give you what you need, like this:
Shell "sqlcmd -S myServer\instanceName -i C:\myScript.sql", vbNormalFocus
I see there is a good amount of useful information about SQLCMD in this article https://www.simple-talk.com/sql/sql-tools/sql-server-sqlcmd-basics/ which may be a useful read.

Save output of a SP to a file and create a job to execute it

I have a SP which returns a XML string as output. I want to save the result in a .xml file automatically when the SP is executed. whats the best way to do that?
First, saving output to file:
exec xp_cmdshell 'bcp "select * from suppliers" queryout "c:\suppliers.txt" -S server -T'
Second, Scheduling a SQL Job
http://msdn.microsoft.com/en-us/library/ms190268.aspx
Alternatively (in case of SQL Express)
Use command-line SQL to execute the stored procedure from a windows task, scheduled accordingly
Build a quick .NET application that executes the stored procedure. Then, setup a windows task to run the executable on a schedule.
This might be exactly what you are looking for: http://munishbansal.wordpress.com/2009/02/20/saving-results-of-a-stored-procedure-into-a-xml-file/
In a nutshell, you have four options:
Using CLR Stored Procedure.
Using Command Line Utility (OSQL).
Using xp_CmdShell utility of SQL Server.
Creating OLE objects in SQL Server (sp_OACreate).
I will not paste most of that article here, but it is pretty well written.

No way to execute SQL script from SQL Server Query Manager like #{file.sql} oracle sqlplus syntax?

Like the title says, in oracle you can issue the following command in SQL*Plus:
SQL> select something from anothertable; #sql
SQL> #{/home/me/somescript.sql}; #load sql from file and execute it
SQL> do something else in script; #other sql
Without having to file->open the sql script to load it to the UI.
Is there an equivalent in SQL Server Query Manager? I've stumbled upon many situation where i could have used it but i couldn't be able to find a way to accomplish it.
You're not really comparing like for like Tools here.
The equivalent tool to SQL*Plus in SQL Server is the SQLCMD Utility.
In particular you will be interested in the -i switch as this allows you to provide a .sql file as input.
Edit:
In response to your comment, you could look to use the system stored procedure xp_cmdshell to launch a prompt form within a T-SQL batch that allows you to use SQLCMD. Not the most elegant solution in my opinion but it should work.
If using latter versions of SQL Server (2005 & 2008), see if the :r command in SQLCMD works for you:
:r <filename>
Parses additional Transact-SQL statements and sqlcmd commands from the file specified by <filename> into the statement cache.
If the file contains Transact-SQL statements that arenot followed by GO, you must enter GO on the line that follows :r.
From sqlcmd Utility
Use isql utility http://msdn.microsoft.com/en-us/library/aa214007(SQL.80).aspx
issql ... -iinputfile
There is also SQLS*Plus tool that you can use to execute scripts within a script

SQLCMD: Prompt for Variable?

Coming from an Oracle background, Oracle's SQLPlus would let you indicate a variable. If the variable wasn't set, you'd be prompted to provide a value.
I'm using SQLCMD, using the $([var_name]) syntax. In SSMS SQLCMD mode, I get:
A fatal scripting error occurred.
Variable tbl_name is not defined.
...for trying to run:
SELECT COUNT(*) FROM $(tbl_name)
Does SQLCMD provide the same functionality as SQLPlus? If so, what am I doing wrong?
SQLCMD does not support prompting for missing variable names. However, you can use SSMS in SQLCMD mode. Dunno how you're error was caused, but this works for me:
:setvar table "sys.tables"
SELECT * FROM $(table);
There is a free tool "SQLS*Plus" which is an SQL*Plus for SQL Server. Works with SQL Server 2000/2005 and 2008
Very flexible with data formatting (set lines size, pagesize, etc), variables (&, &&), spool, etc - light years better than isql, osql or sqlcmd
I downloaded SQLS*Plus from http://www.memfix.com