November 25, 2019 at 3:22 pm
Hi there
I have a routine which outputs an ID generated from a stored procedure into a text file as follows:
:!!if exist \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt del \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt
--:connect localhost\SQL2008R2
:out \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt
select Ltrim(Rtrim(@StoredProcedureRunTrackerID))
Now i wanted to change this , so that the path for :out is not hardcoded and passed in as a variable, ie
:on error exit
:setvar DirectoryRootPath "\\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt"
then calling
:!!if exist @DirectoryRootPath del '$(DirectoryRootPath)'
--:connect localhost\SQL2008R2
:out '$(DirectoryRootPath)'
select @StoredProcedureRunTrackerID
But this not produce my output file
Is there something syntatically that I am doing wrong here?
The SP is executing ok, just the output of the ID into a field isnt
Hi there
I have a routine which outputs an ID generated from a stored procedure into a text file as follows:
:!!if exist \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt del \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt
--:connect localhost\SQL2008R2
:out \\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt
select Ltrim(Rtrim(@StoredProcedureRunTrackerID))
Now i wanted to change this , so that the path for :out is not hardcoded and passed in as a variable, ie
:on error exit
:setvar DirectoryRootPath "\\LONDEVSQL01\Staging\SEG_DEV_H01\Import\SupplementaryImport\StoredProcedureRunTrackerID.txt"
then calling
:!!if exist @DirectoryRootPath del '$(DirectoryRootPath)'
--:connect localhost\SQL2008R2
:out '$(DirectoryRootPath)'
select @StoredProcedureRunTrackerID
But this not produce my output file
Is there something syntatically that I am doing wrong here?
The SP is executing ok, just the output of the ID into a field isnt
November 26, 2019 at 4:10 pm
Thanks for posting your issue and hopefully someone will answer soon.
This is an automated bump to increase visibility of your question.
December 3, 2019 at 6:54 am
I've always been frustrated by things like that. It's a complete tease that MS built into the system. My answer has always been to use xp_CmdShell for such things... and, no... it's not the security risk that most will make it out to be if you set it up and use it correctly. Having xp_CmdShell turned off will not protect you from anything. If someone get's in with sysadmin privs, they can turn it on. If they can't, they can't use it even if it is turned on. And, except for DBAs that have sysadmin privs, you can allow users to execute stored procedures that use it without give those same users privs to use xp_CmdShell directly.
Of course, I stopped using the SQLCmd mode a very long time ago because it couldn't do things like you describe but that may have changed. I'd love for someone to blast an answer for this.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 10, 2019 at 3:12 am
OK, I played around with this. I think your quotes are the problem. This works for me:
:setvar DirectoryRootPath "C:\Users\Steve\OneDrive\Documents\SQL\StoredProcedureRunTrackerID.txt"
--:!!if exist C:\Users\Steve\OneDrive\Documents\SQL\StoredProcedureRunTrackerID.txt del C:\Users\Steve\OneDrive\Documents\SQL\StoredProcedureRunTrackerID.txt
!!if exist $(DirectoryRootPath) del $(DirectoryRootPath)
:connect Plato\SQL2017
:out $(DirectoryRootPath)
select * FROM dbo.MyTable
This deletes the text file and puts the data out there
December 10, 2019 at 9:38 am
Steve,
Will that actually work in a stored procedure? I'm thinking "not" and that it could only work from a "routine" (if you understand the difference) but I could be wrong.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 10, 2019 at 3:43 pm
Should not, as a proc is not in SQLCMD mode. What is happening here in a script is SSMS in interpreting this a certain way.
You can call a proc here, but SQLCMD mode is an interactive execution (or using sqlcmd.exe)
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply