【问题标题】:How to check multiple conditions in stored procedures for my query using if conditions, what mistakes I made in my procedures?如何使用 if 条件检查存储过程中的多个条件以进行查询,我在过程中犯了哪些错误?
【发布时间】:2021-02-23 18:29:54
【问题描述】:

这是我的条件:

  1. 如果 workorderi id = 13 -i.e valid number and allworkorderid=' ' 则只打印一个 workorderid

  2. 如果 workorderid = 1666666 - invalidnumber 并且 allworkorderid=' ' 则打印 invalidnumber

  3. 如果 workorderid = 845u485u45u4 - invalidnumber 并且 allworkorderid='yes' 然后打印 allworkorderid

  4. 如果 workorderid = 13 -validnumber 并且 allworkorderid='yes' 则打印 allworkorderid 。

  5. 如果 workorderid=845u485u45u4 -invalidnumber 和 allworkorderid='no' 则打印无效字符

除了第五个条件外,所有条件都有效。它显示两条消息,但我只希望这些条件的字符串优先

例如:

  • 133333,'no' 应该是一个无效字符,无论它是什么数字,如果它不是,它应该显示无效字符。

这是我的代码:

CREATE OR ALTER PROCEDURE Sp_workorders 
    (@workorderid INT,
     @allworkerid VARCHAR(20))
AS
BEGIN
    --this is used to check the id , if it exists below condition will execute or else ,it will print as invalid number
    IF EXISTS (SELECT wo.workorderid, wr.productid
               FROM adventureworks2019.production.workorder wo
               INNER JOIN adventureworks2019.production.workorderrouting wr
                          ON wo.workorderid = wr.workorderid
               WHERE wo.workorderid = @workorderid
               GROUP BY wo. workorderid, wr.productid)
    BEGIN
        --this will run if exists is true 
        IF (@workorderid = @workorderid AND @allworkerid = ' ')
              SELECT Workorders.workorderid,
                     productid,
                     countofoperationsequence,
                     countoflocationid,
                     max_operationseq,
                     max_locationid,
                     completionduration,
                     latedays,
                     category
              FROM   
                  (SELECT wo.workorderid,
                          wo.productid,
                          Count(operationsequence) CountofOperationSequence,
                          Count(locationid) CountofLocationID,
                          Max(operationsequence) Max_operationSeq,
                          Max(locationid) Max_LocationID
                      FROM
                          adventureworks2019.production.workorder WO
                      INNER JOIN
                          adventureworks2019.production.workorderrouting WR
                                     ON WO.workorderid = WR.workorderid
                      GROUP BY wo.workorderid, wo.productid) Workorders
              INNER JOIN 
                  (SELECT 
                       workorderid,
                       DATEDIFF(DAY, startdate, enddate)
                       CompletionDuration ,
                       DATEDIFF(DAY, duedate, enddate) LateDays,
                       CASE
                          WHEN enddate > duedate 
                             THEN 'Delayed Completion of Work Order'
                          WHEN enddate = duedate 
                             THEN 'On time Completion of Work Order'
                          WHEN enddate < duedate 
                             THEN 'Advance Completion of Work Order'
                       END AS Category
                   FROM   
                       adventureworks2019.production.workorder) DaysforCompletingwork ON Workorders.workorderid = DaysforCompletingwork.workorderid
              WHERE  
                  Workorders.workorderid = @workorderid
       END    
            ELSE--Else it will execute this 
              BEGIN
                  PRINT 'invalid number'
              END
     
--it is used to check only one parameter which is string ,whatever the first parameter it ignores it ,it will check only yes.
      IF ( @allworkerid = 'yes ' or @allworkerid=' ' )
      --calling a second procedure which is saved as a seperate file
        EXEC Sp_workorders2
          @workorderid=@workorderid,
          @allworkerid=@allworkerid
      ELSE
        PRINT 'invalid character'
    END 
--Scenarios:
 --exec Sp_workorders 13,'' -- working fine 
 -- exec Sp_workorders 13,'yes'--working fine
 --  exec Sp_workorders 13,'no'--working fine
 --   exec Sp_workorders 137777,'yes '--working fine whatever workorderidnumber it should give only precedence to only yes
    -- exec Sp_workorders 137788,'no' -- not working ,showing both the messages here i want to print message as  invalid  character 

【问题讨论】:

标签: sql sql-server tsql if-statement stored-procedures


【解决方案1】:

如果您将逻辑简化为用于测试的最小示例,则可以使您的逻辑正确,例如

DECLARE @workorderid INT = 137788, @allworkerid VARCHAR(20) = 'no';

-- This is used to check the id, if it exists below condition will execute or else, it will print as invalid number
IF 1 = 1 -- Toggle while testing
BEGIN
    -- This will run if exists is true 
    IF (@workorderid = @workorderid AND @allworkerid = ' ')
        PRINT('CONDITION SELECT');
END;   
ELSE -- Else it will execute this 
BEGIN
    PRINT 'invalid number';
END;
 
-- This is used to check only one parameter which is string ,whatever the first parameter it ignores it, it will check only yes.
IF (@allworkerid = 'yes ' or @allworkerid = ' ')
    PRINT('CALL Sp_workorders2');
ELSE
    PRINT 'invalid character';

