Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help with my sql server stored procedure

Posted on 2008-10-03
3
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 37

Accepted Solution

by:
momi_sabag earned 2000 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 60

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

610 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