Use "Enter" as column or field separator in SQLCMD - sql

What I need to do:
I want to export data from SQL Server 2008 to a CSV in specific format.
I can't use the BCP utility for this.
I have exported CSV File using SQLCMD but the only issue is I need "Enter" as column or field separator in CSV file.
Then I saved SQLCMD file as windows batch file and executed that using CMD
For Example, (Note: test.sql contains a PLSql Block which return columns data.):
I have saved below statement as batch file with name test.bat:
sqlcmd -S %1 -U %2 -P %3 -v DatabaseName = %4 -i %current_dir%\test.sql -o %current_dir%\test.csv -y0 -I -s "
"
Then executed above in CMD using below command:
test.bat localhost user pass db_name
But it gives output like:
""REC"""LINE"
I want output like:
"Rec"
"LINE"
Any input is appreciated!

I used this syntax in my table and when i opened the file it gived to me some kind of the result you are asking for. Just try it if it is what you are requiring.
sqlcmd -S .\SQLEXPRESS -d Atrax_BazaRM -E -Q "select * from Njesite" -o "E:\MyData.csv" -h-1 -s "
"

[Too long for comment]
You have in your list I can't use the BCP utility for this.. In the comment section you replied BCP gives error of xp_cmdshell on our client..
You know you can enable use of xp_cmdshell, right? I'm not saying you have to and possibly the client won't agree. You can enable xp_cmdshell by:
-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO
After that you can use BCP and possibly this will make your problem easier to solve?

Related

Cannot execute script: Insufficient memory to continue the execution of the program

I have a 123MB sql file which I need to execute in my local PC. But I am getting
Cannot execute script: Insufficient memory to continue the execution of the program
How to solve this issue?
use the command-line tool SQLCMD which is much leaner on memory. It is as simple as:
SQLCMD -d <database-name> -i filename.sql
You need valid credentials to access your SQL Server instance or even to access a database
Taken from here.
It might help you! Please see below steps.
sqlcmd -S server-name -d database-name -i script.sql
Open cmd.exe as Administrator.
Create Documents directory.
Put your SQL Script file(script.sql) in the documents folder.
Type query with sqlcmd, server-name, database-name and script-file-name as like above highlighted query or below command line screen.
For Windows Authentication use this sql cmd
SQLCMD -S TestSQLServer\SQLEXPRESS -d AdventureWorks2018 -i "d:\document\sql document\script.sql"
Note: If there is any space in the sql file path then use " (Quotation marks) "
For SQL Server Authentication use this sql cmd
SQLCMD -S TestSQLServer\SQLEXPRESS -U sa -P sasa -d AdventureWorks2018 -i "d:\document\sql document\script.sql"
-S TestSQLServer\SQLEXPRESS: Here specify SQL Server Name
-U sa: Username (in case of SQL Server Authentication)
-P sasa: Password (in case of SQL Server Authentication)
-d AdventureWorks2018: Database Name come here
-i "d:\document\sql document\script.sql": File Path of SQLFile
You can also simply increase the Minimum memory per query value in server properties. To edit this setting, right click on server name and select Properties > Memory tab.
I encountered this error trying to execute a 30MB SQL script in SSMS 2012. After increasing the value from 1024MB to 2048MB I was able to run the script.
(This is the same answer I provided here)
My database was larger than 500mb, I then used the following
C:\Windows>sqlcmd -S SERVERNAME -U USERNAME -P PASSWORD -d DATABASE -i C:\FILE.sql
It loaded everything including SP's
*NB: Run the cmd as Administrator
If I understand your problem correctly, you are trying to restore (transact sql) xyz.sql - database + schema. You can try this command which worked for me:
SQLCMD -U sa -i xyz.sql
Try this step,
1)Open PowerShell
2)Write this command:
sqlcmd -S PCNAME\SQLEXPRESS -U user -P password -d databanse_name -i C:\script.sql
3)Press Return
:-)
Below script works perfectly:
sqlcmd -s Server_name -d Database_name -E -i c:\Temp\Recovery_script.sql -x
Symptoms:
When executing a recovery script with sqlcmd utility, the ‘Sqlcmd: Error: Syntax error at line XYZ near command ‘X’ in file ‘file_name.sql’.’ error is encountered.
Cause:
This is a sqlcmd utility limitation. If the SQL script contains dollar sign ($) in any form, the utility is unable to properly execute the script, since it is substituting all variables automatically by default.
Resolution:
In order to execute script that has a dollar ($) sign in any form, it is necessary to add “-x” parameter to the command line.
e.g.
Original:
sqlcmd -s Server_name -d Database_name -E -i c:\Temp\Recovery_script.sql
Fixed:
sqlcmd -s Server_name -d Database_name -E -i c:\Temp\Recovery_script.sql -x
Sometimes, due to the heavy size of the script and data, we encounter this type of error. Server needs sufficient memory to execute and give the result. We can simply increase the memory size, per query.
You just need to go to the sql server properties > Memory tab (left side)> Now set the maximum memory limit you want to add.
Also, there is an option at the top, "Results to text", which consume less memory as compare to option "Results to grid", we can also go for Result to Text for less memory execution.
sqlcmd -S mamxxxxxmu\sqlserverr -U sa -P x1123 -d QLDB -i D:\qldbscript.sql
Open command prompt in run as administrator
enter above command
"mamxxxxxmu" is computer name
"sqlserverr" is server name
"sa" is username of server
"x1123" is password of server
"QLDB" is database name
"D:\qldbscript.sql" is sql script file to execute in database
If you need to connect to LocalDB during development, you can use:
sqlcmd -S "(localdb)\MSSQLLocalDB" -d dbname -i file.sql
As in most answers given here use the command-line tool. In my case the script already has database creation code. If your script contains CREATE DATABASE command, for example
USE [master]
GO
CREATE DATABASE [your-database-name]
Then do not use the -d your-database-name, instead use the following command.
For Windows Authentication use the command
sqlcmd -S ServerName\InstanceName -i "script.sql" -x
For SQL Server Authentication use the command
sqlcmd -S ServerName\InstanceName -U usename -P password -i "script.sql" -x

Executing series of SQL commands on the command line

I want to run a series of SQL statements against a SQL Server 2005 database from the command line.
When I launch 1st statement
osql -E -S <Server_Name>\<Instance_Name> -d <Server_Name>
it is going to prompt window 1> from there after I am unable to proceed further through script.
How to give input to 1> prompt I mean giving next SQL statement
BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP
and finally exit to that prompt
I tried with && but I guess that is for only commandline commands.
You ae looking for the -Q switch on the sqlcmd tool (don't use osql on sqlserver 2005 or higher) (type sqlcmd /? to see all options) or lookit up on msdn
sqlcmd -E -S <Server_Name>\<Instance_Name> -d <Server_Name> -Q "BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP"
Alternatively you can create a sqlscript file where you put all the sql statements in you want to execute. Assuming you name your file myscript.sql the osql command would go like this:
sqlcmd -E -S <Server_Name>\<Instance_Name> -d <Server_Name> -i myscript.sql
Perhaps you may want to try a small trick that emerged from other question in this forum (that was deleted unfortunately).
You may insert the input for a command directly in the lines below the command and then execute the file NOT as Batch file, but as input por cmd.exe (this is similar to a here document in Linux). For example:
script.TXT:
#echo off
osql -E -S <Server_Name>\<Instance_Name> -d <Server_Name>
BACKUP DATABASE TO DISK = 'c:\test.bak' WITH INIT,SKIP
exit
Execute previous "script" this way:
cmd < script.TXT
If you perform this test, please report the result...
Antonio

Can I specify an input sql file with bcp?

How can I specify an input sql file with a long query when using bcp? I tried using the -i option but it keeps complaining about a command-line error with no extra information. Is this possible?
I had this problem today and found a convenient workaround, at least in an ad-hoc situation.
Temporary tables can be created by any user with connect permissions. This means you can also create GLOBAL temporary tables.
Just run your query in enterprise manager (or sql cmd or whatever) using SELECT ...INTO with a global temporary table e.g.
SELECT *
INTO ##mytemptable
FROM SomeTable
WHERE [massive where clause, for example]
You can then use the temporary table in the BCP query with a simple
SELECT * FROM ##mytemptable
Then drop the temp table through enterprise manager
DROP TABLE ##mytemptable
I did other way for fix that.
I create a batch file which read a file and send your content in bcp command. See:
#ECHO off
SETLOCAL EnableDelayedExpansion
SET queryFile=%1
SET outFileName=%2
FOR /F "delims=" %%i IN (%queryFile%) DO SET join=!join! %%i
ECHO %join%
bcp "%join%" queryout %outFileName% /S.\SQLE_CAESAR /d /c /t"|" /T
That script receive two parameters:
Filename which has a query;
Filename for export data;
Execute a script in cmd like that:
export-query.bat query.sql export.txt
I hope helped.
As far as I'm concerned the BCP utility only supports Transact-SQL queries directly written to the command line. Ex:
bcp "SELECT Name FROM AdventureWorks.Sales.Currency" queryout Currency.Name.dat -T -c
According to its reference the "-i" option:
Specifies the name of a response file, containing the responses to the command prompt questions for each data field when a bulk copy is being performed using interactive mode (-n, -c, -w, or -N not specified).
Notice that it differs from the sqlcmd Utility "-i" option:
Identifies the file that contains a batch of SQL statements or stored procedures. Multiple files may be specified that will be read and processed in order (...)
try :
query=$( cat < /file.sql )
export query
bcp "${query}" queryout /home/file.csv
Multi-line queries can be given to bcp easily using powershell:
PS> $query = #'
select *
from <table>
'#
PS> bcp $query queryout <outfile> -d <database> -T -S <server> -c
I had face same issue, may not be a very good approach. However, I did something like the following
bcp "declare #query nvarchar(max) set #query = (SELECT * FROM OPENROWSET(BULK 'F:\tasks\report_v2.sql', SINGLE_CLOB) AS Contents) exec sp_executesql #query" queryout %outFileName% /c /C RAW -S . -U sa -P 123 -d blog /T
And I must say, if you use like global temp table then global temp table is dropped itself of after query executed. you can't use this at some situations
What really worked for me is this:
#ECHO off
setlocal enableextensions enabledelayedexpansion
SET "queryFile=%1"
SET "outFileName=%2"
SET RESULT=
FOR /F "delims=" %%i IN ('type %queryFile%') DO SET RESULT=!RESULT! %%i
echo %RESULT%
rem bcp "%RESULT%" queryout %outFileName% -t^ -r \n -T -k -c -d DB_NAME -S SERVER_NAME
type file is the equivalent of cat file in unix
What I did with complex queries was create a stored procedure with the desired statement and call it from BCP:
bcp "exec db.schema.stored_procedure" queryout "c:\file.txt" -T -S localhost -t "|" -c
This worked great for me. Greetings!
I made my own script (called of bulk.sh) to do this (not optimal and not best practice... The script is too ugly, but very functional).
#!/bin/bash
input="SQL_FILE.sql"
count=0
const=1000
lines=()
mkdir -p bulk
while IFS= read -r line
do
lines+=("$line")
count=$((count+1))
check=$((count % const))
if [[ $check -eq 0 ]]; then
bulk="${lines[*]}"
unset lines
number=$(printf "%010d" $count)
echo $bulk > "bulk/bulk${number}.sql"
bulk=""
fi
done < "$input"
FILES="bulk/*"
for f in $FILES
do
echo "Processing $f file..."
sqlcmd -S SERVER -d DATABASE -U USER -P "PASSWORD" -i "$f"
sleep 2s
done
You can try it, with:
$ docker run -v /path/to/your/sql/file/folder:/backup -it mcr.microsoft.com/mssql-tools
$ bash bulk.sh

How to save query result using T-SQL in a csv file?

How to save query result using T-SQL in a csv file in SSMS 2008? Needs to be done programmatically.
You could use System.Diagnostics.Process to run the SQLCMD or OSQL or BCP programs to dump out csv files.
Here is an example I found from google. Play around with the parameters.
EXEC master..xp_cmdshell 'sqlcmd -X -S server -q "SET NOCOUNT ON Select 'department','FundCode','Month' SELECT * FROM pubs.dbo.test" -s , -w 255 -u -o "\\server\output.csv" -h-1'

SQL Statements in a Windows Batch File

Is there any way to have a Windows batch file directly input SQL statements without calling a script? I want the batch file to login to SQL and then enter in the statements directly.
EDIT: I'm using Oracle v10g
For a single command you can use this trick:
echo select * from dual; | sqlplus user/pw#db
To run something on SQL server 2005/2008, you could use sqlcmd command line utility. sqlcmd -h prints the list of switches.
Short answer: No. Batch files by themselves can't do this.
Long answer: You may be able to come close, depending on which kind of database server you're using, and what the capabilities the commandline client provides.
What kind of database server are you using? Oracle, mySql, Sybase, Microsoft, Terradata, ???
For example, with a Sybase database, you can use the isql commandline client to run from a batch file:
isql -S server -D database -U user -P password -i script
You could use sqlcmd (for sql server) or System.Data.Odbc.OdbcCommand from powershell.
The odbc command of the outwit tool suite allows you to run select statements on any database for which an appropriate ODBC data source has been defined.
You can use http://tekkies.co.uk/go/runsqloledb
e.g.
RunSQLOLEDB "Provider=SQLOLEDB;Data Source=(local);..." "SELECT GetDate()"
or
RunSQLOLEDB #ConnectionString.txt #Query.sql
Here is a rough example script for MSSQL which may be able to be modified for Oracle:
#ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
:: batch file for sql query
SET STARTDATE=20101010
SET ENDDATE=20111109
SET AGENCYNAME=Agency
SET DBNAME=AccidentDB
SET SQLSTRING=SELECT Acc.INC_ID,^
Veh.MAKE, Veh.MODEL, Veh.LIC_NUM^
FROM Acc,^
lnk_Acc_Veh, Veh^
WHERE (INC_NUM LIKE '20115000%')^
AND lnk_Acc_Veh.link_id=Veh.key^
AND lnk_Acc_Veh.link_id=Acc.key^
AND Acc.date ^> '%STARTDATE%' OR Acc.date ^< '%ENDDATE%';
CLS
#ECHO.
#ECHO.
#ECHO DBNAME is %DBNAME%
#ECHO.
#ECHO SQLSTRING is "!SQLSTRING!"
#ECHO.
#ECHO ------------------------------------------------------
#sqlcmd.exe -b -S localhost -E -d !DBNAME! -Q "!SQLSTRING!" -W
#ECHO.
#ECHO Report is done. Hit any key to close this window....
#pause>nul