db2 在我的存储过程中,使用if else条件插入两个不同的表

pdkcd3nj  于 2023-01-05  发布在  DB2
关注(0)|答案(2)|浏览(191)
create procedure sp_student 
    @student varchar(50);
as 
    if (@student = marks)
    begin
        insert into marks 
        values (student_id int not null, terms varchar(10), subject1 varchar(100), subject2 varchar(100), total varchar(100))
    end
    else (@student = students)
    begin
        insert into students 
        values(student_id int not null, student_name varchar(100), student_age int, mobile_no varchar(20))
    end

我没有得到上面我提到的问题的答案。

kknvjkwl

kknvjkwl1#

错误太多了。这么多的错误让我觉得,你没有真正使用IBM Db2产品...

create procedure sp_student (
    @student varchar(50),
    @student_id int,
    @terms varchar(10),
    @subject1 varchar(100),
    @subject2 varchar(100),
    @total varchar(100),
    @student_name varchar(100),
    @student_age int,
    @mobile_no varchar(20)    
)
BEGIN
    if (@student = 'marks') then
        insert into marks 
        values (@student_id, @terms, @subject1 , @subject2 , @total);
    elseif (@student = 'students') then
        insert into students 
        values (@student_id ,@student_name, @student_age, @mobile_no);
    end if;
END

fiddle

brjng4g3

brjng4g32#

无法在value(..)中设置dataType:

create procedure sp_student 
    @student varchar(50),
    @student_id int,
    @terms varchar(10),
    @subject1 varchar(100),
    @subject2 varchar(100),
    @total varchar(100),
    @student_name varchar(100),
    @student_age int,
    @mobile_no varchar(20)
    
as 
 if (@student = 'marks') then
    insert into marks 
    values (@student_id, @terms, @subject1 , @subject2 , @total);
elseif (@student = 'students') then
    insert into students 
    values (@student_id ,@student_name, @student_age, @mobile_no);
end if;
    end

相关问题