• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

loop through a spreadsheet, move files, then send email - Stored Procedure

Hi, I want to know if this is possible, I have a spreadsheet like below I want to loop through each cell in column A (ProdID is the header cell A1):

ProdID            
AAA123
AAA124
AAA125

Then go and check if these ProdID are listed in a table on sqlserver which looks like below:

ProdID             Path
AAA123         C:\Bobme\Gray\AAA123.pdf
AAA124         C:\Bobme\Gray\AAA124.pdf
AAA125         C:\Bobme\Gray\AAA125.pdf

If it does then I want it to copy the file from the relevant Path and paste this into a a temp folder i.e C:\Temp. Once the loop has finished I want it to send the files that has been copied into the temp file and send a email to the user, i.e xp_sendmail. I know there is a limit on the attachments via xp_sendmail bu just want to know if this is possible or is there a alternative solution to this process?

Thanks
0
sqlman08
Asked:
sqlman08
  • 4
  • 3
1 Solution
 
vinurajrCommented:
you can do it
1) Import the excel into the DB
2) Copy he file EXEC  master.dbo.xp_cmdshell 'COPY C:\temp\'+@File Name
3) send Mail xp_sendmail
repeat step 2, 3
0
 
sqlman08Author Commented:
ok, I have done a dts package to import the spreadhseet to a table. Now how do I join to the other table find a match and copy the files into a temp folder?
0
 
FJS159Commented:
Select ProdID from TableA Inner Join TableB on TableA.ProdId = TableB.ProdId

Will give you only those items in TableB that exists in TableA

Your ProdID appears sequentially numbered.  I would loop through the results starting with the min ID and add 1 each time until done.  
left(path,len(Path)-7)+@I+'.pdf'  will incriment the number portion of your filename and attach PDF onto the end.  This assumes your filenames start at 0.
You could also insert your email notification into this loop.


declare @I as int, @filename as varchar(200)
set @I = 0    
WHILE (SELECT @I  ) < (select count(ProdID) from TableA Inner Join TableB on TableA.ProdId = TableB.ProdId)

BEGIN

select @I = @I +1

Select @filename = left(path,len(Path)-7)+@I+'.pdf' from from TableA Inner Join TableB on TableA.ProdId = TableB.ProdId where @I = right(ProdID,3)

EXEC  master.dbo.xp_cmdshell 'COPY C:\temp\'+@FileName


END
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
sqlman08Author Commented:
do not cancel I like FJS159 has a better solution which I am currently testing
0
 
sqlman08Author Commented:
hi, I tried t run this in query analyzer

declare @I as int, @filename as varchar(200)
set @I = 0    
WHILE (SELECT @I  ) < (select count(ps.ProdID) from tablea ps Inner Join tableb po on ltrim(rtrim(ps.ProdID)) = 'OP/'+ltrim(rtrim(po.ProdID)))

BEGIN

select @I = @I +1

Select @filename = left(po.path,len(po.Path)-7)+@I+'.pdf' from tablea ps Inner Join tableb po
on ltrim(rtrim(ps.ProdID)) = 'OP/'+ltrim(rtrim(po.ProdID)) where @I = right(ps.ProdID,3)

EXEC  master.dbo.xp_cmdshell 'COPY C:\Temp\'+@filename


but I get the following error:

Server: Msg 170, Level 15, State 1, Line 12
Line 12: Incorrect syntax near '+'.



0
 
FJS159Commented:
Sorry I just copied the cmdshell line from vinurajr, it was just an example but wasn't complete and I think had an error.
The cmdshell must be the same as you would type in DOS and requires a source & destination for the copy command.
First make sure you have permission to copy files.  try a simple command with no variables.  ie
EXEC  master.dbo.xp_cmdshell 'COPY C:\file.txt c:\file.tmp';
go

If that works try assigning the entire value for your copy command to a variable, then passing that.  ie
USE master;
declare @filename as nvarchar(50), @filename2 as nvarchar(50)
set @filename2 = 'file.txt'
set @filename = 'COPY C:\'+@filename2+' c:\file.tmp'
EXEC  master.dbo.xp_cmdshell @filename
go
0
 
sqlman08Author Commented:
Hi, the following works when I use this query:

USE master;
declare @filename as nvarchar(50), @filename2 as nvarchar(50)
set @filename2 = 'file.txt'
set @filename = 'COPY C:\'+@filename2+' c:\file.tmp'
EXEC  master.dbo.xp_cmdshell @filename


When I now run this:
declare @I as int, @filename as varchar(200)
set @I = 0    
WHILE (SELECT @I  ) < (select count(ps.ProdIDo) from tablea ps Inner Join tableb po on ltrim(rtrim(ps.ProdID)) = 'OP/'+ltrim(rtrim(po.ProdID)))


BEGIN

select @I = @I +1

      
Select @filename = left(po.path,len(po.Path)-7)+@I+'.pdf' from tablea ps Inner Join tableb po
on ltrim(rtrim(ps.ProdID)) = 'OP/'+ltrim(rtrim(po.ProdID)) where @I = right(ps.ProdID,3)

EXEC  master.dbo.xp_cmdshell 'COPY C:\pdftemp\'+@filename

 I get the follwoing error:

Server: Msg 170, Level 15, State 1, Line 16
Line 16: Incorrect syntax near '+'.


0
 
FJS159Commented:
Don't use the + in your cmdshell line.  Try:

declare @filename2 as nvarchar(100)
select @I = @I +1

     
Select @filename = left(po.path,len(po.Path)-7)+@I+'.pdf' from tablea ps Inner Join tableb po
on ltrim(rtrim(ps.ProdID)) = 'OP/'+ltrim(rtrim(po.ProdID)) where @I = right(ps.ProdID,3)

set @filename2 = 'COPY C:\pdftemp\'+@filename
EXEC  master.dbo.xp_cmdshell  @filename2
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now