Calling a procedure from a procedure - mysql

I'm trying to call a procedure inside procedure, however the calling procedure is in a different db schema so it's a variable and isn't translating.
DECLARE client_database varchar(255);
SET client_database = get_database(_client_id); --this gets the schema the procedure is in
CALL client_database.client_procedure(); --then trying to call the procedure with the db schema variable however it's not translating
Question: Why isn't client_database translating?

One option is to use a 13.5 Prepared SQL Statement Syntax:
mysql> DROP PROCEDURE IF EXISTS `client_procedure`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP PROCEDURE IF EXISTS `server_procedure`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP DATABASE IF EXISTS `client_db`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DROP DATABASE IF EXISTS `server_db`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE DATABASE IF NOT EXISTS `client_db`;
Query OK, 1 row affected (0.00 sec)
mysql> CREATE DATABASE IF NOT EXISTS `server_db`;
Query OK, 1 row affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `client_db`.`client_procedure`()
-> BEGIN
-> SELECT CONCAT('FROM `', DATABASE(), '`');
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE PROCEDURE `server_db`.`controller_procedure`()
-> BEGIN
-> DECLARE `client_database` VARCHAR(64);
->
-> -- This gets the schema the procedure is in
-> -- SET `client_database` := `get_database`(`client_id`);
-> SET `client_database` := 'client_db';
->
-> SET #`call` := CONCAT('CALL `', `client_database`, '`.`client_procedure`');
-> PREPARE `stmt` FROM #`call`;
-> EXECUTE `stmt`;
-> DEALLOCATE PREPARE `stmt`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `server_db`.`controller_procedure`;
+-----------------------------------+
| CONCAT('FROM `', DATABASE(), '`') |
+-----------------------------------+
| FROM `client_db` |
+-----------------------------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
See db-fiddle.

Related

Mysql Error #1064 near Null in call stored procedure with out param

I'm quite new with MySQL. I have to call a stored procedure with output param. I've searched a lot on internet but I've not found the correct solution to my problem. If I call the stored procedure with the #outputParamName it says that I have an error #1064 near NULL. If I call the procedure with the 'outputParamName' without the # it says thath it is not an OUT or INOUT correct param. Someone can help me please?
the stored procedure just have to check if surname and name in DB exists on the same row:
CREATE PROCEDURE InsertProc (INOUT existsInDb BOOLEAN,
IN dbName VARCHAR(50)
IN tableName VARCHAR(50)
IN surnameNew VARCHAR(50)
IN nameNew VARCHAR(50))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
BEGIN
DECLARE rowSurnameName int;
SET #sqlSel = CONCAT('SELECT COUNT(*) INTO ', rowSurnameName, ' FROM ', dbName, '.', tableName, ' WHERE COGNOME=', surnameNew, ' AND NOME=', nameNew);
PREPARE stmtSel FROM #sqlSel;
EXECUTE stmtSel;
DEALLOCATE PREPARE stmtSel;
IF (rowSurnameName=0) THEN
SET #sqlIns = CONCAT('INSERT INTO ', dbName, '.', tableName, ' (NOME, COGNOME) VALUES (', nameNew, ', ', surnameNew,')');
PREPARE stmtIns FROM #sqlIns;
EXECUTE stmtIns;
DEALLOCATE PREPARE stmtIns;
SELECT false INTO existsInDb;
ELSE SELECT true INTO existsInDb;
END IF;
END
The CALL Statement is:
SET #dbName = 'DBNAME';
SET #tableName = 'DBTABLE';
SET #surname = 'SURNAME';
SET #name = 'NAME';
PREPARE s FROM 'CALL InsertProc(?,?,?,?,?)';
EXECUTE s USING #existsInDB, #dbName, #tableName, #surname, #name;
SELECT #existsInDB;
And the ERROR Line is:
#1064 - 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 'NULL' at line 1
A couple of notes:
You can't use a local variable in a prepared statement.
C.1 Restrictions on Stored Programs
...
SELECT ... INTO local_var cannot be used as a prepared statement.
...
The error shown in your question occurs because the local variable rowSurnameName has the value NULL, see:
mysql> DROP PROCEDURE IF EXISTS `InsertProc`;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `InsertProc`()
-> BEGIN
-> DECLARE `rowSurnameName` INT;
-> SELECT `rowSurnameName`;
-> SET #`sqlSel` := CONCAT('SELECT COUNT(*) INTO ', `rowSurnameName`);
-> SELECT #`sqlSel`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `InsertProc`;
+------------------+
| `rowSurnameName` |
+------------------+
| NULL |
+------------------+
1 row in set (0.00 sec)
+-----------+
| #`sqlSel` |
+-----------+
| NULL |
+-----------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
If you try to use the rowSurnameName local variable in the prepared statement, you will get the error:
mysql> DROP PROCEDURE IF EXISTS `InsertProc`;
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `InsertProc`()
-> BEGIN
-> DECLARE `rowSurnameName` INT;
-> SET #`sqlSel` := CONCAT('SELECT 100 INTO `rowSurnameName`');
-> SELECT #`sqlSel`;
-> PREPARE `stmtSel` FROM #`sqlSel`;
-> EXECUTE `stmtSel`;
-> DEALLOCATE PREPARE `stmtSel`;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `InsertProc`;
+----------------------------------+
| #`sqlSel` |
+----------------------------------+
| SELECT 100 INTO `rowSurnameName` |
+----------------------------------+
1 row in set (0.00 sec)
ERROR 1327 (42000): Undeclared variable: rowSurnameName
You need to use 9.4 User-Defined Variables in your prepared statement:
mysql> DROP PROCEDURE IF EXISTS `InsertProc`;
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `InsertProc`()
-> BEGIN
-> SET #`sqlSel` := CONCAT('SELECT 100 INTO #`rowSurnameName`');
-> SELECT #`sqlSel`;
-> PREPARE `stmtSel` FROM #`sqlSel`;
-> EXECUTE `stmtSel`;
-> DEALLOCATE PREPARE `stmtSel`;
-> IF (#`rowSurnameName` = 0) THEN
-> SELECT 'NotExistsInDbAndInsert';
-> ELSE
-> SELECT 'existsInDb';
-> END IF;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> CALL `InsertProc`;
+-----------------------------------+
| #`sqlSel` |
+-----------------------------------+
| SELECT 100 INTO #`rowSurnameName` |
+-----------------------------------+
1 row in set (0.00 sec)
+------------+
| existsInDb |
+------------+
| existsInDb |
+------------+
1 row in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)

