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.
Related
I have a simple move command as:
move tfa.war ..\..\..\tomcat\webapps
When i run this command in command prompt it works fine. But when i put this command in a batch file with variables like so, and execute the batch file in command prompt it fails with the message "The system cannot find the file specified".
cmd.exe /C webapp_deploy.bat tfa
And the contents of webapp_deploy.bat:
#ECHO OFF
set app_name=%~1
set webapps_directory=..\..\..\tomcat\webapps
move %app_name%.war %webapps_directory%
Any help would be much appreciated!
I have written a batch file for cleaning up the recovery files for one of my project work for testing. The script is explained below:
It will run an executable which takes a dll file as an argument and then run a process.
If the process is killed before doing some calculation, it creates recovery file.
Here my script will always load the dll after cleaning up the recovery files to avoid manually deleting the files by visiting a particular path.
To make generic, batch script will store the dll path and dll name in text files from where it will automatically pick the path.
Below is the batch script code:
#echo off
::Display the information first time if variables not set. Store them for future retrieval
set "currPath=%cd%"
if exist dllPath.txt (
set /p dllPath<=dllPath.txt
) else (
set /p dllPath="Provide game dll path (1st time only): "
::save the environment variables
echo %dllPath% > "dllPath.txt"
)
if exist dllName.txt (
set /p dllName<=dllName.txt
) else (
set /p dllName="Provide game dll name (1st time only): "
::save the environment variables
echo %dllName% > "dllName.txt"
)
::clean the data
cd /D %dllPath%
#echo %dllPath%
del *.bin
del *.recovery
%dllPath%\..\..\RunProcess.exe -dll %dllPath%\%dllName%
cd /D %currPath%
Issue with the script
First time on running the script from a command prompt, script is able to properly store the dll path and name properly in a text file.
If I run the script multiple times from the same command prompt, the process will run successfully.
But if I close the command prompt after running the script and try to run the script from newly opened command prompt, the script will throw error as
The syntax of the command is incorrect.
I tried to google about this and try to fix it, but couldn't. This may be a silly question to ask, but being novice in batch scripting I don't have any other option.
Place the = before the <.
set /p dllName=<dllName.txt
I'm trying to connect to sqlplus using batch file. I have the following code. when i run the batch file the command prompt is throwing error "The filename, directory name, or volumen label syntax is incorrect.
but it is working fine in another machine. i can't understand the problem. the same is connecting when i type the commands in cmd directly without running batch file.
setlocal
set raaga=raaga1
set ORACLE_HOME=C:\Oracle\product\11.2.3\dbhome_1
set ORACLE_SID=%raaga%
set PATH=%ORACLE_HOME%\bin;%PATH%
(
Echo connect sys/manager22#%raaga% as SYSDBA;
) | Sqlplus /nolog
Do i need to install anything? any help is appreciated.
thanks in advance
I cannot execute command line command on Windows 2003 server.
Below is the path which i put in notepad and save it as .bat and RUN it doesnt help.
D:\Program Files(x86)\PH\Check This 7.11\ASD\ABC>sm -reportlic > D:\srini\test1.txt
Above command helps me to execute the command from dos prompt and save results in txt file to D:\ drive at mentioned path.
But cant create a batch file to execute the same.
Please help any assistance will be appreciated.
Regards,
Sri
You need to either change to the proper drive and folder to execute sm first, or call it directly using a full path. In either case, you need to quote the path because it contains spaces.
Changing drive and directory first:
cd /d "D:\Program Files(x86)\PH\Check This 7.11\ASD\ABC"
sm -reportlic > D:\srini\test1.txt
Using a full path:
"D:\Program Files(x86)\PH\Check This 7.11\ASD\ABC>sm" -reportlic > D:\srini\test1.txt
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.