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
 Transact-SQL (2000)
 Using Top to set a Variable

Author  Topic 

mj
Starting Member

25 Posts

Posted - 2003-10-01 : 09:26:39
Here is my scenario: Have records in a table, want to send email, using xp_sendmail, have to pull the lowest recordnumber from the table and set that equal to @emid, then loop through, and send the records. I'm getting a syntax error using keyword TOP, here is the query. I appreciate all assistance in advance, thanks mj.



declare @EMid int
select @emid = top 1 SampleRequestID from dbo.vSampleRequest order by SampleRequestID asc
--print @emid
While (Select email From Dbo.vSampleRequest Where (SampleRequestID = @EmID)) is not null
begin SELECT * from Dbo.vSampleRequest
Where SampleRequestID = @emid
EXEC master..xp_sendmail @recipients ='jordan_murphy@core.com',
@message = 'test of Sp', @Subject = 'Sample Request Email'
--print @emid
set
@emid = @emid + 1


end
GO

X002548
Not Just a Number

15586 Posts

Posted - 2003-10-01 : 10:29:54
Pronoun trouble...

Try this..


USE Northwind
GO

DECLARE @EMid int

SELECT TOP 1 @EMid = OrderId
FROM Orders
ORDER BY OrderId

SELECT TOP 1 OrderId
FROM Orders
ORDER BY OrderId

SELECT @EMid




Brett

8-)

SELECT @@POST FROM Brain ORDER BY NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page

Nazim
A custom title

1408 Posts

Posted - 2003-10-01 : 10:37:42
I think, this will be faster.

USE Northwind
GO
DECLARE @EMid int
SELECT @EMid = min(Orderid) FROM Orders


HTH


-------------------------
What lies behind you and what lies ahead of you are small matters compared to what lies within you.-Ralph Waldo Emerson
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-10-01 : 10:46:32
Did I mention (today?) that I hate TOP...

Anyway...the execution plan adds an extra step to the MIN code...

Doing stream aggregates...

The Traces are identical...but it's not a lot of data...

Brett

8-)

SELECT @@POST FROM Brain ORDER BY NewId()

That's correct! It's an AlphaNumeric!
Go to Top of Page
   

- Advertisement -