views:

78

answers:

2

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
+1  A: 

You call "exec" from inside the script, right? Please post the code.

vulkanino
+2  A: 

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.

Pondlife
i am just trying to automate a process so i don't have to run the command manually for each table i am processing. the path issue is not really that much of a problem for me, i can just use absolute paths and avoid that. i can probably do this through an external script but would be nice and impressive to be able to do it within sql server. i am only going to be running this infrequently, so its not really a big deal. if it can't be done without major annoyance, then don't worry, i will just do it the manual way.
warsong
I'm not sure why an external script is "manual"? The script can query the list of tables, build and execute a bcp.exe command for eaach one, and then do the same for 7z.exe. Then you only have to maintain the table list. My personal experience is that working with files and filesystems is such a pain in SQL that it's not worth the bother, but obviously things may be different in your environment.
Pondlife
sorry i meant manual as in without a script. i think if it is not possible/easy in sql server, i then will just do it manually, which is how i am doing it currently. i am having problems getting sql server to recognise the 7z.exe, it keeps saying it can't see it, even though i have restarted it.
warsong