CREATE TABLE [Poll] (
	[PollName] [nvarchar] (128) NOT NULL ,
	[Vote] [char] (1) NOT NULL ,
	[VoteCount] [int] NOT NULL 
) ON [PRIMARY]
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

Create  procedure GetVoteCount
(
  @PollName nvarchar(128)
)
AS
BEGIN
  SELECT
    Vote, 
    VoteCount 
  FROM 
    Poll 
  WHERE 
    PollName = @PollName

END

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO


Create   PROCEDURE IncrementPoll
(
    @PollName  nvarchar(128),
    @Vote char
)
AS
IF NOT EXISTS (
    SELECT
        * 
    FROM 
        Poll 
    WHERE 
        PollName = @PollName AND Vote = @Vote
)
BEGIN
    -- Transacted insert for download count
    BEGIN TRAN
        INSERT INTO 
            Poll
        VALUES
        (
            @PollName,
            @Vote,
            1
        )
    COMMIT TRAN
END
ELSE
BEGIN
    -- Transacted update for download count
    BEGIN TRAN
        UPDATE 
          Poll
        SET 
          VoteCount  =  VoteCount + 1
        WHERE 
          PollName = @PollName AND
          Vote = @Vote
    COMMIT TRAN
END


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO