在存储过程中显示消息

问题描述 投票:0回答:5

如何显示存储过程中的消息?

显示行是否存在的消息的正确语法是什么?

在 SQL Server 中,它是 PRINT 以在 WORKBENCH 中显示消息蝙蝠...

CREATE PROCEDURE `new_proced` (
    in myid     int(3)
)
BEGIN
    if not exists (select id from table where id = myid)
then
    show message 'Row no exists';
else
    show message 'Row exists';
end if;
END
mysql stored-procedures mysql-workbench
5个回答
26
投票

不完全确定为什么你会想做这样的事情,但你可以这样做: ...

then
  select 'YOUR MESSAGE HERE'  
else
  select 'YOUR OTHER MESSAGE HERE'
end if

或者你可以选择1或0,可能会好一点...


2
投票

MySQL 中没有像 Oracle 那样提供正确的输出语句,我们有 DBMS_OUTPUT.PUT_LINE。因此,要在控制台上显示任何消息,您可以使用 SELECT 语句:

SELECT message;

例如:

SELECT "WELCOME TO MySQL";


1
投票

要调试 MySQL 中存储过程的信息,可以通过以下选项来执行此操作。

1.外部写入文件:

选择“your_message”作为登录到输出文件“/temp/brajesh.txt”;

2.使用选择命令打印消息:

选择“结果消息”;

3.使用选择命令打印带有消息的附加信息:

选择 concat("你好!:", 结果);

4.创建附加表temp并将所有消息推送到其中:

插入临时选择 concat(结果);

示例

drop procedure if exists display_name_procedure;
delimiter //
create procedure display_name_procedure(IN name_val varchar(65))
begin
declare result varchar(65);
set result := display_name_function(name_val);

create table if not exists temp (name_val varchar(65) not null);
insert into temp select concat(result);
select "penguin" as log into outfile '/temp/brajesh.txt';
select concat("Hello ! :", result);

end//

delimiter ;

1
投票

完整的过程是在此代码中构建的。您可以根据您的要求修改此代码。

CREATE PROCEDURE insertData
    @Title nvarchar(max),
    @Releasedate datetime2(7),
    @genre nvarchar(max),
    @Price decimal(18,2)
AS
BEGIN
   
    IF NOT EXISTS (SELECT 1 FROM Movie WHERE Title = @Title)
    BEGIN
 
        INSERT INTO Movie (Title, Releasedate, Genre, Price)
        VALUES (@Title, @Releasedate, @genre, @Price);
    END
    ELSE
    BEGIN
   
        SELECT 'Already exists' AS Status;
    END
END

-1
投票
分隔符 //
 
创建程序

insert_data_student1(student_name varchar(50),email varchar(50),group_id int)

 开始

     如果不存在(选择来自学生的电子邮件,其中电子邮件=“电子邮件”)
     然后
         插入学生(学生姓名,电子邮件,组ID)值(学生姓名,电子邮件,组ID);
     别的
          选择“所有重放都存在”;

     万一;
 结尾 //
分隔符;
© www.soinside.com 2019 - 2024. All rights reserved.