我正在尝试创建第一套MySQL过程。在这种情况下,我想通过一个简单的调用将4个表中的相关信息整理为单个结果,并返回离散选择的字段作为结果。为此,我写了这个:
CREATE PROCEDURE `DatabaseName.GetCityIdentity`
(
IN CityId BIGINT(20) UNSIGNED,
OUT
`id` BIGINT(20) UNSIGNED,
`provId` BIGINT(20) UNSIGNED,
`countryId` BIGINT(20) UNSIGNED,
`currencyId` BIGINT(20) UNSIGNED,
`cityName` VARCHAR(64),
`latitude` DECIMAL(10,6),
`longitude` DECIMAL(10,6),
`provName` VARCHAR(64),
`provAbbr` VARCHAR(4),
`countryName` VARCHAR(64),
`countryAbbr` VARCHAR(4),
`currencyName` VARCHAR(64),
`currencySymbol`VARCHAR(8),
`fullName` VARCHAR(256),
`abbrName` VARCHAR(80)
)
BEGIN
SELECT
`c`.`id` INTO `id`, `c`.`name` INTO `cityName`, `c`.`provId` INTO `provId`, `c`.`latitude` INTO`latitude`, `c`.`longitude` INTO `longitude`,
`p`.`name` INTO `provName`, `p`.`abbr` INTO `provAbbr`, `p`.`countryId` INTO `countryId`,
`y`.`name` INTO `countryName`, `y`.`abbr` INTO `countryAbbr`, `y`.`defaultCurrency` INTO `currencyId`,
`d`.`name` INTO `currencyName`, `d`.`symbol` INTO `currencySymbol`,
CONCAT(`c`.`name`, ', ', `p`.`name`, ', ', `y`.`Name`) INTO `fullName`,
CONCAT(`c`.`name`, ', ', `p`.`abbr`, ', ', `y`.`abbr`) INTO `abbrName`
FROM `cities` AS `c`
LEFT JOIN `provinces` AS `p` ON `c`.`provId` = `p`.`id`
LEFT JOIN `countries` AS `y` ON `p`.`countryId` = `y`.`id`
LEFT JOIN `currencies` AS `d` ON `y`.`defaultCurrency` = `d`.`id`
WHERE (`c`.`id` = CityId) LIMIT 1
END;
不幸的是,我在SELECT
查询中的表名上遇到了错误(“错误:1327:未声明的变量:c”)。因此,我应该如何构建此SELECT
使其按预期工作而不引用表名?
我尝试删除所有“ AS x
”语句,并仅使用实际的表名,但是,当然,我得到了完全相同的错误,只是用表名替换了变量名。 ..
对于常规的(My)SQL开发人员,我确定这是显而易见的/简单的事情,但到目前为止,对我而言,这似乎与直觉相反。
PS:如果有关系,我正在使用MySQL Workbench进行此操作。
答案 0 :(得分:0)
我认为您的语法不正确;请尝试将INTO放置在选择项下方,如下所示:
DELIMITER //
CREATE PROCEDURE `DatabaseName.GetCityIdentity`
(
IN CityId bigint(20) unsigned,
OUT
`id` BIGINT(20) UNSIGNED,
`provId` BIGINT(20) UNSIGNED,
`countryId` BIGINT(20) UNSIGNED,
`currencyId` BIGINT(20) UNSIGNED,
`cityName` VARCHAR(64),
`latitude` DECIMAL(10,6),
`longitude` DECIMAL(10,6),
`provName` VARCHAR(64),
`provAbbr` VARCHAR(4),
`countryName` VARCHAR(64),
`countryAbbr` VARCHAR(4),
`currencyName` VARCHAR(64),
`currencySymbol`VARCHAR(8),
`fullName` VARCHAR(256),
`abbrName` VARCHAR(80)
)
BEGIN
SELECT
`cities`.`id`,
`cities`.`name`,
`cities`.`provId`,
`cities`.`latitude`,
`cities`.`longitude`,
`provinces`.`name`,
`provinces`.`abbr`,
`provinces`.`countryId`,
`countries`.`name`,
`countries`.`abbr`,
`countries`.`defaultCurrency`,
`currencies`.`name`,
`currencies`.`symbol`,
CONCAT(`cities`.`name`, ', ', `provinces`.`name`, ', ', `country`.`Name`),
CONCAT(`cities`.`name`, ', ', `provinces`.`abbr`, ', ', `country`.`abbr`)
INTO `id`, `cityName`, `provId`, `latitude`, `longitude`, `provName`,
`provAbbr`, `countryId`, `countryName`, `countryAbbr`,
`currencyId`, `currencyName`, `currencySymbol`, `fullName`,
`abbrName`
FROM `cities`
LEFT JOIN `provinces` ON `cities`.`provId` = `provinces`.`id`
LEFT JOIN `countries` ON `provinces`.`countryId` = `countries`.`id`
LEFT JOIN `currencies` ON `countries`.`defaultCurrency` = `currencies`.`id`
WHERE (`cities`.`id` = CityId) LIMIT 1;
END //
DELIMITER ;