Solved

Help with my sql server stored procedure

Posted on 2008-10-03
3
203 Views
Last Modified: 2010-03-19
I have a stored procedure to which i wish to pass an additional parameter @ROW_TO_RETURN

How do i change the sql query. ( Instead of the top 50 which i am retrieving in the query below, i wish to use the input parameter to specify the row to returns.

SELECT TOP 50
      QUESTION_ID,
      CORRECT_ANSWER_ID,
      MARK_ALLOCATED,
      NEWID()ID
FROM CERTIFYMAX_QUESTIONBANK_VIEW
WHERE SUBJECT_ID=@SUBJECT_ID AND (TOPIC_ID=@TOPIC_ID or @TOPIC_ID = -2) ORDER BY NEWID()

CREATE PROCEDURE [dbo].[CertifyMax_ExamSetup_GenerateExamQuestions]

(

@SUBJECT_ID int,

@TOPIC_ID int,

@ROW_TO_RETURN int,

@EXAM_REFERENCE_CODE nvarchar(50)

)

AS

SET NOCOUNT ON;

 

INSERT INTO CERTIFYMAX_EXAMINATION_QUESTIONS 

(

	QUESTION_ID,

	CORRECT_ANSWER_ID,

	MARK_ALLOCATED,

	EXAM_REFERENCE_CODE,

	COUNT_NO

)

SELECT 

	QUESTION_ID,

	CORRECT_ANSWER_ID,

	MARK_ALLOCATED,

	@EXAM_REFERENCE_CODE,

	ROW_NUMBER() OVER(ORDER BY ID) 

FROM

(

SELECT TOP 50

	QUESTION_ID,

	CORRECT_ANSWER_ID,

	MARK_ALLOCATED,

	NEWID()ID

FROM CERTIFYMAX_QUESTIONBANK_VIEW 

WHERE SUBJECT_ID=@SUBJECT_ID AND (TOPIC_ID=@TOPIC_ID or @TOPIC_ID = -2) ORDER BY NEWID()

) tmp

Open in new window

0
Comment
Question by:TECH_NET
3 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 22639832
SELECT TOP (@ROW_TO_RETURN)
      QUESTION_ID,
      CORRECT_ANSWER_ID,
      MARK_ALLOCATED,
      NEWID()ID
FROM CERTIFYMAX_QUESTIONBANK_VIEW
WHERE SUBJECT_ID=@SUBJECT_ID AND (TOPIC_ID=@TOPIC_ID or @TOPIC_ID = -2) ORDER BY NEWID()
0
 
LVL 4

Expert Comment

by:petr_hlucin
ID: 22639833
This one should help.
SELECT 

      QUESTION_ID,

      CORRECT_ANSWER_ID,

      MARK_ALLOCATED,

      NEWID()ID,

      ROW_NUMBER() Over (ORDER BY ID) as RowID

FROM CERTIFYMAX_QUESTIONBANK_VIEW 

WHERE SUBJECT_ID=@SUBJECT_ID AND (TOPIC_ID=@TOPIC_ID or @TOPIC_ID = -2) 

AND RowID <= 50

ORDER BY NEWID()

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22639926
Additional note to asker.

ORDER BY NEWID()

You are essentially creating new uniqueidentifiers for sake of ordering.  Since you have already created these in the select statement through:

NEWID() AS ID

You can order by the column number, in your case 4.

ORDER BY 4

Just an FYI.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now