MySQL如何在没有过程/函数的情况下执行命令块 [英] MySQL how execute a command block without a procedure/function

查看:157
本文介绍了MySQL如何在没有过程/函数的情况下执行命令块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 MySQL Workbench 上运行一段 SQL 命令,就像我在 SQL Server 上所做的那样,但它说我:declare 在这个位置无效.

I am try to run a block of SQL commands on MySQL Workbench, like I do on SQL Server, but it is saying me: declare is not valid at this position.

我在网上看到了很多这样的例子,我真的不明白为什么会出现这个错误.

I saw various examples of this on web and I realy do not undertand why this error.

一些提示?

代码,其中SQL Server的例子没问题(运行完美):

The codes, where the example of SQL Server is ok (running perfectly):

MySQL                 | SQL Server
--------------------- | ------------------------
DECLARE A INT;        | DECLARE @A INT;        
DECLARE B INT;        | DECLARE @B INT;        
DECLARE RESULT INT;   | DECLARE @RESULT INT;        
                      |         
BEGIN                 | BEGIN        
  SET A = 1;          |   SET @A = 1;        
  SET B = 2;          |   SET @B = 2;        
  SET RESULT = A + B; |   SET @RESULT = @A + @B;        
END;                  | END;        

推荐答案

不幸的是,这在 MySQL 中不受支持.您只能在复合语句中使用 DECLARE(即在 BEGINE .. END 块内).因此,您只能将此类语句放在存储过程、触发器、事件和函数中.

Unfortunately, this is not supported in MySQL. You can use DECLARE only in compound statements (i.e. within BEGINE .. END blocks). Therefore, you can only place such statements in stored procedures, triggers, events and functions.

根据文档

本节描述了 BEGIN ... END 复合词的语法语句和其他可以在存储体中使用的语句程序:存储过程和函数、触发器和事件.这些对象是根据存储在服务器上的 SQL 代码定义的供以后调用(参见第 20 章,存储的程序和视图).

This section describes the syntax for the BEGIN ... END compound statement and other statements that can be used in the body of stored programs: Stored procedures and functions, triggers, and events. These objects are defined in terms of SQL code that is stored on the server for later invocation (see Chapter 20, Stored Programs and Views).

复合语句是一个可以包含其他块的块;变量、条件处理程序和游标的声明;和流动控制结构,例如循环和条件测试.

A compound statement is a block that can contain other blocks; declarations for variables, condition handlers, and cursors; and flow control constructs such as loops and conditional tests.

还有

DECLARE 只允许在 BEGIN ... END 复合语句中并且必须在它的开头,在任何其他语句之前.

DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

这篇关于MySQL如何在没有过程/函数的情况下执行命令块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