很明显,您有两个单独的条件块,所以是的,它将贯穿它们。您需要应用每个块中所需的完整逻辑以避免重复消息。而且我强烈鼓励与使用开始/结束块保持一致 - 虽然您可以在没有开始/结束的 if 中使用单个语句,但我发现如果添加第二条语句会导致混淆和错误。最后,删除未使用的条件 @workorderid = @workorderid 并将 @allworkerid 设为 bit - 因为它就是这样使用的。

DECLARE @workorderid INT = 137788, @allworkerid bit = 0;

-- This is used to check the id, if it exists below condition will execute or else, it will print as invalid number
IF 1 = 0 -- Toggle while testing
BEGIN
    -- This will run if exists is true 
    IF (@allworkerid is null)
    BEGIN
        PRINT('CONDITION SELECT');
    END;
END;   
ELSE IF (@allworkerid = 1 or @allworkerid is null) -- Else ignore condition handled by next block
BEGIN
    PRINT 'invalid number';
END;
 
-- This is used to check only one parameter which is string ,whatever the first parameter it ignores it, it will check only yes.
IF (@allworkerid = 1 or @allworkerid is null)
BEGIN
    PRINT('CALL Sp_workorders2');
END;
ELSE
BEGIN
    PRINT 'invalid character';
END;

【讨论】:

  • 感谢 dale,根据您的输入,我应用了条件并且它起作用了,您可以看看我更新的查询,我可以对其进行更多优化吗?
  • @MuralidharanGopalakrishnan 在这个网站上,我们希望每个问题都问一个问题。如果这回答了你的问题,请接受它。然后,如果您还有其他问题,请提出新问题。但请注意,询问“能否优化此查询”并不是一个好问题。
【解决方案2】:

感谢 Dale 的投入。

我得到了想要的输出,请你检查一下,我可以为我的代码优化查询吗?

ALTER PROC Sp_workorders (@workorderid INT,
                      @allworkerid VARCHAR(20)=upper)
AS
-- Declare @allworkerid1 varchar(20);
--set @allworkerid1=@allworkerid
--this is used to check the id , if it exists below condition will execute or else ,it will print as invalid number
IF EXISTS(SELECT wo.workorderid,
                 wr.productid
          FROM   adventureworks2019.production.workorder wo
                 INNER JOIN adventureworks2019.production.workorderrouting
                            wr
                         ON wo.workorderid = wr.workorderid
          WHERE  wo.workorderid = @workorderid
          GROUP  BY wo. workorderid,
                    wr.productid)
  BEGIN
      --this will run if exists is true 
      IF ( @allworkerid = ' ' )
        BEGIN
            SELECT Workorders.workorderid,
                   productid,
                   countofoperationsequence,
                   countoflocationid,
                   max_operationseq,
                   max_locationid,
                   completionduration,
                   latedays,
                   category
            FROM   (SELECT wo.workorderid,
                           wo.productid,
                           Count(operationsequence)CountofOperationSequence,
                           Count(locationid)       CountofLocationID,
                           Max(operationsequence)  MAx_operationSeq,
                           Max(locationid)         Max_LocationID
                    FROM   adventureworks2019.production.workorder WO
                           INNER JOIN
                           adventureworks2019.production.workorderrouting
                           WR
                                   ON WO.workorderid = WR.workorderid
                    GROUP  BY wo.workorderid,
                              wo.productid)Workorders
                   INNER JOIN (SELECT workorderid,
                                      Datediff(day, startdate, enddate)
                                      CompletionDuration,
                                      Datediff(day, duedate, enddate)
                                      LateDays
                                      ,
                   CASE
                     WHEN enddate > duedate THEN
                     'Delayed Completion of Work Order'
                     WHEN enddate = duedate THEN
                     'On time Completion of Work Order'
                     WHEN enddate < duedate THEN
                     'Advance Completion of Work Order'
                   END                               AS
                   Category
                               FROM
                   adventureworks2019.production.workorder)
                              DaysforCompletingwork
                           ON Workorders.workorderid =
                              DaysforCompletingwork.workorderid
            WHERE  Workorders.workorderid = @workorderid
        END
  END
ELSE IF ( @allworkerid != ' '
     AND @allworkerid != 'yes' )--Else it will execute this 
  BEGIN
      PRINT 'invalid character'

      RETURN;
  END
ELSE
  BEGIN
      PRINT 'invalid  Number '
  END

--it is used to check only one parameter which is string ,whatever the first parameter it ignores it ,it will check only yes.
IF ( @allworkerid = 'YES'
      OR @allworkerid = ' ' )
  BEGIN
      --calling a second procedure which is saved as a seperate file
      EXEC Sp_workorders2
        @workorderid=@workorderid,
        @allworkerid=@allworkerid
  END
ELSE
  BEGIN
      PRINT ' Invalid character'
  END 

场景:

-- exec Sp_workorders 13,'yes' -- working fine 
-- exec Sp_workorders 13,' ' -- working fine 
-- exec Sp_workorders 13,'no'--working fine
-- exec Sp_workorders 135555,'yes'--working fine
-- exec Sp_workorders 137777,'no'--working fine whatever workorderidnumber it should give only precedence to only yes
-- exec Sp_workorders 137885,'' -- working good

【讨论】:

  • 这不是一个答案,应该是一个新问题。请考虑接受我的回答,因为它回答了最初提出的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多