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
 SQL Server Development (2000)
 Do I need cursors?

Author  Topic 

nickfinity
Yak Posting Veteran

55 Posts

Posted - 2005-12-03 : 20:55:54
Hello,

Before I am punished and sent to the guillotine for thinking I may need cursors, I know it is not a good idea to use them, so please pardon my ignorance. I will be using SQL Mail to send out e-mails regarding order status. The items ordered need to be included as part of the e-mail. To loop through the items and create the text of the e-mail do I need a cursor? If not, what would my other options be?

The client only wants to do this once a day, so would I need a cursor to first loop through the orders? I hope not. Right now I have the queries I need. They are fairly simple:

SELECT orderid, orderdate, etc FROM orders WHERE somecondition
-- add order info to text of email
SELECT itemname,itemprice, etc FROM items WHERE orderid=@orderid
-- add items info to text of email

Thank you very much for any help, I really appreciate it.

Thanks,
Nick

cfederl
Starting Member

26 Posts

Posted - 2005-12-03 : 21:53:18
The extended stored procedure xp_sendmail will accept a SQL statement and will handle formating the output. The SQL statement is limit to 8,000 bytes but executing a stored procedure instead of SELECT does work. See Books OnLine for more details.

EXEC xp_sendmail @recipients = 'nickfinity@mycompany.com'
, @subject = 'Orders received in the last 24 hours'
, @attach_results = 'TRUE', @width = 100
, @query = '
select *
from Northwind.dbo.[Order Details Extended]
where OrderDate between current_timestamp - 1 and current_timestamp
order by OrderId , ProductId'


Carl Federl
Go to Top of Page
   

- Advertisement -