如果是在MYSQL存储过程中

时间:2014-05-23 06:56:14

标签: mysql stored-procedures procedure

我的程序中给定的插入查询非常庞大。针对给定的不同条件的所有插入查询都非常相同。如何准备循环以避免每次都提供插入查询而不是一次。

begin
insert query(..........)
elseif dayofmonth(now()) = 12 then
insert query(..........)
elseif dayofmonth(now()) = 22 then
insert query(..........)
endif;
end

我需要像

这样的程序
begin
if dayofmonth(now()) = 2 then
elseif dayofmonth(now()) = 12 then
elseif dayofmonth(now()) = 22 then
????
insert query(........)
how to end this???
谁能帮助我?谢谢提前..

1 个答案:

答案 0 :(得分:0)

试试这个

begin

if dayofmonth(now()) = 6 then
SET @query="columnnames and values"
elseif dayofmonth(now()) = 12 then
SET @query="columnnames and values"
elseif dayofmonth(now()) = 22 then
SET @query="columnnames and values"
endif;

SET @query=concat("insert into", @query)
PREPARE stmt1 FROM @query; 
EXECUTE stmt1;

end
相关问题