2
ALTER PROCEDURE [dbo].[Sp_GetEmailForMailing] 
    -- Add the parameters for the stored procedure here
    @end int=1000
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT top(500) RegistrationID, EmailID from dbo.Candidate_RegistrationData
    where status_flag=1 and payment_status=2
    and RegistrationID NOT IN (SELECT top(@end) RegistrationID from dbo.Candidate_RegistrationData
    where status_flag=1 and payment_status=2) order by RegistrationID
END

This is my stored procedure. In this I want rows from 500 but I am getting from 250 row number.... any one have idea why it happen...thank you

1
  • You mean to say, first @end registration will be skipped? order by registrationID ascending ? Commented Jun 4, 2011 at 9:27

2 Answers 2

3

The subquery used as the source of data for the NOT IN does not have an ORDER BY clause. As a result, the query engine is free to take the TOP(@end) rows in whichever order it sees fit.

Sign up to request clarification or add additional context in comments.

Comments

0

I hope you want to skip top @end registration, Order By RegistrationID

You should try below code.

ALTER PROCEDURE [dbo].[Sp_GetEmailForMailing]   

@end int = 1000  

AS  

SET NOCOUNT ON;

BEGIN
    Select Top(500) RegistrationID, EmailID From
    (
         Select Row_Number() Over(Order by RegistrationID) as RowId, 
                   RegistrationID,     
                   EmailID,    
                   status_flag, 
                   payment_status
         From dbo.Candidate_RegistrationData
         Where status_flag = 1 and payment_status = 2
    )T
    Where RowId > @end and status_flag = 1 and payment_status = 2 
    Order By RegistrationID
End

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.