I looked at the way this was done with tabs but that seems like a lot of code and I thought a stored proc might be easier can anybody help me polish this off
I have a table
ID |
SortOrder |
Question |
1 |
4 |
Question 1 |
2 |
1 |
Question 2 |
3 |
2 |
Question 3 |
I query the table and sort by the sortorder - I get
Question 2
Question 3
Question 1
-Now I have requirement to be able to reorder the questions so I created a stored proc to do that part
Okay so in this case I find the ID of the question that would have a sortorder lower than the QuestionID i send to the proc
the first problem is if I send an id that has a sortorder of 1 it redoes it to 0 then goes on to -1 - how can i stop that
Is there a better way to do it?
PROCEDURE [dbo].[RCI_CAP_ReorderQuestions]
@QuestionID int,
@CompanyID int,
@Move bit
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--This is the id of the question to change to
DECLARE @IDUpper int
-- Insert statements for procedure here
IF @Move=1 --Move item higher in list (subtract from position)
BEGIN
SELECT @IDUpper= ID
FROM RCI_CAP_CapQuestions
Where CompanyID = @CompanyID
AND QuestionOrder+1 =
(SELECT QuestionOrder
FROM RCI_CAP_CapQuestions
WHERE ID = @QuestionID)
UPDATE RCI_CAP_CapQuestions
SET QuestionOrder=QuestionOrder+1
Where ID = @IDUpper
UPDATE RCI_CAP_CapQuestions
SET QuestionOrder=QuestionOrder-1
Where ID = @QuestionID
END
END