1

我使用 myDBR 为我的数据库创建报告,并且我有一个报告的表单,我可以在其中为每个事件添加具有多个DesignType的新事件

我有 3 个表:EventsEventID、Event、URL)、DesignTypeDesignTypeID、DesignType)和DesignTypeXEventsDesignTypeXEventID、EventID、DesignTypeID)。

我的表单有DesignTypes的复选框,它应该将EventIDDesignTypeID插入到表DesignTypeXEvents中。但是,如果我选择多个设计类型,我会收到一条错误消息,指出Subquery 返回超过 1 行。如果我选择多个设计类型,有没有办法让EventID将自身复制到其他行中?

这是我的代码:

    CREATE PROCEDURE `sp_DBR_events_create`(
    inEvent varchar(70),
    inEventURL varchar(70),
    inDesignType text
    )
    BEGIN

    INSERT INTO Events (Event, URL)
    VALUES (inEvent, inURL);

    set @vSQL = concat('insert into DesignTypeXEvents (EventID, 
    DesignTypeID) values ((SELECT MAX(EventID) FROM Events),
    (select DesignTypeID from DesignTypes where DesignTypeID in
    (', inDesignType, ')))');
    prepare stmt from @vSQL;
    execute stmt;
    deallocate prepare stmt;

如果我不包含EventID,则此代码效果很好并将正确的DesignTypeID插入多行,但我当然需要EventID

4

1 回答 1

0

使用INSERT INTO ... SELECT而不是INSERT INTO ... VALUES.

set @vSQL = concat('
    INSERT INTO DesignTypeXEvents (EventID, DesignTypeID)
    SELECT MaxEvent, DesignTypeID
    FROM (SELECT MAX(EventID) MaxEvent FROM Events) m
    CROSS JOIN
    (SELECT DesignTypeID FROM DesignTypes WHERE DesignTypeID IN (', inDesignType, ')) d');
于 2013-11-05T22:53:47.517 回答