SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQLCMD mode in a job step?


SQLCMD mode in a job step?

Author
Message
glugingboulder
glugingboulder
Old Hand
Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)

Group: General Forum Members
Points: 398 Visits: 482
Is there anyway to get SQLCMD mode to run in a job step?

I find it a quick method to execute *.sql scripts with :r "C:\path\file.sql"
Also quick access using !! to execute cmdshell

I know there are methods using xp_cmdshell just not as elegant as SQLCMD mode in query editor
magasvs
magasvs
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2387 Visits: 756
Yes, you can do it.
Create "Operating system" type job step: "sqlcmd -S SQLSRV -i C:\Tests\TestCMD.sql"

example of TestCMD.sql file :

:Setvar BackFldr "C:\TestCMD\"

!!if exist C:\TestCMD\*.bak (move K:\TestCMD\*.bak K:\TestCMD\ARCHIVE\) ELSE (ECHO No files found)
glugingboulder
glugingboulder
Old Hand
Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)

Group: General Forum Members
Points: 398 Visits: 482
I guess its close, thanks for your input
tomiapi
tomiapi
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 6
"Yes, you can do it.
Create "Operating system" type job step: "sqlcmd -S SQLSRV -i C:\Tests\TestCMD.sql"

example of TestCMD.sql file :

:Setvar BackFldr "C:\TestCMD\"

!!if exist C:\TestCMD\*.bak (move K:\TestCMD\*.bak K:\TestCMD\ARCHIVE\) ELSE (ECHO No files found)"

My job is not working
don't you need to specify user and password for sqlserver?
magasvs
magasvs
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2387 Visits: 756
You can use Windows authentication - you need to add -E parameter (no need to use username and password).
tomiapi
tomiapi
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 6
you don't need to add the -E parameter if you don't specify password and username. it uses windows authentication by default.
I got it to run when i changed this :
"sqlcmd -S SQLSRV -i C:\Tests\TestCMD.sql"
to this:
sqlcmd -S SQLSRV -q "C:\Tests\TestCMD.sql"

I executed a tiny query that respond on the Command promt console inmediatly but my Job starts running and never finishes.. has this happened to you ?

thanks
Tom
magasvs
magasvs
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2387 Visits: 756
Try -Q instead of -q
tomiapi
tomiapi
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 6
That was awsome. Thank you so much
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search