Question

is it possible to call 7-zip from a SQL script? basically i am extracting tables to csv and then packaging them into 7z files. I have installed 7-zip on the machine where SQL Server is installed and added it to the path, but this is not enough it seems.

I get the following error when i try to run the script with xp_cmdshell

'7z' is not recognized as an internal or external command,
operable program or batch file.

this is the code, i have already declared the variables

declare @archiveCommand nvarchar(max)
declare @filename nvarchar(max)

set @archiveCommand = '7z a '+@filename+'.7z '+@filename+' '
print @archiveCommand
EXEC master..xp_cmdshell @archiveCommand
Was it helpful?

Solution

It's possible, yes, but probably a bad idea: permissions are often a problem (as you've found out), paths and working directories will trip you up, and building shell commands in SQL is a pain all round. It would be much easier just to use an external script, and run it from a scheduled job or SSIS package.

If you clarify exaclty why and when you want to run the script from SQL then someone may be able to suggest a better approach. I do exactly the same thing using SSIS and Python, for example.

OTHER TIPS

I don't know if this will be helpful now, but I came across this issue and solved it by calling the 7z app with the file name.

So instead of:

set @archiveCommand = '7z a '+@filename+'.7z '+@filename+' '

You would want:

set @archiveCommand = '"C:\Program Files\7-zip\7z" a '+@filename+'.7z '+@filename+' '

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top