使用带有变量值

时间:2016-11-02 14:16:13

标签: mysql

我在MYSQL中执行此查询时遇到错误。你能帮我解决一下这个问题。这是查询。

IF ('"+ @[User::CrMgName] +"' IS NULL)
THEN
Update companies
  Set crm_id = '"+ @[User::crm_guid] +"'
Where TRIM(LOWER(company_directory_name)) = '"+ @[User::EquityStoryId] +"'

ELSE
Update companies
  Set cr_manager = '"+ @[User::CrMgName] +"',
crm_id = '"+ @[User::crm_guid] +"'
Where TRIM(LOWER(company_directory_name)) = '"+ @[User::EquityStoryId] +"'  
END IF

1 个答案:

答案 0 :(得分:0)

评论之后,如果在脚本中使用IF语句表示该问题。

所以,解决方案是两个:

  1. (正如Fari所说):使用存储过程
  2. IF语句替换为CASE语句
  3. 对于解决方案2.试试这个:

    UPDATE companies
    SET crm_id = 
    CASE 
       WHEN @[User::CrMgName] IS NULL THEN '"+ @[User::crm_guid] +"'
       ELSE'"+ @[User::crm_guid] +"'
    END,
    cr_manager = 
    CASE
      WHEN @[User::CrMgName] IS NULL THEN cr_manager
      ELSE '"+ @[User::CrMgName] +"'
    END
    WHERE TRIM(LOWER(company_directory_name)) = '"+ @[User::EquityStoryId] +"'