我正在尝试执行以下操作,我想知道在MySQL中是否可行:
CREATE PROCEDURE `sp_test`(
ulon int(4),
usrname varchar(20),
usrst varchar(20),
usrdt varchar(10)
)
BEGIN
DECLARE bid int(11);
START TRANSACTION;
SELECT t_bid INTO bid FROM generalb WHERE dt=usrdt;
INSERT
INTO
rslt
(
rbid,
rusrname,
rusrst,
usrdt
)
VALUES
(
bid,
usrname,
usrst,
usrdt
);
call sp_nextproc_13(bid, ulon);
COMMIT;
END;
看看这一行:
call sp_nextproc_13(bid, ulon);
我怎么能达到这样的目的:
调用sp_nextproc_ @ bid(bid,ulon);
我需要能够动态调用过程,因为在我收到出价之前我不知道过程的名称。如果有人知道答案,非常感谢
最新更新:
我做了以下更改:
CREATE PROCEDURE `sp_test`(
ulon int(4),
usrname varchar(20),
usrst varchar(20),
usrdt varchar(10)
)
BEGIN
DECLARE bid int(11);
START TRANSACTION;
SELECT t_bid INTO bid FROM generalb WHERE dt=usrdt;
INSERT
INTO
rslt
(
rbid,
rusrname,
rusrst,
usrdt
)
VALUES
(
bid,
usrname,
usrst,
usrdt
);
SET @sql=concat('call sp_nextproc_',bid,'(?,?)');
PREPARE stmt FROM @sql;
SET @var1=bid;
SET @var2=ulon;
EXECUTE stmt USING @var1, @var2;
COMMIT;
END;
如果我对某些值进行硬编码,则可行。但是,如果我不这样做,那就没有了。请参阅以下内容:
如果我打电话:
致电sp_test
(2,' John'' test',' AAAA');
错误在这里:
SELECT t_bid INTO bid FROM generalb WHERE dt=usrdt;
列t_bid不能为空
如果我像这样对值进行硬编码:
SELECT t_bid INTO bid FROM generalb WHERE dt='AAAA';
错误消失。
第二个错误位于以下行:
SET @var1=bid;
SET @var2=ulon;
EXECUTE stmt USING @var1, @var2;
ERROR缺失值@ var1和@ var2。
如果我硬编码var1和var2,它可以工作:
SET @var1=13;
SET @var2=2;
EXECUTE stmt USING @var1, @var2;
我尝试使用@而没有变量,但是没有用。我做错了什么? :(
答案 0 :(得分:1)
delimiter $$
create procedure sp_test
(
ulon int(4)
)
BEGIN
DECLARE bid int(11);
set bid=8888;
...
...
...
...
set @sql=concat('call sp_nextproc_',bid,'(?,?)');
prepare stmt from @sql;
set @var1=bid;
set @var2=ulon;
EXECUTE stmt using @var1,@var2;
END
$$
-- ------------
delimiter $$
CREATE PROCEDURE `sp_nextproc_8888`(
IN bid int(11),
IN ulon int(4)
)
BEGIN
select bid,ulon;
END
$$
-- test with these:
-- call sp_test(9999);
-- call sp_nextproc_8888(111,222);