Unable to use SIGNAL and INSERT at the same time

I have a problem with this trigger. When I use the INSERT and the SIGNAL statements inside the IF condition, the insertion is not performed. However, without SIGNAL, the insertion is done. Anyone have an explanation for that? My main concern is that I need both the insertion and the SIGNAL statement to cancel the main insertion (the insertion that throws the trigger)
DELIMITER //
CREATE TRIGGER log_venta BEFORE INSERT ON `venta_producto`
FOR EACH ROW BEGIN
DECLARE value int;
DECLARE valor_venta int;
DECLARE saldo_cliente int;
DECLARE cliente_id int;
SELECT cliente INTO cliente_id FROM venta WHERE id=NEW.ventaID;
SELECT credito INTO saldo_cliente FROM cliente WHERE no_cliente=cliente_id;
SELECT precio INTO valor_producto FROM producto WHERE id=NEW.producto;
SET valor_venta = NEW.cantidad*valor_producto;
IF valor_venta > saldo_cliente THEN
INSERT INTO log(cliente) VALUES (cliente_id);
SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'ERROR';
END IF;
END
//
DELIMITER ;
Thanks
14.6.7.5 SIGNAL Syntax
. . .
SIGNAL is the
way to “return” an error.
. . .
21.3.1 Trigger Syntax and Examples
. . .
For transactional tables, failure of a statement should cause rollback of all changes performed by the statement. Failure of a
trigger causes the statement to fail, so trigger failure also causes
rollback. For nontransactional tables, such rollback cannot be done,
so although the statement fails, any changes performed prior to the
point of the error remain in effect.
. . .
Mentioned above can be demonstrated in the following example:
mysql> DROP TABLE IF EXISTS `venta_producto`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `log`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `venta_producto` (
-> `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> `cliente_id` INT UNSIGNED
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE TABLE IF NOT EXISTS `log` (
-> `cliente_id` INT UNSIGNED
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE TRIGGER `log_venta` BEFORE INSERT ON `venta_producto`
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO `log` (`cliente_id`) VALUES (NEW.`cliente_id`);
-> SIGNAL SQLSTATE '02000' SET MESSAGE_TEXT = 'ERROR';
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> INSERT INTO `venta_producto`
-> (`cliente_id`)
-> VALUES
-> (1);
ERROR 1643 (02000): ERROR
mysql> SELECT
-> `id`,
-> `cliente_id`
-> FROM
-> `venta_producto`;
Empty set (0.00 sec)
mysql> SELECT
-> `cliente_id`
-> FROM
-> `log`;
Empty set (0.00 sec)
mysql> ALTER TABLE `log` ENGINE=MyISAM;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> INSERT INTO `venta_producto`
-> (`cliente_id`)
-> VALUES
-> (2);
ERROR 1643 (02000): ERROR
mysql> SELECT
-> `id`,
-> `cliente_id`
-> FROM
-> `venta_producto`;
Empty set (0.00 sec)
mysql> SELECT
-> `cliente_id`
-> FROM
-> `log`;
+------------+
| cliente_id |
+------------+
| 2 |
+------------+
1 row in set (0.00 sec)

MYSQL - SELECT data from dynamic table names

Imagine that I have several dynamic table names such as :
select table_name
from INFORMATION_SCHEMA.TABLES
where table_name like 'ifhcraw%';
ifhcraw_2016_03_25_13
ifhcraw_2016_03_26_19
ifhcraw_2016_03_28_2
And I don't found any rule on names. To find last edited table I have just select last modified table with query :
select table_name
from INFORMATION_SCHEMA.TABLES
where table_name like 'ifhcraw%' and
update_time = (select max(update_time)
from INFORMATION_SCHEMA.TABLES
where table_name like 'ifhcraw%');
Now the purpose all of this step to get data from table_name.
I have tried to use variables , however it was failed. For example :
SET #query1 := 'select table_name
from INFORMATION_SCHEMA.TABLES
where table_name like \'ifhcraw%\' and
update_time = (select max(update_time)
from INFORMATION_SCHEMA.TABLES
where table_name like \'ifhcraw%\') ';
SET #query2 := concat('select *
from ', #query1);
PREPARE stmt from #query2;
execute stmt;
Please help to solve issue.
Try:
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.11 |
+-----------+
1 row in set (0.00 sec)
mysql> DROP TABLE IF EXISTS `ifhcraw_2016_03_25_13`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `ifhcraw_2016_03_26_19`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `ifhcraw_2016_03_28_2`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE `ifhcraw_2016_03_25_13` (
-> `id` INT
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE `ifhcraw_2016_03_26_19` (
-> `id` INT
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE `ifhcraw_2016_03_28_2` (
-> `id` INT
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> SET #`TABLE_NAME` := NULL;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT `TABLE_NAME` INTO #`TABLE_NAME`
-> FROM `INFORMATION_SCHEMA`.`TABLES`
-> WHERE `TABLE_NAME` LIKE 'ifhcraw%' AND
-> `UPDATE_TIME` = (SELECT MAX(`UPDATE_TIME`)
-> FROM `INFORMATION_SCHEMA`.`TABLES`
-> WHERE `TABLE_NAME` LIKE 'ifhcraw%'
-> );
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> SELECT #`TABLE_NAME`;
+---------------+
| #`TABLE_NAME` |
+---------------+
| NULL |
+---------------+
1 row in set (0.00 sec)
mysql> SET #`qry` := IF(#`TABLE_NAME` IS NULL,
-> 'SELECT NULL',
-> CONCAT('SELECT * FROM ', #`TABLE_NAME`));
Query OK, 0 rows affected (0.00 sec)
mysql> PREPARE `stmt` FROM #`qry`;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE `stmt`;
+------+
| NULL |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
mysql> DEALLOCATE PREPARE `stmt`;
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO `ifhcraw_2016_03_26_19`
-> (`id`)
-> VALUES
-> (1);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT `TABLE_NAME` INTO #`TABLE_NAME`
-> FROM `INFORMATION_SCHEMA`.`TABLES`
-> WHERE `TABLE_NAME` LIKE 'ifhcraw%' AND
-> `UPDATE_TIME` = (SELECT MAX(`UPDATE_TIME`)
-> FROM `INFORMATION_SCHEMA`.`TABLES`
-> WHERE `TABLE_NAME` LIKE 'ifhcraw%'
-> );
Query OK, 1 row affected (0.00 sec)
mysql> SELECT #`TABLE_NAME`;
+-----------------------+
| #`TABLE_NAME` |
+-----------------------+
| ifhcraw_2016_03_26_19 |
+-----------------------+
1 row in set (0.00 sec)
mysql> SET #`qry` := IF(#`TABLE_NAME` IS NULL,
-> 'SELECT NULL',
-> CONCAT('SELECT * FROM ', #`TABLE_NAME`));
Query OK, 0 rows affected (0.00 sec)
mysql> PREPARE `stmt` FROM #`qry`;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql> EXECUTE `stmt`;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
mysql> DEALLOCATE PREPARE `stmt`;
Query OK, 0 rows affected (0.00 sec)
UPDATE
Care should be taken when there are two or more tables that match the criteria, will fail as follows:
mysql> INSERT INTO `ifhcraw_2016_03_26_19`
-> (`id`)
-> VALUES
-> (1);
Query OK, 1 row affected (0,00 sec)
mysql> INSERT INTO `ifhcraw_2016_03_28_2`
-> (`id`)
-> VALUES
-> (1);
Query OK, 1 row affected (0,00 sec)
mysql> SELECT `TABLE_NAME`
-> FROM `INFORMATION_SCHEMA`.`TABLES`
-> WHERE `TABLE_NAME` LIKE 'ifhcraw%' AND
-> `UPDATE_TIME` = (SELECT MAX(`UPDATE_TIME`)
-> FROM `INFORMATION_SCHEMA`.`TABLES`
-> WHERE `TABLE_NAME` LIKE 'ifhcraw%'
-> );
+-----------------------+
| TABLE_NAME |
+-----------------------+
| ifhcraw_2016_03_26_19 |
| ifhcraw_2016_03_28_2 |
+-----------------------+
2 rows in set (0,00 sec)
mysql> SELECT `TABLE_NAME` INTO #`TABLE_NAME`
-> FROM `INFORMATION_SCHEMA`.`TABLES`
-> WHERE `TABLE_NAME` LIKE 'ifhcraw%' AND
-> `UPDATE_TIME` = (SELECT MAX(`UPDATE_TIME`)
-> FROM `INFORMATION_SCHEMA`.`TABLES`
-> WHERE `TABLE_NAME` LIKE 'ifhcraw%'
-> );
ERROR 1172 (42000): Result consisted of more than one row
You should handle the case as you deem appropriate.

setting variable in case of exception

I have a stored procedure which will set my customized error as well as set an out variable to some value . I tried the following procedure .
***delimiter //
drop procedure if exists test_dalpu //
create procedure test_dalpu(out out_value int)
begin
DECLARE EXIT HANDLER FOR SQLSTATE '22001'
BEGIN
set out_value = 1;
SIGNAL SQLSTATE '22001' SET
MYSQL_ERRNO = 2,
MESSAGE_TEXT = 'Too long ';
END;
insert into abc_test values ( 5,"eerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");***
end//
delimiter ;
Then after executing
**call test_dalpu(#out_value);**
I got the correct output as Error code 2 Too long as expected, But after that If I do
select #out_value;
Am getting the value as null instead of 1 .
Is it the correct way?
Try:
mysql> DROP PROCEDURE IF EXISTS `test_dalpu`;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP TABLE IF EXISTS `abc_test`;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE IF NOT EXISTS `abc_test` (
-> `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> `description` CHAR(10)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER //
mysql> CREATE PROCEDURE `test_dalpu`(`out_value` VARCHAR(64))
-> BEGIN
-> DECLARE EXIT HANDLER FOR SQLSTATE '22001'
-> BEGIN
-> ROLLBACK;
-> SET #`set_variable` := CONCAT('SET #`', `out_value`, '` := 1');
-> PREPARE `stmt` FROM #`set_variable`;
-> EXECUTE `stmt`;
-> DEALLOCATE PREPARE `stmt`;
-> SIGNAL SQLSTATE '22001' SET
-> MYSQL_ERRNO = 2,
-> MESSAGE_TEXT = 'Too long';
-> END;
-> START TRANSACTION;
-> INSERT INTO `abc_test`
-> (`description`)
-> VALUES
-> ('ABCDEFGHIJK');
-> COMMIT;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> DELIMITER ;
mysql> SET #`out_value` := NULL;
Query OK, 0 rows affected (0.00 sec)
mysql> CALL `test_dalpu`('out_value');
ERROR 2 (22001): Too long
mysql> SHOW WARNINGS;
+-------+------+----------+
| Level | Code | Message |
+-------+------+----------+
| Error | 2 | Too long |
+-------+------+----------+
1 row in set (0.00 sec)
mysql> SELECT #`out_value`;
+--------------+
| #`out_value` |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)

Mysql Triggers, declare a variable and use it as column name

I have the table "mytable" that contains the "columnname" field wich is the name of a column in mytable2.
I use this one for the selection:
SET #DptScn = (SELECT columnname FROM mytable WHERE tablename = 'CustomTableName' AND fieldlabel = 'CustomField');
SET #identifiedid=144;
but, when I try:
SELECT #DptScn FROM mytable2 WHERE identifiedid = #identifiedid;
this give me NOT the content of the field but the name containted into variable #DptScn...
Any advice?
I can't use Prepared Statement because I'm in a Trigger...
UPDATE:
As suggested by spencer7593 I'm creating a procedure:
DROP PROCEDURE IF EXISTS p_t;
DELIMITER $$
CREATE PROCEDURE p_t (IN DptTcn VARCHAR(255), IN tid INT, OUT tT INT)
BEGIN
SET #DptTcn = DptTcn;
SET #tid = tid;
SET #sql = CONCAT('SELECT #DptTcn FROM mytable3 WHERE tid = #tid');
PREPARE stmt FROM #sql;
EXECUTE stmt;
END$$
DELIMITER ;
Then I try it:
SET #DptTcn = (SELECT columnname mytable WHERE tablename = 'CustomTableName' AND fieldlabel = 'CustomField');
SET #identifiedid=145;
CALL proc_ticket(#DptTcn, #identifiedid, #DptT);
But I receive a:
#2014 - Commands out of sync; you can't run this command now
One option to consider is creating a PROCEDURE that makes use of prepared statements, and then calling the the stored procedure from the trigger.
The SQL statement you execute to get the value from a particular column MUST have the column_name specified in the SQL text; this can't be derived "dynamically" in the execution of the statement.
To achieve something like this, you'll need to run two separate statements; one to get the column_name; the second to "SELECT column_name FROM". And the MySQL provided mechanism for executing that second query is a prepared statement.
Followup
Here's an example. I tried to get this built in SQLFiddle, but wasn't able to get it working (it just hung. So, here's the output from a mysql command line client instead.
(All of the statements below use the same delimiter // because we can't use a semicolon as a delimiter for the stored procedure. In SQLFiddle, we have to use the same delimiter on all statements, and the // just happens to be one of the options in SQLFiddle.)
mysql> DELIMITER //
mysql> CREATE PROCEDURE foo(IN colname VARCHAR(255), IN id INT, OUT val VARCHAR(255))
-> BEGIN
-> -- handler for "Unknown column" and "No data" exceptions
-> DECLARE EXIT HANDLER FOR 1054, 1329 BEGIN SET val = NULL; END;
-> SET #sql = CONCAT('SELECT ',colname,' INTO #val FROM t WHERE id = ',id,' LIMIT 1');
-> PREPARE stmt FROM #sql;
-> EXECUTE stmt;
-> SET val = #val;
-> END//
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE t (id INT, attr VARCHAR(4), ball VARCHAR(4))//
Query OK, 0 rows affected (0.11 sec)
mysql> INSERT INTO t VALUES (1, 'abcd','efgh'),(2,'ijkl','mnop')//
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> CALL foo('attr',1,#attr_1)//
Query OK, 0 rows affected (0.00 sec)
mysql> CALL foo('attr',2,#attr_2)//
Query OK, 0 rows affected (0.00 sec)
mysql> CALL foo('ball',1,#ball_1)//
Query OK, 0 rows affected (0.00 sec)
mysql> CALL foo('ball',2,#ball_2)//
Query OK, 0 rows affected (0.00 sec)
mysql> CALL foo('attr',777,#err_bad_id)//
Query OK, 0 rows affected (0.00 sec)
mysql> CALL foo('badcol',1,#err_badcol)//
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT #attr_1
-> , #attr_2
-> , #ball_1
-> , #ball_2
-> , #err_bad_id
-> , #err_badcol//
+---------+---------+---------+---------+-------------+-------------+
| #attr_1 | #attr_2 | #ball_1 | #ball_2 | #err_bad_id | #err_badcol |
+---------+---------+---------+---------+-------------+-------------+
| abcd | ijkl | efgh | mnop | NULL | NULL |
+---------+---------+---------+---------+-------------+-------------+
1 row in set (0.00 sec)
mysql> DELIMITER ;
you should create a SP and give the column name.
create proc dbo.TestGetData(#DptScn nvarchar(256))
as
begin
set nocount on
DECLARE #SQL NVARCHAR(MAX)
SET #SQL = 'SELECT #DptScn FROM mytable2 WHERE identifiedid = 144'
exec sp_executesql #SQL, N'#DptScn nvarchar(256)', #DptScn =#DptScn
end
Then
exec dbo.TestGetData 'Column1'