Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Import/Export (DTS) and Replication (2000)
 DTS & sprocs

Author  Topic 

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-06 : 09:57:28
Good Morning!

I'm using SQL server 7.0.

I have a stored procedure that changes a shipping column to 'X' based on certain criteria. I then want to execute a DTS to create a CSV file, then return to the sproc and change all of the rows marked as 'X' to 'Y'.

Is it possible to execute a DTS from an sproc? I can't find any references for it to figure it out.

Thanks!
Teresa

izaltsman
A custom title

1139 Posts

Posted - 2002-05-06 : 10:35:52
Just use xp_cmdshell extended stored procedure to execute dtsrun utility.

---------------
Strong SQL Developer wanted in the Boston area. Please e-mail if interested.
Go to Top of Page

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-06 : 10:54:23
Please bear with me, I'm trying to figure it out...

Here's what I have:

UPDATE EFILE SET SHIPPED = 'N' WHERE SHIPPED = 'X'
Update efile
set shipped = 'X' where (efikey = 'F' or efikey = 'O' or efikey = 'P') and shipped = 'N'

exec master..xp_cmdshell 'dtsrun/NTS05/EFI FEDEX', no_output



It runs without error, however... it doesn't create the CSV file. It works if I manually execute the DTS though.

Should I set 'output' equal to a variable, so the sproc knows when the dts is done and it can continue with the rest of the processes?

Please tell me what to look for now...

Thanks!
Teresa

Edited by - TJ on 05/06/2002 10:57:20
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-06 : 11:09:48
The line where you call dtsrun utility should look something like:

exec master..xp_cmdshell 'dtsrun /SNTS05 /Uuser_name /Ppassword /NEFI FEDEX', no_output

(this assumes that the name of your server is NTS05 and the name of the package is EFI FEDEX)

Actually, while you are debugging, you probably don't want to use the no_output parameter -- that way you will be able to execute your script in Query Analyzer and see any errors reported by DTSRUN...


---------------
Strong SQL Developer wanted in the Boston area. Please e-mail if interested.

Edited by - izaltsman on 05/06/2002 11:10:45
Go to Top of Page

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-06 : 11:17:43
quote:
exec master..xp_cmdshell 'dtsrun /SNTS05 /Uuser_name /Ppassword /NEFI FEDEX', no_output


What do the S and the N represent?

quote:

(this assumes that the name of your server is NTS05 and the name of the package is EFI FEDEX)



That's correct.

quote:

you probably don't want to use the no_output parameter



It's returning errors... I just don't understand them.

Please tell me where I can read about this besides 'books online'. It's just not clicking... yet!

Thanks again!
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-06 : 11:24:01
Just open BOL, go to index tab and type in "dtsrun"...
That should clear things up. If you still have problems after you read up on it, post the error messages here, and we'll try to figure out what's going on.

---------------
Strong SQL Developer wanted in the Boston area. Please e-mail if interested.
Go to Top of Page

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-06 : 12:10:05
quote:

That should clear things up.


It's a little clearer now. Thank you!!!

It's also working now!

I have another question...

I was reading Professional SQL Server 7.0 programming by Robert Vieira. He mentions locking down this XP because of potential security risks. Do I need to do that? If so, how?

Best regards,
Teresa

Edited by - TJ on 05/06/2002 12:10:36
Go to Top of Page

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-06 : 13:16:56
one more issue...

This ran fine on my local server. When I set it up to run on the system server, I ran into a problem. I had an issue with the username and password. Because of this, the dts did not run, but the rest of the sproc did. It should not do this. I need to verify that the dts executed successfully before the rest of the code executes. Can I set up a variable to tell me if it executed and then set up an if statement? If so, how.


CREATE PROCEDURE [NTS_FEDEXLabels] AS

--create the EFI FEDEX Labels
UPDATE EFILE SET SHIPPED = 'N' WHERE SHIPPED = 'X'
Update efile
set shipped = 'X'
where (efikey = 'F' or efikey = 'O' or efikey = 'P') and shipped = 'N'

--run the dts
exec master..xp_cmdshell 'dtsrun /Sserver/Uusername/Ppassword /NEFI FEDEX LABELS'

update efile
set shipped = 'Y', shipdte = getdate()
where shipped = 'X'

--create the QIK FEDEX Labels
Update efile
set shipped = 'X'
where efikey = 'R' and shipped = 'N'

