Hi,
I am trying to write a .bat (batch file) for one of our users to use each day. I'm using a .bat file because there are several other functions I need to run. In SQL I have the following query:
It works great in T-SQL. I would like to see if there is a way I can run in SQLCMD. Something like this. The user will be entering in a folder name that will be used in the script as %folder%.
Looks like using multiple lines in sqlcmd is kind of difficult. Not sure if I can utilize this type of While loop in sqlcmd?
Thanks!
Brian
I am trying to write a .bat (batch file) for one of our users to use each day. I'm using a .bat file because there are several other functions I need to run. In SQL I have the following query:
Code:
while (select count(copied) from #temp where copied is not null) > 0
begin
declare @Mincounter int
declare @imagename varchar(100)
DECLARE @DynamicSQL nvarchar(1000);
set @mincounter = (select MIN(counter) from temp where copied is not null)
set @imagename = (select imagename from #temp where counter = @mincounter)
set @DynamicSQL = ('exec master..xp_cmdshell ' + '''' + 'copy \\server\share1\folder\' + @imagename + '.* \\server\share2\folder\' + 'D' + @mincounter + '.*' + '''')
begin
print @dynamicsql
EXEC sp_executesql @DynamicSQL
update temp
set copied = NULL
where counter = @mincounter
end
end
It works great in T-SQL. I would like to see if there is a way I can run in SQLCMD. Something like this. The user will be entering in a folder name that will be used in the script as %folder%.
Code:
set /p folder=
sqlcmd -S servername -U user -P password -Q "while (select count(copied) from #temp where copied is not null) > 0
begin
declare @Mincounter int
declare @imagename varchar(100)
DECLARE @DynamicSQL nvarchar(1000);
set @mincounter = (select MIN(counter) from temp where copied is not null)
set @imagename = (select imagename from #temp where counter = @mincounter)
set @DynamicSQL = ('exec master..xp_cmdshell ' + '''' + 'copy \\server\share1\%folder%\' + @imagename + '.* \\server\shar2e\%folder%\' + 'D' + @mincounter + '.*' + '''')
begin
print @dynamicsql
EXEC sp_executesql @DynamicSQL
update temp
set copied = NULL
where counter = @mincounter
end
end"
Looks like using multiple lines in sqlcmd is kind of difficult. Not sure if I can utilize this type of While loop in sqlcmd?
Thanks!
Brian