下面是我的查询:
DELIMITER $$
DROP PROCEDURE IF EXISTS `UpdateDB_Base_Url`$$
CREATE PROCEDURE UpdateDB_Base_Url()
BEGIN
DECLARE @rownum INT DEFAULT 1;
DECLARE @tbname VARCHAR(50);
WHILE @rownum<105 DO
SET @tbname= SELECT CONCAT((SELECT dbname FROM temp_db WHERE rownum=@rownum),'.','core_config_data');
UPDATE @tbname t
SET t.`value` =CONCAT(SUBSTRING(`value`, 1,LOCATE('.' ,`value`)),'magapa.ns-staging.com.au')
WHERE t.`value` LIKE '%staging-php%';
SET @rownum=@rownum+1;
END WHILE;
END$$
DELIMITER ;
temp_db表具有行号最多104条记录的数据库名称。在这里我试图通过浏览我的服务器中的所有数据库来更新表,我收到了以下错误
Query: CREATE PROCEDURE UpdateDB_Base_Url() begin declare @rownum int default 1; declare @tbname varchar(50); while @rownum<105 do SET ...
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@rownum int default 1;
declare @tbname varchar(50);
while @rownum<105 do
SET @' at line 3
有人可以帮我这个吗?
谢谢!
答案 0 :(得分:0)
您的代码存在一些问题。
'@'
符号限定局部变量。他们是为了
用户/会话变量。select ... into
。无需明确set
和select
。update
从a中读取表名的表
变量,直接。你必须使用准备好的声明。更改以下代码:
DELIMITER $$
DROP PROCEDURE IF EXISTS `UpdateDB_Base_Url` $$
CREATE PROCEDURE UpdateDB_Base_Url()
BEGIN
DECLARE _rownum INT DEFAULT 1;
DECLARE _tbname VARCHAR(50);
SET _rownum := 1;
WHILE _rownum < 105 DO
SELECT CONCAT( dbname, ',', 'core_config_data' )
INTO _tbname
FROM temp_db
WHERE rownum = _rownum;
SET @sql := CONCAT( 'UPDATE ', _tbname, ' t ' );
SET @sql := CONCAT( @sql, ' SET t.`value` = ' );
SET @sql := CONCAT( @sql, ' CONCAT( SUBSTRING( t.`value`, 1, ' );
SET @sql := CONCAT( @sql, ' LOCATE( \'.\', t.`value` ) )' );
SET @sql := CONCAT( @sql, ', \'magapa.ns-staging.com.au\' )';
SET @sql := CONCAT( @sql, ' WHERE t.`value` LIKE \'%staging-php%\'' );
PREPARE stmt FROM @sql;
EXECUTE stmt;
DROP PREPARE stmt;
SET _rownum := _rownum + 1;
END WHILE;
END;
$$
DELIMITER ;