Delete a list of files from SQL Server table - sql-server

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

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.

Getting an unwanted file after executing a bat file

I have a batch file that extracts one row from a database and uses that data as a variable to use the value to be a filename, and exports data as text file.
It works fine except for exporting two text files.
One of the two files is the correct file.
The other one is the unnecessary file. The filename is ".txt" and has the same data as the first one.
Here is my bat file:
SET SQLCMD=sqlcmd -S server -d db -Q "set NOCOUNT
on; select name from t1" -h-1
FOR /F "usebackq delims=, tokens=1" %%i IN (`%SQLCMD%`) DO (
CALL :PROCESSING %%i
)
:PROCESSING
SET COL1=%1
bcp "SELECT * FROM t1" queryout "C:\Export\%COL1%.txt." -S server -T -c
What do I need to fix to get only one file?
Since I am new to coding, if anyone could help, that would be very appreciated.
Thank you,

How can i execute a sql server 2008 command by a batch file?

i have to execute a sql command from a batch file. like :-
"Select top 1 versionNumber from SchemaVersion order by Id desc"
and Show its result on batch file , just when this batch file gets open.
please can any one suggest me right solution to do this ??
thanks in advance..
Use the sqlcmd.exe command in your batch file. (MSDN Reference: http://msdn.microsoft.com/en-us/library/ms162773.aspx)
sqlcmd.exe -S mysqlserver -d mydb -U myuser -P mypass -Q "Select top 1 versionNumber from SchemaVersion order by Id desc"
for example
specify -Q "set nocount on; Select top 1 versionNumber from SchemaVersion order by Id desc"

running SQL command from batch file and getting error

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.

running a bunch of SQL files in a folder

I have a bunch of scripts in a folder which I have to run in order. I just open it up in SSMS and hit execute one by one. I have to wait for one of them to complete then run the second one.
Is there a way for me to create a batch script or a script in SSMS (if its possible) that will go through the files one by one? This would save me time as I can do something else while the scripts are running and prevent error (most of them rely on temp tables created in succession).
I think a batch script with sqlcmd is what I am looking for but no idea how to accomplish this task.
Thank you.
Very easy to put together a batch file for this. Put something like the following into a text file but end the file with .bat, eg 'ExecuteMyScripts.bat'
Sqlcmd takes various arguments but the main ones are -S for server instance, -d for database, -i for input file ie your SQL file, -U for user, -P for password, and -o for output file. Watch the case of the letters.
So open notepad and add the following:
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\script1.sql" -o "c:\script1log.txt"
IF ERRORLEVEL 1 goto :eof
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\script2.sql" -o "c:\script2log.txt"
IF ERRORLEVEL 1 goto :eof
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\scripr3.sql" -o "c:\script3log.txt"
Save the file as ExecuteMyScripts.bat.
Double click the file and both script will be executed.
Adding 'IF ERRORLEVEL 1 goto :eof' after each sqlcmd call will jump to the end of the script if error found.
Full usage found here, http://msdn.microsoft.com/en-us/library/ms162773.aspx

Resources