sql server - Passing variable into xp_cmdshell -


i have stored procedure in sql server checks today's backup files (files has date in filename). after checks, move on robocopy these files folder.

the challenge: in folder, there files yesterday's or other dates. today's bak files required transfer.

--@day allows me capture day of month declare @day char(2) set @day = right('00' + convert(nvarchar(2),datepart(day,getdate())),2) --print @day  --in "myfolder", might containts files  --project_backupfile_01_2006_02_28_001.bak --or project_backupfile_01_2006_02_27_001.bak  --currently need hard code 28 represent 28th. how pass in @day? exec master..xp_cmdshell 'dir d:\myfolder\project*28*.bak/b'  --similarly, pass in @day variable --project_backupfile*02_*@day.bak  -- copy backup fules ftp local drive exec master..xp_cmdshell 'robocopy "d:\source" "e:\mssql\restore\" project_backupfile*_02_28*.bak /nfl /ndl /copy:dat /r:2 /w:1 /xo /e /z /mt:10'  

use variable form command before passing xp_cmdshell

declare @cmd varchar(100) select @cmd = 'dir d:\myfolder\project*' + datename(day, getdate()) + '*.bak/b' -- print @cmd exec master..xp_cmdshell @cmd 

note: datename(day, getdate()) give day of month string.

stuff(convert(varchar(5), getdate(), 101), 3, 1, '_') give 02_28


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -