MySQL-存储过程错误操作数应包含1列

时间:2019-03-23 16:13:13

标签: mysql stored-procedures temp-tables

我在MySQL数据库中有此存储过程,但是当我像下面进行测试时,出现Operand should contain 1 column(s)错误:

CALL `get_strongest_policy_from_all_involved_accounts_for_user`(57);

这是我的存储过程。从第6行到第20行的代码可以正常工作,并且它返回一个包含 0个或更多记录的表。

有人可以帮我解决错误,因为我不知道是什么原因引起的。非常感谢!

CREATE DEFINER=`root`@`localhost` PROCEDURE `get_strongest_policy_from_all_involved_accounts_for_user` (
  IN app_user_id INT
)
BEGIN
  CREATE TEMPORARY TABLE `ParentAccountPolicies` ENGINE=MEMORY SELECT (
    SELECT UAS.`password_rotation_active`, UAS.`password_rotation_days`, UAS.`Password_minimum_lenght`,
     UAS.`password_must_contain_numeric_char`, UAS.`password_must_contain_lowercase_char`,
           UAS.`password_must_contain_upercase_char`, UAS.`password_must_contain_special_char`
    FROM `user_account` UA, `user_account_settings` UAS
    WHERE UA.`account_id` = UAS.`account_id`
  AND UA.`primary_user_id` = app_user_id
    UNION
    SELECT UAS.`password_rotation_active`, UAS.`password_rotation_days`, UAS.`Password_minimum_lenght`,
     UAS.`password_must_contain_numeric_char`, UAS.`password_must_contain_lowercase_char`,
           UAS.`password_must_contain_upercase_char`, UAS.`password_must_contain_special_char`
    FROM `user_account` UA, `business` B, `app_user_n_business` AUB, `user_account_settings` UAS
    WHERE UA.`business_id` = B.`business_id`
  AND B.`business_id` = AUB.`business_id`
        AND UAS.`account_id` = UA.`account_id`
        AND AUB.`app_user_id` = app_user_id
);

  SET @RotationActive = B'0', @HaveNum = B'0', @HaveLowCase = B'0', @HaveUpCase = B'0', @HaveSpecial = B'0';
  SET @RotationDays = 0, @MinLength = 0;

  IF (SELECT DISTINCT `password_rotation_active` FROM `ParentAccountPolicies` WHERE `password_rotation_active` = TRUE) IS NOT NULL THEN
    SET @RotationActive = B'1';
  END IF;

  IF (SELECT DISTINCT `password_must_contain_numeric_char` FROM `ParentAccountPolicies` WHERE `password_must_contain_numeric_char` = TRUE) IS NOT NULL THEN
    SET @HaveNum = B'1';
  END IF;

  IF (SELECT DISTINCT `password_must_contain_lowercase_char` FROM `ParentAccountPolicies` WHERE `password_must_contain_lowercase_char` = TRUE) IS NOT NULL THEN
    SET @HaveLowCase = B'1';
  END IF;

  IF (SELECT DISTINCT `password_must_contain_upercase_char` FROM `ParentAccountPolicies` WHERE `password_must_contain_upercase_char` = TRUE) IS NOT NULL THEN
    SET @HaveUpCase = B'1';
  END IF;

  IF (SELECT DISTINCT `password_must_contain_special_char` FROM `ParentAccountPolicies` WHERE `password_must_contain_special_char` = TRUE) IS NOT NULL THEN
    SET @HaveSpecial = B'1';
  END IF;    

    SELECT MAX(`password_rotation_days`) INTO @RotationDays FROM `ParentAccountPolicies`;
    SELECT MAX(`Password_minimum_lenght`) INTO @RotationDays FROM `ParentAccountPolicies`;

    CREATE TEMPORARY TABLE `FinalPolicy` (
        `RotationActive` BIT,
        `RotationDays` INT,
        `MinimumLength` INT,
        `ShouldHaveDigit` BIT,
        `ShouldHaveLowerCaseChar` BIT,
        `ShouldHaveUpperCaseChar` BIT,
        `ShouldHaveSpecialChar` BIT
    );

    INSERT INTO `FinalPolicy` VALUES (@RotationActive, @RotationDays, @MinLength, @HaveNum, @HaveLowCase, @HaveUpCase, @HaveSpecial);

    SELECT `FinalPolicy`;
END;

1 个答案:

答案 0 :(得分:0)

您执行以下操作:

CREATE TEMPORARY TABLE `ParentAccountPolicies` ENGINE=MEMORY SELECT (
  SELECT 'a' as x, 'b' as y
  FROM dual
  UNION
  SELECT 'c' as x, 'd' as y
  FROM dual
);

这会导致以下错误:

ER_OPERAND_COLUMNS: Operand should contain 1 column(s)

Demo

原因是您有一个子选择(SELECT子句中的子查询),该子选择必须仅返回一列,最多返回一行。但是您的子选择将返回多列。

但是您根本不需要使用子选择。只需像下面这样删除外部SELECT:

CREATE TEMPORARY TABLE `ParentAccountPolicies` ENGINE=MEMORY  
  SELECT 'a' as x, 'b' as y
  FROM dual
  UNION
  SELECT 'c' as x, 'd' as y
  FROM dual
;

Demo