running SQL command from batch file and getting error - sql-server

Good morning everyone. Wondering if anyone can help me out. I am trying to write a batch file to make my life and others lives easier in my job but wanted to seek out some help. I typically run a bunch of different SQL scripts that I wrote to install a particular installation and decided life would be easier if i moved these all to batch files. I am trying to run the following command from a batch file:
:T02
cls
echo Qualifiers will be installed next with names of
echo Q_AutoFax_CC_1
echo Q_AutoFax_CC_2
echo Q_AutoFax_CC_3
echo Q_AutoFax_CC_4
echo Q_AutoFax_CC_5
pause
cls
If Exist "C:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe" cd "C:\Program Files\Microsoft SQL Server\100\Tools\Binn"
If Exist "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\bcp.exe" cd "C:\Program Files\Microsoft SQL Server\90\Tools\Binn"
If Exist "C:\Program Files\Microsoft SQL Server\80\Tools\Binn\bcp.exe" cd "C:\Program Files\Microsoft SQL Server\80\Tools\Binn"
sqlcmd /U eiw_admin /P eiw_admin /d cabinet /S . /Q
"declare #q int
select #q = 1
While #q < 6
Begin
Insert into cabinet..qualifiers
select 'HL7', 'Q_AutoFax_CC_'+ convert(varchar(2),#q),
'%Physician_Code'+ convert(varchar(2),#q)+'% == (NULL) || %Physician_Code'+ convert(varchar(2),#q)+'% == (BLANK)', 'R'
select #q = #q + 1
End"
pause
cls
I typically run just:
declare #q int
select #q = 1
While #q < 6
Begin
Insert into cabinet..qualifiers
select 'HL7', 'Q_AutoFax_CC_'+ convert(varchar(2),#q),
'%Physician_Code'+ convert(varchar(2),#q)+'% == (NULL) || %Physician_Code'+ convert(varchar(2),#q)+'% == (BLANK)', 'R'
select #q = #q + 1
End
from SQL and it runs just fine...i have used SQL commands before in a batch file...is there something different that needs to be done for this one since it is a loop and not a generic command? The file is being ran from the database server and I am getting an error:
Sqlcmd: '-Q': Missing argument. Enter '-?' for help. ' "declare #q int' is not recognized as an internal or external command, operable program or batch file
.....same thing with select, begin, insert and select....etc
I thought if you ever wanted to run SQLCMD, you just had to put your command in single quotes, is this not correct?

Your SQL statement in the batch file should be on the same line as the sqlcmd statement. It might be easiest to put the SQL statement in a seperate file, and use the -i switch to read the query from your file.

Related

Execute a batch file from SQL server using xp_cmdshell

I have a .bat file that iterates through it's residing director and takes the file name and creates a series of insert into select openrowset statements that is written to another file for later use.
I am trying to execute this .bat file in SQL Server using the below statement:
EXEC master..xp_cmdshell 'cmd /c filepath\FileTransfers\incoming\pdfs\gensql.bat'
overall the command works, if I replace gensql.bat with helloworld.bat I get the expected output.
The problem:
When I attempt to execute the command using the gensql.bat I get null output and the file is not written to.
If I run the bat file from the command prompt it works as expected.
If I manually run the bat file from it's folder it works as expected.
If I attempt it to run it via PowerShell I get access denied as expected.
I have tried to run it using the SQL Server job agent and the job runs successfully but there the expected output to the file is not there.
The account I am logged into and running the bat file has the permissions to the file location because I load other files from the same file path.
Command I am executing in SQL Server
EXEC master..xp_cmdshell 'cmd /c filepath\FileTransfers\incoming\pdfs\gensql.bat'
Contents of .bat file -- all this bat file is doing is writing statements to a file
#echo off
echo. > loadFiles.sql
for /r %%i in (*.pdf) do (
echo INSERT INTO dbo.user_attachments(content, extension, recipient_id^)
echo SELECT bulkcolumn, '.pdf', '%%~ni'
echo FROM OPENROWSET(BULK '%%i', SINGLE_BLOB^) AS t;
) >> loadFiles.sql
Any help I can get with this is very much appreciated.

How to use sqlcmd query result in batch script

I know this is very similar to this SO question, but I've modified the script to do what I'm trying to do and when I run the batch file it just closes right after hitting Enter after entering the company ID.
I need to check that the company id the user enters when the batch script runs is valid. I'm using user-entered values elsewhere in the script, so I'm sure that's correct, but I'm not getting why the script doesn't run as-is.
Here's my script:
#SETLOCAL ENABLEDELAYEDEXPANSION
#ECHO OFF
ECHO Enter the Company ID the data is for
SET /p _COMPANYID=Company ID:
SET _INSTANCE=someinstance
SET _DATABASE=somedb
for /f %%a in (
sqlcmd -b -S %_INSTANCE% -d %_DATABASE% -E -l 2 -Q "SET NOCOUNT ON; select count(*) from glb_companies where companyid = %_COMPANYID%"
)
do set _RESULT=%%a
echo %_RESULT%
#pause
I've tried the sqlcmd line with and without single quotes. The switches I have in the sqlcmd are what I'm using elsewhere and I've also tried it just with the switches from the other SO post. Nothing's worked for me yet.

Batch file to execute SQL Server stored procedure from client to server

I have the following situation:
I need to execute a SQL Server stored procedure compiled on the server, passing some values. I'm creating a batch file to do this task. Something like this:
#ECHO OFF
ECHO Enter the initial date in YYYYMMDD format (eg. 20171201):
SET /p pi_dte_ini=
ECHO Enter the final date in YYYYMMDD format (eg. 20171231):
SET /p pi_dte_end=
sqlcmd -Q "exec my_on_the_server_sp %pi_dte_ini%, %pi_dte_end%" -S server\instance
PAUSE
The problem is that I have to execute this .bat on a user's machine of our network. This machine don't have SQL Server installed, so it can't run sqlcmd. Any suggestions are welcome.
Regards!
You do not have to echo then run set /p. Instead add the echo text as part of the set /p
To execute sqlcmd remotely, use wmic
#Echo off
Set /p "pi_dte_ini=Set the initial date with YYYYMMDD format (eg: 20171201) and press Enter: "
Set /p "pi_dte_end=Set the final date with YYYYMMDD format (eg: 20171231) and press Enter: "
wmic /node:"servername" process call create ""cmd.exe /c sqlcmd -Q "exec my_on_the_server_sp %pi_dte_ini%, %pi_dte_end%" -S server\instance"
Pause
I cannot test this as I do not have sqlcmd, but you will probably need to adjust the double quotes in the wmic call.

Command result capture into variable is not working

I have a complicated batch script involving multiple BCP commands that load SQL server with flat file data. I am using a branch statement that logs the result of the command into a log file. This is all working fine.
The result is also logged in an SQL table as a success or failure. What I am trying to do is, upon failure, insert the contents of the log file into the SQL log table using what I thought was a simple redirect. It isn't working.
Here is what I am doing in a batch file in a Win Server 2012 environment:
BCP (the parameters here don't matter) >> %LogFile% (
MOVE %FileName% to Archive/%FileName%
sqlcmd -E -Q "EXEC spImportLog 'SUCCESS', %FileName%, ''
) || (
SET /p LogText=<%LogFile%
sqlcmd -E -Q "EXEC spImportLog 'FAILED', %FileName%, %LogText%
)
This code executes without error. The true part of the statement works fine.
The false part executes fine but I end up with empty string instead of
the contents of %LogText%.
When I run this from a command prompt, the SET looks like this:
SET /p LogText<0Log_2016_9_23.log
The %LogFile% is a variable because it is dynamically named at run time based on today's date. I don't know where the 0 after the less than sign is coming from, or if it is the source of my problem. Any ideas?
YADXP.
SET /p LogText=<%LogFile%
setlocal enabledelayedexpansion
sqlcmd -E -Q "EXEC spImportLog 'FAILED', %FileName%, !LogText!
endlocal
should work. See the billion or so SO entries about "delayed expansion"
As I said, set /p var=<filename will set the variable from the first line of the file. Since the file accumulates values and is initially sset to contain just one line (AAUI) 'None' then that is the value that will be read from the file.
Try
for /f "usebackqdelims=" %%a in ("%LogFile%") do set "logtext=%%a"
setlocal enabledelayedexpansion
sqlcmd -E -Q "EXEC spImportLog 'FAILED', %FileName%, !LogText!
endlocal
which should read every line of %logfile% and assign to logtest the value (of each line) in turn, so that when the for terminates, logtext will have the value of the last line of the file.
Okay, thanks for the posts. It doesn't sound like what I want to do is possible. At least not the way I am trying to do it. What I wanted is to store the entire contents of %LogFile% in a variable as a textstream, and then insert that text stream into a varchar column in SQL server. In retrospect, I wasn't clear enough in my initial post exactly the problem I was trying to solve. I know I can store the command results (multiple lines) in a temporary table in SQL Server. It is just that it requires using xp_cmdshell, which I was hoping to avoid.

Delete a list of files from SQL Server table

I have a SQL Server table with a list of file paths for files that I need to delete from my windows system, is there any way I can accomplish this using batch file in command prompt or any software to help me do this?? appreciate any help.
You could write a select statement that will create the commands for you, then run it from the command line:
e.g.
SELECT 'del /Q ' + file_name FROM your_table;
Save the output results to a file, then you can run it from the command line.
And you may automate the whole thing a little bit more by doing it on the command line with sqlcmd:
sqlcmd -d MyDb -Q "SELECT * FROM (SELECT 'DELETE /Q ' + file_name AS x FROM your_table UNION ALL SELECT 'EXIT') AS x" -h -1 -o temp.bat
temp.bat
DEL temp.bat

Resources