mysql動態sql
1. Mysql觸發器怎麼變相使用動態SQL
一般mysql觸發器變相使用動態sql可以通過如下方法:
1、一個是直接調用存儲過程,因為mysql的存儲過程還是支持動態sql的
2、一個是通過MyBatis直接動態傳入參數包括表名、欄位名等等
2. 如何執行mysql動態SQL
連接字元串用CONCAT,在用
prepare s1 from @x;
execute s1;
執行
3. mysql 如何給變數賦一個動態sql執行的結果
DECLAREdt_idvarchar(32);
SET@sqls=CONCAT('SELECTid
FROM
w_volume_detail
WHERE
v_id="',dd_id,'"ANDuidISNULLLIMIT0,1');
executeimmediate@sqlsintodt_id;
4. MySQL觸發器怎麼變相使用動態SQL
MySQL觸發器動態sql: CREATE TRIGGER trigger1 after INSERT ON cdr FOR EACH ROW BEGIN DECLARE id BIGINT; DECLARE vname VARCHAR(200); DECLARE destination VARCHAR(200) DEFAULT 'null'; DECLARE rows1 BIGINT DEFAULT 0; DECLARE sql1 VARCHAR(2000); SELECT v.vendorid,v.name, ve.DestinationID INTO id, vname, destination FROM (select name, v.VendorID from vendor v natural join vendorendpoints where endpoint=NEW.Field27) v NATURAL JOIN (SELECT * FROM vendorextraction order by length desc) ve WHERE substr(NEW.Field8 , 1 ,ve.length)=substr(CONCAT(ve.Prefix,ve.OperatorList),1,ve.length) limit 0,1; SET @@sql_mode='ansi'; SET sql1 =CONCAT('insert into `',CONCAT(vname,id),'` values(',id,',"',vname,'","',NEW.Field5,'","',NEW.Field35,'","',NEW.Field12,'","',NEW.Field8,'","',NEW.Field0,'")'); IF (id >0) THEN call testing(sql1); ELSE SET sql1 =CONCAT('insert into `unknown` values(',id,',"',vname,'","',NEW.Field5,'","',NEW.Field35,'","',NEW.Field12,'","',NEW.Field8... MySQL觸發器動態sql:
CREATE TRIGGER trigger1 after INSERT ON cdr
FOR EACH ROW
BEGIN
DECLARE id BIGINT;
DECLARE vname VARCHAR(200);
DECLARE destination VARCHAR(200) DEFAULT 'null';
DECLARE rows1 BIGINT DEFAULT 0;
DECLARE sql1 VARCHAR(2000);
SELECT v.vendorid,v.name, ve.DestinationID INTO id, vname, destination
FROM (select name, v.VendorID from vendor v natural join vendorendpoints where endpoint=NEW.Field27) v NATURAL JOIN (SELECT * FROM vendorextraction order by length desc) ve
WHERE
substr(NEW.Field8 , 1 ,ve.length)=substr(CONCAT(ve.Prefix,ve.OperatorList),1,ve.length) limit 0,1;
SET @@sql_mode='ansi';
SET sql1 =CONCAT('insert into `',CONCAT(vname,id),'` values(',id,',"',vname,'","',NEW.Field5,'","',NEW.Field35,'","',NEW.Field12,'","',NEW.Field8,'","',NEW.Field0,'")');
IF (id >0) THEN
call testing(sql1);
ELSE
SET sql1 =CONCAT('insert into `unknown` values(',id,',"',vname,'","',NEW.Field5,'","',NEW.Field35,'","',NEW.Field12,'","',NEW.Field8,'","',NEW.Field0,'")');
call testing(sql1);
END IF;
5. MySQL中寫動態SQL語句出錯,請指正
set @str='select ? from ?';
修改為
set @str= CONCAT ( 'select Sno from ', str1) ;
後面的
set @a=Sno;
set @b=str1;
刪除。
execute stmt1 using @a,@b;
修改為
execute stmt1
6. MySQL存儲過程中實現執行動態SQL語句的方法
本文實例講述了MySQL存儲過程中實現執行動態SQL語句的方法。分享給大家供大家參考。具體實現方法如下:
mysql>
mysql>
delimiter
$$
mysql>
mysql>
CREATE
PROCEDURE
set_col_value
->
(in_table
VARCHAR(128),
->
in_column
VARCHAR(128),
->
in_new_value
VARCHAR(1000),
->
in_where
VARCHAR(4000))
->
->
BEGIN
->
DECLARE
l_sql
VARCHAR(4000);
->
SET
l_sql=CONCAT_ws('
',
->
'UPDATE',in_table,
->
'SET',in_column,'=',in_new_value,
->
'
WHERE',in_where);
->
SET
@sql=l_sql;
->
PREPARE
s1
FROM
@sql;
->
EXECUTE
s1;
->
DEALLOCATE
PREPARE
s1;
->
END$$
Query
OK,
0
rows
affected
(0.00
sec)
希望本文所述對大家的MySQL資料庫程序設計有所幫助。
7. MySQL中能執行動態SQL嗎
可以的。
set v_sql= '動態SQL';
set @v_sql=v_sql; --將動態SQL字元串賦值給一個變數(可以之前沒有定義,但要以@開頭)
prepare stmt from @v_sql; --預處理需要執行的動態SQL,其中stmt是一個變數
EXECUTE stmt; --執行SQL語句
deallocate prepare stmt; --釋放掉預處理段
8. MySQL中能執行動態SQL嗎
mysql> PREPARE stmt1 FROM 'SELECT SQRT(POW(?,2) + POW(?,2)) AS hypotenuse';
mysql> SET @a = 3;
mysql> SET @b = 4;
mysql> EXECUTE stmt1 USING @a, @b;
http://dev.mysql.com/doc/refman/5.0/en/sqlps.html
9. MySQL觸發器怎麼變相使用動態SQL
1、一個是直接調用存儲過程,因為mysql的存儲過程還是支持動態sql的
2、一個是通過MyBatis直接動態傳入參數包括表名、欄位名等等
10. MySQL中能執行動態SQL嗎
存儲過程中可以!
drop PROCEDURE if exists my_procere;
create PROCEDURE my_procere()
BEGIN
declare my_sqll varchar(500);
set my_sqll='select * from aa_list';
set @ms=my_sqll;
PREPARE s1 from @ms;
EXECUTE s1;
deallocate prepare s1;
end;