请问下面这个 sql 语句哪里有错?

2017-07-09 22:47:50 +08:00
 kanilu

运行下面的 sql 语句 总是说 query 出错

    use employees;
    set autocommit=0;
    begin;
    SELECT * from employees where emp_no=10002;
    commit;
2433 次点击
所在节点    问与答
15 条回复
kanilu
2017-07-09 22:51:54 +08:00
喔,对了,上面的 employees 是一个数据库,里面有一个表也叫 employees
kanilu
2017-07-09 23:06:25 +08:00
顶顶顶
zhusimaji
2017-07-09 23:11:14 +08:00
你要贴具体的错误信息....
kanilu
2017-07-09 23:15:03 +08:00
@zhusimaji

SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * from employees where emp_no=10002;
commit' at line 2
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * from employees where emp_no=10002;
commit' at line 2
wdlth
2017-07-09 23:23:16 +08:00
开始事务是 begin transaction 吧,你输入的是 begin,就变成了 begin...end 流程控制了。
kanilu
2017-07-10 00:00:24 +08:00
@wdlth 改成 begin transaction
还是错误啊
misaka19000
2017-07-10 00:19:27 +08:00
查询还有事务吗,第一次看到
11138
2017-07-10 00:28:33 +08:00
语法没有问题,问题在于你的用法有问题,你应该帖出相关的使用代码。
kiwi95
2017-07-10 00:36:05 +08:00
不知道你是要用 begin-end 控制块还是要用事物,按说你就一个 select 不需要用事物。
如果用 begin-end 你的写法不对,如果用事物应该用 start transaction,不是 begin transaction。
给你贴个文档 https://dev.mysql.com/doc/refman/5.7/en/begin-end.html
kiwi95
2017-07-10 00:38:13 +08:00
既然是提问就多提供些信息,什么数据库,什么版本,具体报什么错,没人会去给你跑一下你的语句给你分析
11138
2017-07-10 00:39:52 +08:00
mysql> use employees;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * from employees where emp_no=10002;
+----+--------+
| id | emp_no |
+----+--------+
| 1 | 10002 |
+----+--------+
1 row in set (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> use employees;set autocommit=0;begin;SELECT * from employees where emp_no=10002;commit;
Database changed
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+----+--------+
| id | emp_no |
+----+--------+
| 1 | 10002 |
+----+--------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
11138
2017-07-10 00:45:33 +08:00
https://dev.mysql.com/doc/refman/5.7/en/commit.html
START TRANSACTION or BEGIN start a new transaction.
tomatoz
2017-07-10 04:37:34 +08:00
目测你把 commit 和其他语句写一起了。。
应该这样吧:
……
statement.execute("blablabla");
conn.commit();
……
另外查询操作也用不到事务 +1
heaton_nobu
2017-07-10 09:21:30 +08:00
这是在 commit 什么
leejanfin
2017-07-10 09:49:51 +08:00
单纯看这个 SQL 没发现什么问题

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/374136

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX