Groups | Blog | Home
all groups > sql server programming > august 2006 >

sql server programming : SP for poll table


--CELKO--
8/27/2006 3:09:45 PM
Let's fix the DDL first. All those nulls made no sense, you had no
key, etc.

CREATE TABLE Poll
(question_nbr INTEGER NOT NULL PRIMARY KEY,
question_txt NVARCHAR(500) NOT NULL,
answer1 NVARCHAR(500) NOT NULL,
score1 INTEGER DEFAULT 0 NOT NULL
CHECK (score1 >= 0),
answer2 NVARCHAR(500) NOT NULL,
score2 INTEGER DEFAULT 0 NOT NULL
CHECK (score2 >= 0));

CREATE PROCEDURE UpdatePollScores
(@my_question_nbr INTEGER, @my_answer_nbr INTEGER)
AS
UPDATE Poll
SET score1
= score1 + CASE @my_answer_nbr WHEN 1 THEN 1 ELSE 0 END,
score2
= score2 + CASE @my_answer_nbr WHEN 2 THEN 2 ELSE 0 END
WHERE question_nbr = @my_question_nbr;
Arjen
8/27/2006 6:48:50 PM
Hi,

This is my poll table:
CREATE TABLE [dbo].[Poll](
[Id] [int] NOT NULL,
[Statement] [nvarchar](500) COLLATE Latin1_General_CI_AI NULL,
[Answer1] [nvarchar](500) COLLATE Latin1_General_CI_AI NULL,
[Score1] [int] NULL,
[Answer2] [nvarchar](500) COLLATE Latin1_General_CI_AI NULL,
[Score2] [int] NULL
)

I have a statement with two answers. If users select answer1 the score1
value has to be updated with + 1.

The stored procedure to update the score will get a '1' (for answer1) or a
'2' (for answer2) (and Id).
The stored procedure should update the Score1 or Score2 column. But how do I
know the correct column name?
And second, I first have to select the current score of that column.

Maybe, I'm thinking the wrong way. But I don't know how to do this.

Thanks for your help,
Arjen

Hugo Kornelis
8/28/2006 12:18:03 AM
[quoted text, click to view]

Hi Arjen,

CREATE STORED PROCUDERE NameGoesHere
@Answer char(1), @Id int
AS
BEGIN;
IF @Answer = '1'
BEGIN;
UPDATE Poll
SET Score1 = Score1 + 1
WHERE Id = @Id;
END;
ELSE
BEGIN;
UPDATE Poll
SET Score2 = Score2 + 1
WHERE Id = @Id;
END;
END;
go

--
Arjen
8/28/2006 1:12:18 PM
Hey thanks,
Arjen


[quoted text, click to view]
AddThis Social Bookmark Button