我试图在我的MySQL中执行一个简单的过程:
DELIMITER $$
CREATE PROCEDURE `countRows`(IN v varchar(30))
BEGIN
DECLARE e INT DEFAULT 0;
SET @t1 =CONCAT("SELECT COUNT(*) FROM ",V, " INTO",e);
PREPARE stmt3 FROM @t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
SELECT CONCAT("The ",V," table contains ",e," rows");
END$$
DELIMITER ;
编辑:一切正常但@ t1并没有将值保存在变量中,因此默认情况下第二个查询显示为0。
我无法看到任何语法错误,有什么建议让这个工作好吗?
答案 0 :(得分:2)
@t2
中的陈述;不需要预准备语句,因为它不包含任何要执行的SQL语句。下面提到的3个代码行是不必要的。
PREPARE stmt3 FROM @t2;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
您只需将其显示为
即可SET @t2 =CONCAT("The ",V," table contains ",e," rows");
SELECT @t2;
(或)只是
SELECT CONCAT("The ",V," table contains ",e," rows");
这样,你的程序看起来应该是
DELIMITER $$
CREATE PROCEDURE `countRows`(IN v varchar(30))
BEGIN
DECLARE e INT DEFAULT 0;
SET @t1 =CONCAT("SELECT COUNT(*) FROM ",V, " INTO",e);
SET @t2 =CONCAT("The ",V," table contains ",e," rows");
PREPARE stmt3 FROM @t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
SELECT @t2;
END$$
DELIMITER ;
修改强>
尝试使用用户定义的变量(下面的过程体)
DELIMITER $$
CREATE PROCEDURE `countRows`(IN v varchar(30))
BEGIN
SET @e := 0;
SET @t1 =CONCAT("SELECT COUNT(*) INTO @e FROM ",V);
PREPARE stmt3 FROM @t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
SELECT CONCAT("The ",V," table contains ",@e," rows");
END$$
DELIMITER ;
答案 1 :(得分:1)
正如您所指出的那样,您在存储过程中声明了变量# Add population boxplot (not grouped by peer)
ggplot(dat, aes(year, pct, group=interaction(year,peer), colour=factor(peer))) +
geom_boxplot(aes(group=year), width=0.05, colour="grey60", fill="#FFFFFF90") +
geom_boxplot(position=position_dodge(width=0.4), width=0.2) +
stat_summary(fun.y=mean, geom="line", position=position_dodge(width=0.4),
aes(group=peer)) +
stat_summary(fun.y=mean, geom="point", position=position_dodge(width=0.4), size=4,
aes(group=peer)) +
scale_x_continuous(breaks=unique(dat$year))
,因此它不存在于该环境之外。
我认为问题出在这里:
# Add an ID variable to the data
dat$id = rep(1:(nrow(dat)/2), each=2)
library(gridExtra) # For grid.arrange function
pdf("plots.pdf", 7, 5)
for (i in unique(dat$id)) {
p1 = ggplot() +
geom_boxplot(data=dat[dat$peer==unique(dat$peer[dat$id==i]),],
aes(year, pct, group=year)) +
geom_point(data=dat[dat$id==i,], aes(year, pct),
pch=8, colour="red", size=5) +
ggtitle("Your Peers")
p2 = ggplot() +
geom_boxplot(data=dat, aes(year, pct, group=year)) +
geom_point(data=dat[dat$id==i,], aes(year, pct),
pch=8, colour="red", size=5) +
ggtitle("All Participants")
grid.arrange(p1, p2, ncol=2, main=paste0("ID = ", i))
}
dev.off()
尝试将其更改为:
e