How to throw an error in MySql procedure?

http://stackoverflow.com/questions/4862911/how-to-throw-an-error-in-mysql-procedure

What is the mechanism to force the MySQL to throw an error within the stored procedure?

I have a procedure which call s another function:

PREPARE my_cmd FROM @jobcommand; EXECUTE my_cmd; DEALLOCATE PREPARE my_cmd;

the job command is:

jobq.exec("Select 1;wfdlk# to simulatte an error");

then:

CREATE PROCEDURE jobq.`exec`(jobID VARCHAR(128),cmd TEXT) BEGIN DECLARE result INT DEFAULT 0; SELECT sys_exec( CONCAT('echo ',cmd,' | base64 -d > ', '/tmp/jobq.',jobID,'.sh ; bash /tmp/jobq.',jobID,'.sh &> /tmp/jobq.',jobID)) INTO result; IF result>0 THEN # call raise_mysql_error(result); END IF; END;

My jobq.exec is always succeeding. Are there way to rise an error? How to implement raise_mysql_error function??

BTW I am using MySQL 5.5.8

thanks Arman.

share improve this question
 

2 Answers

up vote 7down voteaccepted

Yes, there is: use the SIGNAL keyword.

share improve this answer
 
2 
Thank you! DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET err= 1 exactly that what I need!!! – Arman Feb 2 '11 at 11:11

You may use following stored procedure to emulate error-throwing:

CREATE PROCEDURE `raise`(`errno` BIGINT UNSIGNED, `message` VARCHAR(256)) BEGIN SIGNAL SQLSTATE 'ERR0R' SET MESSAGE_TEXT = `message`, MYSQL_ERRNO = `errno`; END

Example:

CALL `raise`(1356, 'My Error Message');

转载于:https://www.cnblogs.com/kungfupanda/p/5887410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 JavaScript 中发送 HTTP 请求的方法有多种,常见的方法包括使用 XMLHttpRequest 对象和使用 fetch API。 XMLHttpRequest 示例代码: ``` var xhr = new XMLHttpRequest(); xhr.open('GET', 'https://api.example.com/data.json', true); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) { console.log(xhr.responseText); } }; xhr.send(); ``` fetch API 示例代码: ``` fetch('https://api.example.com/data.json') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); ``` 注意:这只是最简单的 HTTP GET 请求的例子,实际使用中你需要根据需求进行更多的设置,例如设置请求头、发送请求体等。 ### 回答2: 在JavaScript中,可以使用内置的XMLHttpRequest对象来发送HTTP请求。下面是一个使用XMLHttpRequest对象发送GET请求的示例: ```javascript // 创建XMLHttpRequest对象 var xhr = new XMLHttpRequest(); // 设置请求方法和URL xhr.open('GET', 'http://example.com/api', true); // 设置响应类型 xhr.responseType = 'json'; // 发送请求 xhr.send(); // 当请求状态改变时执行回调函数 xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (xhr.status === 200) { // 请求成功,处理响应数据 console.log(xhr.response); } else { // 请求失败,处理错误 console.error(xhr.statusText); } } }; ``` 以上代码创建了一个XMLHttpRequest对象,并使用`open`方法设置了GET请求的URL和异步标志位。然后,通过`send`方法发送请求。 在`onreadystatechange`回调函数中,检查请求状态(`readyState`)和响应状态(`status`)来确定请求是否完成。如果`readyState`等于4,表示请求已完成;如果`status`等于200,表示请求成功。 你也可以使用第三方库,如axios或fetch,来简化HTTP请求。这些库提供了更简洁的API,并支持更多的功能,比如发送POST请求和设置请求头。 ### 回答3: 在JavaScript中进行HTTP请求有多种方法。最常见的是使用内置的XMLHttpRequest对象。 可以通过创建新的XMLHttpRequest实例来发送HTTP请求。以下是一个简单的示例: ```javascript var xhr = new XMLHttpRequest(); xhr.open('GET', 'https://api.example.com/data', true); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { console.log(xhr.responseText); } }; xhr.send(); ``` 上述代码创建了一个GET请求,向"https://api.example.com/data"地址发送请求。当请求状态改变时,将触发`onreadystatechange`事件处理函数。如果请求已成功完成(`readyState`为4,`status`为200),则在控制台打印响应的文本。 除了使用`open`和`send`方法之外,还可以通过`setRequestHeader`方法设置HTTP标头,以及通过`getResponseHeader`方法获取响应标头。 另一种进行HTTP请求的常见方法是使用基于Promise的fetch API。它提供了更简洁和现代的方式来发送HTTP请求。以下是使用fetch API发送GET请求的示例: ```javascript fetch('https://api.example.com/data') .then(function(response) { if (response.ok) { return response.text(); } throw new Error('Network response was not ok.'); }) .then(function(data) { console.log(data); }) .catch(function(error) { console.log('Error:', error.message); }); ``` 上述代码通过调用fetch函数发送GET请求,并返回一个Promise对象。然后可以根据响应的状态确定是否处理响应或抛出错误。最后,可以在控制台打印响应的文本。 这里只是展示了两种常见的进行HTTP请求的方法,JavaScript中还有其他的库和框架可以帮助处理HTTP请求,比如Axios、jQuery等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值