--run the DTS
exec master..xp_cmdshell 'dtsrun /Sserver /Uusername /Ppassword /NQIK FEDEX LABELS'

update efile
set shipped = 'Y', shipdte = getdate()
where efikey = 'R' and shipped = 'X'


Thanks for all of your help and patience. I am truly thankful!!!

Teresa
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-06 : 14:36:34
quote:

I was reading Professional SQL Server 7.0 programming by Robert Vieira. He mentions locking down this XP because of potential security risks. Do I need to do that? If so, how?



Just make sure that SQLAgentCmdExec account has very limited permissions on your server. Revoke access to any folders that it shouldn't need to touch.

And as far as checking success/failure of the DTS package, I think the easiest way would be to capture the output of xp_cmdshell into a temp table, and then scan for the word 'Error' (or whatever dtsrun usually writes out when a package bombs).
So basically, the part of your proc that runs your DTS package might look something like this:



...

CREATE TABLE #cmdshell_out (line_out varchar(1000))

insert into #cmdshell_out
exec master..xp_cmdshell 'dtsrun /Sserver/Uusername/Ppassword /NEFI FEDEX LABELS'

IF EXISTS (select * from #cmdshell_out WHERE line_out LIKE '% error%')
BEGIN
RAISERROR ('DTS package failed', 16, 1)
RETURN 1
END

...


---------------
Strong SQL Developer wanted in the Boston area. Please e-mail if interested.

Edited by - izaltsman on 05/06/2002 14:37:08
Go to Top of Page

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-06 : 16:53:12

I've been working with this code:

CREATE PROCEDURE [NTS_FEDEXLabels] AS
declare @retval int
set @retval = 1

--create the EFI FEDEX Labels
UPDATE EFILE SET SHIPPED = 'N' WHERE SHIPPED = 'X'
Update efile
set shipped = 'X'
where (efikey = 'F' or efikey = 'O' or efikey = 'P') and shipped = 'N'
--check to see if there are any records to export to the CSV file
select @@rowcount from efile where Shipped = 'X'
if @@rowcount > 0
begin
--run the dts
CREATE TABLE #cmdshell_out2 (line_out varchar(1000))
insert into #cmdshell_out2
exec @retval = master..xp_cmdshell 'dtsrun /Sserver /Uusername /Ppassword /NEFI FEDEX LABELS'
-- if @retval = 0 then it was successful, if @retval = 1 then it failed
if @retval = 0
begin
update efile
set shipped = 'Y', shipdte = getdate()
where shipped = 'X'
end
else
IF EXISTS (select * from #cmdshell_out2 WHERE line_out LIKE '% error%')
BEGIN
RAISERROR ('DTS package failed', 16, 1)
RETURN 1
END
--print "The FEDEX Label process failed at EFI FEDEX Labels! Contact Teresa with this message information!"
end

--create the QIK FEDEX Labels
Update efile
set shipped = 'X'
where efikey = 'R' and shipped = 'N'
--check to see if there are any records to export to the QIK FEDEX CSV file
select @@rowcount from efile where shipped = 'X'
if @@rowcount > 0
begin
--run the DTS
exec @retval = master..xp_cmdshell 'dtsrun /Sserver /Uusername /Ppassword /NQIK FEDEX LABELS'
if @retval = 0 --successful
begin
update efile
set shipped = 'Y', shipdte = getdate()
where efikey = 'R' and shipped = 'X'
end
else
Print "The FEDEX Label process failed at QIK FEDEX Labels! Contact Teresa with this message information!"
end


It raises an error. But the only place I see it is in SQL Query Analyzer. Another person will be running this code from an ASP page. She'll need to see it, if it errors, and be able to tell me.

Can I access the #cmdshell_out2 table?

Thanks again...
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-07 : 09:09:13
quote:

It raises an error. But the only place I see it is in SQL Query Analyzer. Another person will be running this code from an ASP page. She'll need to see it, if it errors, and be able to tell me.
Can I access the #cmdshell_out2 table?



Well, #cmdshell_out2 gets discarded as soon as your stored procedure completes. But there are a few things you can do to find out when the DTS failed...
First of all, if you review your SQL Errorlogs daily you can use a WITH LOG option on your RAISERROR statement (just be sure to make the error message descriptive enough, so you know which package failed when you read your log).
Secondly, if you have SQLMail set up, you could use xp_sendmail to notify you that your proc detected an error (just make sure that you call xp_sendmail before you issue a RETURN command -- otherwise it will not get executed).
Finally, there is no reason why you can't add an additional audit table to your database, and have your proc write out error messages there (again, the INSERT statement would need to come before RETURN). You would have to review this table periodically.

---------------
Strong SQL Developer wanted in the Boston area. Please e-mail if interested.

Edited by - izaltsman on 05/07/2002 10:05:08
Go to Top of Page

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-07 : 10:57:05
quote:
Well, #cmdshell_out2 gets discarded as soon as your stored procedure completes.


I figured that out yesterday after I'd posted this. :o)

quote:
if you have SQLMail set up, you could use xp_sendmail to notify you that your proc detected an error (just make sure that you call xp_sendmail before you issue a RETURN command -- otherwise it will not get executed).


I used this code:

IF EXISTS (select * from ##cmdshell_out2 WHERE line_out LIKE '% error%')
BEGIN
RAISERROR (50001, 10, 1)
RETURN 1
EXEC xp_sendmail @recipients = 'Teresa J. Bishop;Tammy',
@message = 'The EFI FEDEX Label Process failed!',
@subject = 'EFI FEDEX LABEL'
END


I get an error message that says:

quote:

Cannot add rows to sysdepends for the current stored procedure because it depends on the missing object 'xp_sendmail'. The stored procedure will still be created.



I did verify that xp_sendmail is in the master db. We do not currently have email set up on the system server, but would like to have it. I have been testing this on my local server to get the issues resolved before I set it up on the system server.

What do I need to look for? I know the message is there. I know xp_sendmail is there. I just don't know why it's not working. :o)

Thanks!
Teresa
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-07 : 11:05:35
quote:

I did verify that xp_sendmail is in the master db.



Right... But your SQL Server doesn't know that! It is looking for this procedure in your current database. You have to use the three-part naming convention to explicitly point to the master database.
And secondly, like I said, make sure that you call xp_sendmail BEFORE you issue RETURN command:


IF EXISTS (select * from ##cmdshell_out2 WHERE line_out LIKE '% error%')
BEGIN
RAISERROR (50001, 10, 1)
EXEC master.dbo.xp_sendmail @recipients = 'Teresa J. Bishop;Tammy',
@message = 'The EFI FEDEX Label Process failed!',
@subject = 'EFI FEDEX LABEL'
RETURN 1
END



And until you set up e-mail on your server, you should be able to use the log or an audit table solution...

Edited by - izaltsman on 05/07/2002 11:08:12
Go to Top of Page

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-07 : 14:57:54
quote:

like I said, make sure that you call xp_sendmail BEFORE you issue RETURN command:


You're right... I apologize
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-07 : 15:17:14
quote:

You're right... I apologize



Oh I didn't mean to sound annoyed there at all!!! I just wanted to make sure that you notice the problem. I've had quite a few times when things that were supposed to run -- didn't simply because I overlooked a misplaced RETURN statement somewhere in the code. So I wanted to save you the headache of having to look for it later on!

Cheers!


Go to Top of Page

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-07 : 15:26:42
quote:

Cheers!




Thanks... I needed that... and when I get home, I'm going to have a few more!

New developments....

They don't want me to use the xp_sendmail. The new directions are to use begin transaction & commit transactions...

Thank you for all of your help!



Edited by - TJ on 05/07/2002 17:51:24

Edited by - TJ on 05/08/2002 09:17:31
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-07 : 20:13:33
Doh! I didn't even think of using return value from xp_cmdshell to check for DTS errors!

I don't see any problems with calling DTS package from the transaction. The only concern might be that you will be locking your tables for the duration of transaction.
Also you should take out "commit transaction fe" from the if block, that checks @retval... If you don't take it out, your proc will fail when it tries to execute the commit for the second time a couple of lines later!




Go to Top of Page

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-08 : 09:15:44
This ended up being the final code. If you see anything that needs changed, please let me know! Thanks for seeing this through with me. I am extremely greatful!


declare @retval int
set @retval = 1

begin transaction
--create the EFI FEDEX Labels
Update efile
set shipped = 'X'
where (efikey = 'F' or efikey = 'O' or efikey = 'P') and shipped = 'N'
commit transaction

--check to see if there are any records to export to the CSV file
select @@rowcount from efile where Shipped = 'X'
if @@rowcount > 0
begin
--run the dts
exec @retval = master..xp_cmdshell 'dtsrun /Sserver /Uusername /Ppassword /NEFI FEDEX LABELS'
-- if @retval = 0 then it was successful, if @retval = 1 then it failed
begin transaction
if @retval = 0 --successful
begin
update efile
set shipped = 'Y', shipdte = getdate()
where (efikey = 'F' or efikey = 'O' or efikey = 'P') and shipped = 'X'
end
commit transaction

if @retval <> 0 --failed
begin
update efile
set shipped = 'N', shipdte = getdate()
where (efikey = 'F' or efikey = 'O' or efikey = 'P') and shipped = 'X'
end

end
--create the QIK FEDEX Labels
BEGIN TRANSACTION
Update efile
set shipped = 'X'
where efikey = 'R' and shipped = 'N'
commit transaction

--check to see if there are any records to export to the QIK FEDEX CSV file
select @@rowcount from efile where shipped = 'X'
if @@rowcount > 0
begin
--run the DTS
exec @retval = master..xp_cmdshell 'dtsrun /Sserver /Uusername /Ppassword /NQIK FEDEX LABELS'

begin transaction
if @retval = 0 --successful
begin
update efile
set shipped = 'Y', shipdte = getdate()
where efikey = 'R' and shipped = 'X'
end
COMMIT TRANSACTION

if @retval <> 0 --failed
begin
update efile
set shipped = 'N', shipdte = getdate()
where efikey = 'R' and shipped = 'X'
end
end
Go to Top of Page

izaltsman
A custom title

1139 Posts

Posted - 2002-05-08 : 18:16:48
The code you provided will work, but it does have a few minor issues that don't affect functionality, but make it look odd.
1. You don't need to wrap every UPDATE statement in a separate transaction. SQL Server automatically ensures that each individual UPDATE, INSERT, and DELETE statement is transactional (would get executed completely or not at all). So a user-defined transaction around it is redundant. Besides, you aren't doing any error handling in your code, and aren't trying to ROLLBACK anywhere, so you don't need any user-defined transactions at all.
2. In a couple of places you've tried to select @@rowcount value from a table... @@rowcount is an automatic variable that gets set to the number of rows affected by the last statement executed on the connection. So it makes no sense to select it from a table! In fact, since you are simply trying to check whether there are any rows with an 'X' in the "Shipped" field, an IF EXISTS statement would be more appropriate there.
So I'm thinking your code could look something like this:


declare @retval int
set @retval = 1

--create the EFI FEDEX Labels
Update efile
set shipped = 'X'
where (efikey = 'F' or efikey = 'O' or efikey = 'P') and shipped = 'N'

--check to see if there are any records to export to the CSV file

if EXISTS (select * from efile where Shipped = 'X')
begin
--run the dts
exec @retval = master..xp_cmdshell 'dtsrun /Sserver /Uusername /Ppassword /NEFI FEDEX LABELS'
-- if @retval = 0 then it was successful, if @retval = 1 then it failed
if @retval = 0 --successful
begin
update efile
set shipped = 'Y', shipdte = getdate()
where (efikey = 'F' or efikey = 'O' or efikey = 'P') and shipped = 'X'
end
else --failed
begin
update efile
set shipped = 'N', shipdte = getdate()
where (efikey = 'F' or efikey = 'O' or efikey = 'P') and shipped = 'X'
end
end

--create the QIK FEDEX Labels
Update efile
set shipped = 'X'
where efikey = 'R' and shipped = 'N'

--check to see if there are any records to export to the QIK FEDEX CSV file
if EXISTS (select * from efile where shipped = 'X')
begin
--run the DTS
exec @retval = master..xp_cmdshell 'dtsrun /Sserver /Uusername /Ppassword /NQIK FEDEX LABELS'

if @retval = 0 --successful
begin
update efile
set shipped = 'Y', shipdte = getdate()
where efikey = 'R' and shipped = 'X'
end
else --failed
begin
update efile
set shipped = 'N', shipdte = getdate()
where efikey = 'R' and shipped = 'X'
end
end


Go to Top of Page

TJ
Posting Yak Master

201 Posts

Posted - 2002-05-09 : 09:28:22
quote:

you aren't doing any error handling in your code, and aren't trying to ROLLBACK anywhere, so you don't need any user-defined transactions at all.


Should I be? What is the best practice to employ?

quote:
IF EXISTS statement would be more appropriate there.


I'll implement the changes you suggested.

Thanks again for sticking with me and lending your expertise!

Teresa
Go to Top of Page
   

- Advertisement -