【问题标题】:MySQL stored procedure will execute but conditions not workingMySQL 存储过程将执行但条件不起作用
【发布时间】:2014-12-31 08:11:29
【问题描述】:

MySQL 存储过程将执行,但条件不起作用。有人可以解决这个问题吗?

空值未检查or 条件。是否需要替换or

DELIMITER $$

CREATE DEFINER=`rebar`@`%` PROCEDURE `SearchInProgress`(
ClientID bigint,
GCName varchar(250),
TeamID int,
USPMID Bigint,
JobReceivedDate datetime,
importanceID Bigint
)
begin
select * from jobdetails 
where 
    (clientid = ClientID or ClientID = "") and 
    (GCName = GCName or GCName ="") and
    (TeamID = TeamID or TeamID ="") and 
    (ReceivedDate = JobReceivedDate or JobReceivedDate = "") and
    (ImportanceID = importanceID or importanceID = "") and
    (JobID in (select jobid from JobCoordinatorDetails where USProjectManagerID = USPMID) );
end

【问题讨论】:

    标签: mysql stored-procedures scope mariadb


    【解决方案1】:

    我觉得可能是括号

    这个(clientid = ClientID or ClientID = "")可以这样写

    (clientid = ClientID) OR (ClientID = "" )

    这只是一个想法。我可能错了

    【讨论】:

      【解决方案2】:
      DELIMITER $$
      DROP PROCEDURE if exists SearchInProgress $$
      
      CREATE PROCEDURE `SearchInProgress`(
          aclientID bigint,
          aGCName varchar(250),
          aTeamID int,
          aUSProjectManagerID bigint,
          aReceivedDate datetime,
          aImportanceID bigint
      )
      begin
      select * from jobdetails 
      where 
          (clientid = aclientID or aclientID = '') and 
          (GCName = aGCName or aGCName = '') and
          (TeamID = aTeamID or aTeamID = '') and 
          (ReceivedDate = aReceivedDate or aReceivedDate = '0000-00-00 00:00:00') and
          (ImportanceID = aImportanceID or aImportanceID = '') and
          (JobID in (select jobid
                      from JobCoordinatorDetails
                      where USProjectManagerID = aUSProjectManagerID) or aUSProjectManagerID = '')
         ;
      end
      

      参数名和列名必须不同。我已经在参数名称前添加了a

      将空的日期时间值替换为“0000-00-00 00:00:00”:https://dev.mysql.com/doc/refman/5.7/en/datetime.html

      【讨论】:

        猜你喜欢
        • 2020-06-15
        • 2020-04-06
        • 2020-11-10
        • 1970-01-01
        • 1970-01-01
        • 2012-04-07
        • 1970-01-01
        • 2020-12-17
        • 2021-10-02
        相关资源
        最近更新 更多