February 22, 2011 at 2:37 pm
This probably belongs in the newbie section, but maybe someone here can help...
I want to Copy and Rename a file to a Customer Account No. I want this file to be created for every customer account that is pulled from the SELECT statement. Here is what I have, but I am having difficulties understanding which loop I should be using, and how I should use it. I am able to use the xp_cmdshell successfully by itself, but once I try to put it into a loop, it seems it is the apostrophes that are getting messed up.
First, i had to enable the xp_cmdshell. You can do this by referring to
Here is the script I want to run successfully...
DECLARE @CustNo VARCHAR(MAX)
SET @CustNo = ''
DECLARE @SQLQUERY VARCHAR(MAX);
SET @SQLQUERY = ''
WHILE Exists (SELECT Cust_No FROM AR_CUST WHERE TERMS_COD = 'CASH')
SET @CustNo = (SELECT cust_no FROM AR_CUST WHERE TERMS_COD = 'CASH')
SET @SQLQUERY =
'xp_cmdshell 'COPY /Y C:\CounterPoint\Edwards\CustomerImages\100-100009.jpg /B C:\CounterPoint\Edwards\CustomerImages\'+@CustNo+'.jpg''
EXEC (@SQLQUERY);
end loop;
Thanks in advance,
Andy
February 22, 2011 at 2:40 pm
Double the single-quote right after xp_cmdshell, right before COPY.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
February 22, 2011 at 3:20 pm
I did get the xp_cmdshell to work correctly by fixing the apostrophes. But how do I get the @CustNo variable to equal whatever value the loop is currently on?
DECLARE @CustNo VARCHAR(MAX)
SET @CustNo = ''
DECLARE @SQLQUERY VARCHAR(MAX);
SET @SQLQUERY = ''
WHILE Exists (select cust_no from AR_CUST where TERMS_COD = 'CASH')
SET @CustNo = 'whatever Cust_No the loop is currently on'
SET @SQLQUERY =
'xp_cmdshell ''COPY /Y C:\CounterPoint\Edwards\CustomerImages\100-100009.jpg /B C:\CounterPoint\Edwards\CustomerImages\'+@CustNo+'.jpg'''
EXEC (@SQLQUERY);
February 22, 2011 at 3:30 pm
here's a cursor example that is based on what you posted:
DECLARE @CustNo VARCHAR(MAX)
DECLARE @SQLQUERY VARCHAR(MAX);
SET @SQLQUERY = ''
declare c1 cursor for
select
CustNo
from YourTable
where someCriteria > 0
open c1
fetch next from c1 into @CustNo
While @@fetch_status <> -1
begin
SET @SQLQUERY =
'xp_cmdshell ''COPY /Y C:\CounterPoint\Edwards\CustomerImages\100-100009.jpg /B C:\CounterPoint\Edwards\CustomerImages\'+@CustNo+'.jpg'''
fetch next from c1 into @CustNo
end
close c1
deallocate c1
Lowell
February 22, 2011 at 4:25 pm
Thanks All. Got it working.
DECLARE @CustNo VARCHAR(MAX)
DECLARE @SQLQUERY VARCHAR(MAX);
SET @SQLQUERY = ''
SET @CustNo = ''
declare c1 cursor for
select CUST_NO from AR_CUST where TERMS_COD = 'CASH'
open c1
fetch next from c1 into @CustNo
While @@fetch_status <> -1
begin
SET @SQLQUERY =
'xp_cmdshell ''COPY /Y C:\CounterPoint\Edwards\CustomerImages\100-100001.jpg /B C:\CounterPoint\Edwards\CustomerImages\'+@CustNo+'.jpg'''
EXEC (@SQLQUERY)
fetch next from c1 into @CustNo
end
close c1
deallocate c1
Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply