如果数据库字段 = x 然后设置存储过程变量 = y,如何做一个案例或 if 语句

how to do a case or if statement if databse field = x then set storedprocvariable = y

我在 Database1 中有 tableA,其中有一列 Genre(只有 3 种电影类型)

在我的存储过程中,我想以某种方式做一个 case 或 if 语句,它在 database1.tableA 中检查列 Genre 并执行以下逻辑..

if genre is horror then 
   set @newvariable = 1 
if genre is comedy then 
   set @newvarable = 2 
if genre is animated then 
   set @newvarible = 3 

有什么建议吗?

你可以使用case statement得到你想要的,

   SELECT 
      newvalue  = CASE WHEN genre = 'horror' THEN 1
                       WHEN genre = 'comedy' THEN 2
                       WHEN genre = 'animated' THEN 3
                  END
  FROM 
       tableA 

这将 return 您获得所需的价值。


更新

AS OP需要存储过程,

CREATE PROCEDURE [dbo].[spnamehere]
@gameId INT
AS
BEGIN
   SET NOCOUNT ON;
   SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

   SELECT
       NewValue = CASE WHEN genre = 'horror' THEN 1
                       WHEN genre = 'comedy' THEN 2
                       WHEN genre = 'animated' THEN 3
                  END
   FROM 
       tablename
   WHERE
       gameId = @gameId
END