我正在尝试使用这样的分隔符在MySQL中创建存储过程:
use am;
DELIMITER $$
CREATE PROCEDURE addfields()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE acc INT(16);
DECLARE validId INT DEFAULT 0;
END $$
DELIMITER ;
它给了我一个错误:
#1304 - PROCEDURE addfields already exists
使用分隔符创建存储过程的正确语法是什么,如果它首先存在则删除它?
答案 0 :(得分:67)
优秀的程序员使用终端,GUI让你在中间变得柔软。当你习惯它并记住命令时,它比任何GUI快5倍。生产力=成功。
<强> 1。打开终端并登录mysql,如下所示:
el@apollo:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql>
<强> 2。看看你是否有任何程序:
mysql> show procedure status;
+-----------+---------------+-----------+---------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+-----------+---------------+-----------+---------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| yourdb | sp_user_login | PROCEDURE | root@% | 2013-12-06 14:10:25 | 2013-12-06 14:10:25 | DEFINER | | utf8 | utf8_general_ci | latin1_swedish_ci |
+-----------+---------------+-----------+---------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
1 row in set (0.01 sec)
我有一个定义,你可能没有开始。
第3。切换到数据库,删除它。
mysql> use yourdb;
Database changed
mysql> drop procedure if exists sp_user_login;
Query OK, 0 rows affected (0.01 sec)
mysql> show procedure status;
Empty set (0.00 sec)
<强> 4。好的,现在我没有定义存储过程。做最简单的一个:
mysql> delimiter //
mysql> create procedure foobar()
-> begin select 'hello'; end//
Query OK, 0 rows affected (0.00 sec)
当您输入存储过程的命令时,//将与终端通信。存储过程名称为foobar。它不需要参数,应该返回“你好”。
<强> 5。看看它是否在那里,记得放回你的分隔符!:
mysql> show procedure status;
->
->
疑难杂症!为什么这不起作用?您将分隔符设置为//
还记得吗?将其重新设置为;
<强> 6。重新设置分隔符并查看过程:
mysql> delimiter ;
mysql> show procedure status;
+-----------+--------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+-----------+--------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| yourdb | foobar | PROCEDURE | root@localhost | 2013-12-06 14:27:23 | 2013-12-06 14:27:23 | DEFINER | | utf8 | utf8_general_ci | latin1_swedish_ci |
+-----------+--------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
1 row in set (0.00 sec)
<强> 7。运行它:
mysql> call foobar();
+-------+
| hello |
+-------+
| hello |
+-------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Hello world complete,让我们用更好的东西覆盖它。
<强> 8。删除foobar,重新定义它以接受参数,然后重新运行它:
mysql> drop procedure foobar;
Query OK, 0 rows affected (0.00 sec)
mysql> show procedure status;
Empty set (0.00 sec)
mysql> delimiter //
mysql> create procedure foobar (in var1 int)
-> begin select var1 + 2 as result;
-> end//
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call foobar(5);
+--------+
| result |
+--------+
| 7 |
+--------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
尼斯!我们制作了一个接受输入,修改它并输出的过程。现在让我们做一个变量。
<强> 9。删除foobar,创建一个变量,运行它:
mysql> delimiter ;
mysql> drop procedure foobar;
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter //
mysql> create procedure foobar(out var1 varchar(100))
-> begin set var1="kowalski, what's the status of the nuclear reactor?";
-> end//
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call foobar(@kowalski_status);
Query OK, 0 rows affected (0.00 sec)
mysql> select @kowalski_status;
+-----------------------------------------------------+
| @kowalski_status |
+-----------------------------------------------------+
| kowalski, what's the status of the nuclear reactor? |
+-----------------------------------------------------+
1 row in set (0.00 sec)
<强> 10。在MySQL中使用INOUT的示例:
mysql> select 'ricksays' into @msg;
Query OK, 1 row affected (0.00 sec)
mysql> delimiter //
mysql> create procedure foobar (inout msg varchar(100))
-> begin
-> set msg = concat(@msg, " never gonna let you down");
-> end//
mysql> delimiter ;
mysql> call foobar(@msg);
Query OK, 0 rows affected (0.00 sec)
mysql> select @msg;
+-----------------------------------+
| @msg |
+-----------------------------------+
| ricksays never gonna let you down |
+-----------------------------------+
1 row in set (0.00 sec)
好的,它有效,它加在一起。因此,您定义了一个变量msg,将该变量传递给名为foobar的存储过程,并且@msg由foobar写入。
现在您知道如何使用分隔符创建存储过程。继续本教程,从存储过程中的变量开始:http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/
答案 1 :(得分:51)
以下是带有分隔符的示例 MYSQL存储过程以及如何调用..
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_user_login` $$
CREATE DEFINER=`root`@`%` PROCEDURE `sp_user_login`(
IN loc_username VARCHAR(255),
IN loc_password VARCHAR(255)
)
BEGIN
SELECT user_id,
user_name,
user_emailid,
user_profileimage,
last_update
FROM tbl_user
WHERE user_name = loc_username
AND password = loc_password
AND status = 1;
END $$
DELIMITER ;
并通过mysql_connection规范和
调用$loginCheck="call sp_user_login('".$username."','".$password."');";
它将返回程序的结果。
答案 2 :(得分:0)
我创建了一个简单的MySQL过程,如下所示:
DELIMITER //
CREATE PROCEDURE GetAllListings()
BEGIN
SELECT nid, type, title FROM node where type = 'lms_listing' order by nid desc;
END //
DELIMITER;
请遵循此步骤。创建过程之后,您可以看到相同的过程并执行它。
答案 3 :(得分:0)
import wx
class Main1(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent=parent)
self.bSizer1 = wx.BoxSizer(wx.VERTICAL)
self.Btn1 = wx.Button(self, wx.ID_ANY, "Btn1", wx.DefaultPosition, wx.DefaultSize, 0)
self.bSizer1.Add(self.Btn1, 1, wx.ALL|wx.EXPAND, 5)
self.SetSizer(self.bSizer1)
self.Btn1.Bind(wx.EVT_BUTTON, self.btn1_f)
def btn1_f(self, event):
print("here 1")
self.x = 12
self.y = 30
a = Other_a([self.x, self.y])
a.Show()
b = Other_b(self.x)
b.Show()
class Other_a(wx.Frame):
def __init__(self, foo):
self.foo = foo
wx.Frame.__init__(self, None)
self.bSizer2 = wx.BoxSizer(wx.VERTICAL)
self.Btn2 = wx.Button(self, wx.ID_ANY, "Btn 2a", wx.DefaultPosition, wx.DefaultSize, 0)
self.bSizer2.Add(self.Btn2, 0, wx.ALL|wx.EXPAND, 5)
self.SetSizer(self.bSizer2)
self.Btn2.Bind(wx.EVT_BUTTON, self.btn2_f)
print("here 2")
print(self.foo[0])
def btn2_f(self, event):
print("here 3")
print(self.foo[1])
class Other_b(wx.Frame):
def __init__(self, x):
self.x = x
wx.Frame.__init__(self, None)
self.bSizer3 = wx.BoxSizer(wx.VERTICAL)
self.Btn3 = wx.Button(self, wx.ID_ANY, "Btn 2b", wx.DefaultPosition, wx.DefaultSize, 0)
self.bSizer3.Add(self.Btn3, 0, wx.ALL|wx.EXPAND, 5)
self.SetSizer(self.bSizer3)
self.Btn3.Bind(wx.EVT_BUTTON, self.btn3_f)
print("here 4")
def btn3_f(self, event):
print("here 5")
print(self.x)
if __name__ == "__main__":
app = wx.App(False)
frame = Main1(None)
frame.Show(True)
app.MainLoop()
答案 4 :(得分:-1)
在mysql中创建过程语法: -
DELIMITER //
CREATE PROCEDURE `user_rank`(IN group_id int)
BEGIN
SET @total_like:=0, @rank:=0,@rnk :=0;
select * from (select IF(@total_like = total_like, @rank:=@rank, @rank:=@rank + 1) rank, @rnk:=@rnk + 1 as rank_offset, @total_like:=total_like as total_likes, user_id, user_name, thumb from (SELECT like_count as total_like, like_counts.user_id, user_name, thumb from like_counts inner join class_grades ON (class_grades.user_id = like_counts.user_id) inner join class_groups ON (class_groups.class_id = user_grades.grade_id and class_groups.class_group_id = group_id ) where role_id = '8' and like_counts.status = '1' and country_code ='+91' group by like_counts.user_id order by total_like desc LIMIT 10 OFFSET 0) r) t, (SELECT @total_like:=0, @rank:=0, @rnk:=0) s;
END //
DELIMITER ;