Pass value to sql with prepare : PREPARE « Command MySQL « SQL / MySQL






Pass value to sql with prepare

       
mysql>
mysql>
mysql>
mysql> CREATE   TABLE TEAMS
    ->         (TEAMNO         INTEGER      NOT NULL,
    ->          EmployeeNO       INTEGER      NOT NULL,
    ->          DIVISION       CHAR(6)      NOT NULL,
    ->          PRIMARY KEY    (TEAMNO)             );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>
mysql> INSERT INTO TEAMS VALUES (1,  6, 'first');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO TEAMS VALUES (2, 27, 'second');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> PREPARE S2 FROM 'SELECT * FROM TEAMS WHERE TEAMNO = @TNO';
Query OK, 0 rows affected (0.00 sec)
Statement prepared

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

mysql>
mysql> EXECUTE S2;
+--------+------------+----------+
| TEAMNO | EmployeeNO | DIVISION |
+--------+------------+----------+
|      1 |          6 | first    |
+--------+------------+----------+
1 row in set (0.00 sec)

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

mysql>
mysql> EXECUTE S2;
+--------+------------+----------+
| TEAMNO | EmployeeNO | DIVISION |
+--------+------------+----------+
|      2 |         27 | second   |
+--------+------------+----------+
1 row in set (0.00 sec)

mysql>
mysql> drop table teams;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
    
  








Related examples in the same category

1.Prepare command
2.Pass two value to prepare