Home > Database > Mysql Tutorial > body text

What kind of SQL statements can be used to prepare statements?

WBOY
Release: 2023-09-04 17:41:02
forward
732 people have browsed it

What kind of SQL statements can be used to prepare statements?

In fact, it is impossible to prepare all SQL statements, because MySQL only allows the preparation of the following types of SQL statements:

SELECT statement

Example

mysql> PREPARE stmt FROM 'SELECT tender_value from Tender WHERE
Companyname = ?';
Query OK, 0 rows affected (0.09 sec)
Statement prepared

mysql> SET @A = 'Singla Group.';
Query OK, 0 rows affected (0.00 sec)

mysql> EXECUTE stmt using @A;
+--------------+
| tender_value |
+--------------+
|   220.255997 |
+--------------+
1 row in set (0.07 sec)

mysql> DEALLOCATE PREPARE stmt;
Query OK, 0 rows affected (0.00 sec)
Copy after login

INSERT, REPLACE, UPDATE and DELETE

Statements that modify data.

Example

mysql> PREPARE stmt1 FROM 'DELETE from Tender WHERE Sr = ?';
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> SET @A = 4;
Query OK, 0 rows affected (0.00 sec)

mysql> EXECUTE stmt1;
ERROR 1210 (HY000): Unknown error 1210

mysql> EXECUTE stmt1 using @A;
Query OK, 1 row affected (0.08 sec)

mysql> DEALLOCATE PREPARE stmt1;
Query OK, 0 rows affected (0.00 sec)

mysql> Select * from tender;
+----+---------------+--------------+
| Sr | CompanyName   | Tender_value |
+----+---------------+--------------+
| 1  | Abc Corp.     |   250.369003 |
| 2  | Khaitan Corp. |   265.588989 |
| 3  | Singla group. |   220.255997 |
+----+---------------+--------------+
3 rows in set (0.00 sec)
Copy after login

CREATE TABLE statement.

Example

mysql> PREPARE stmt3 FROM 'CREATE TABLE Student(Id INT, Name
Varchar(20))';
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE stmt3;
Query OK, 0 rows affected (0.73 sec)

mysql> DEALLOCATE PREPARE stmt3;
Query OK, 0 rows affected (0.00 sec)
Copy after login

SET, DO and many SHOW statements

Example

mysql> PREPARE stmt10 FROM 'SHOW TABLES';
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE stmt10;
+-------------------+
| Tables_in_query   |
+-------------------+
| emp               |
| emp123            |
| emp_t             |
| examination_btech |
| new_number        |
| student           |
| student_detail    |
| student_info      |
| tender            |
| website           |
+-------------------+
10 rows in set (0.00 sec)
Copy after login

The above is the detailed content of What kind of SQL statements can be used to prepare statements?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template