how to set sql mode variable as a local variable in mysql - mysql

What i want thing is, i want to set the sql_mode variable as a local variable, not as a session or global variable. Reason to do that is i want to disapear the change of sql mode variable after one of query was executed. Below session and global are worked well, but this is not the what i want. Global one is kept the sql mode as a empty one forever. Session one is kept the sql mode as a empty one until connection close. I want thing is, keep the sql mode until a quarry is executed only.
mysql> set global sql_mode='';
mysql> set session sql_mode='';
mysql query :-
SELECT tc_exe_grp_num,tcs.tc_tc_id,tcs.tcs_id
FROM tc_exe_res tcer
INNER JOIN tcs tcs
ON tcs.tcs_id = tcer.tcs_tcs_id
WHERE tcs.tc_tc_id='1'
AND tcs.tc_tc_id='1'
GROUP BY tc_exe_grp_num
ORDER BY tc_exe_grp_num ;
got the idea from this article
please help me.

##sql_mode is session variable, not a local variable.
It is possible to retrieve the current setting of sql_mode, and save it in a user-defined variable, and then later set sql_mode back to the original setting.
For example:
-- save current setting of sql_mode in user defined variable
-- change sql_mode to desired setting
SET #SAVE_sql_mode = ##sql_mode ;
SET ##sql_mode = 'NO_ENGINE_SUBSTITUTION' ;
-- subsequent operations run under new setting of sql_mode
SELECT '...';
-- set sql_mode back to saved setting
SET ##sql_mode = #SAVE_sql_mode ;

I couldn't find a direct answer for this, but there is a solution,
First set the "sql mode" as a empty one and after quarry was executed set the "sql mode" with what previously had values, try it in below way,
set session sql_mode='';
SELECT tc_exe_grp_num,tcs.tc_tc_id,tcs.tcs_id FROM tc_exe_res tcer INNER JOIN tcs tcs ON tcs.tcs_id = tcer.tcs_tcs_id WHERE tcs.tc_tc_id='1' AND tcs.tc_tc_id='1' group by tc_exe_grp_num ORDER BY tc_exe_grp_num ;
set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

Related

Default global sql_mode setting for server

I have the sql_mode on a shared server set to
ONLY_FULL_GROUP_BY,​STRICT_TRANS_TABLES,​NO_ZERO_IN_DATE,​NO_ZERO_DATE,​ERROR_FOR_DIVISION_BY_ZERO,​NO_AUTO_CREATE_USER,​NO_ENGINE_SUBSTITUTION
What would happen if I removed these variables, and it remains blank? Would it affect other users? Some users want the sql_mode blank, while my thinking is that they should set their own modes using their code.
session_start();
include_once("./includes/conn.php");
mysql_query("SET SESSION sql_mode = ''");
The whole point of asking this question is to solve the problem at the level its occurring rather than altering a global setting

Disable strict sql mode for a inline query in mysql

Can some how i could Disable strict sql mode for a stored procedure or inline query in mysql?
I just wanted to either disable for single sp/query or a single database.
What i have tried is
set sql_mode='' ;INSERT INTO system_log(appname, action, level, thread_id, context,
context_id,market_id, message,transaction_id,primary_msisdn,primary_issuer
,secondary_msisdn,secondary_issuer, merchant_id,acquirer_id)
VALUES(
#appname, #action,#level, #thread_id, #context
, #context_id
,#market_id
,CASE WHEN #message = 'NULL' THEN NULL ELSE #message END
,#transaction_id
,#primary_msisdn
,#primary_issuer
,#secondary_msisdn,#secondary_issuer, #merchant_id,#acquirer_id
);
You have to set the session version of the sql_mode server system variable:
SET SESSION sql_mode = ''; --no mode set
After that you can restore sql_mode by setting it to the appropriate value.
However, I would rather consider rewriting the stored procedure so that you do not have to change the sql mode.

Execute update command in MySql while overriding sql_safe_updates option

I need to run a script with update commands which do not specify the record IDs, so I know I must turn the flag of sql_safe_updates to OFF.
Yet, I need to restore the flag to its previous state.
Is there a way to hold the flag's value in a temporary parameter, then run my updates and restore it to its initial value?
(I will need to keep the script for deployment procedure, so I cannot change the flag's value manually every time it runs)
UPDATE:
That is about the script I have:
SET SQL_SAFE_UPDATES = 0;
UPDATE offensedb.offenses set Status = 6 , ClosingReason = 1 WHERE Status = 1;
SET SQL_SAFE_UPDATES = 1;
After examining a dump file of mysql I came across these commands:
SET #OLD_SQL_MODE=##SQL_MODE
-- some other sql commands there...
SET SQL_MODE=#OLD_SQL_MODE
So I figured out the answer to my question would be:
SET #SQL_SAFE_UPDATES=##SQL_SAFE_UPDATES;
UPDATE offensedb.offenses set Status = 6 , ClosingReason = 1 WHERE Status = 1;
SET SQL_SAFE_UPDATES = #SQL_SAFE_UPDATES;

Cannot set a global variable on MySQL

I'm using MySQL in localhost (in ubuntu and also in windows). I want to set a global variable, and I have tried in all ways but even though I get an "ok" message from mysql, then when I do the "select #var" it allways says "NULL".
I've tried:
set global var=17;
set #global.var=17;
set ##var=17;
Can anyone help me?
Thanks in advance.
ps: I have the SUPER privilege.
The variable name var does not reference a valid system variable. The GLOBAL and SESSION keywords in the SET statement are used for specifying the scope when setting MySQL system variables, not MySQL user variables.
Try for example:
SELECT ##global.net_read_timeout ;
SET GLOBAL net_read_timeout = 45 ;
SELECT ##global.net_read_timeout ;
http://dev.mysql.com/doc/refman/8.0/en/set-statement.html
http://dev.mysql.com/doc/refman/5.5/en/set-statement.html
According to the MySQL 5.0 Reference Manual:
User-defined variables are session-specific. That is, a user variable
defined by one client cannot be seen or used by other clients. All
variables for a given client session are automatically freed when that
client exits.
You could consider using an extension like MySQL Global User Variables UDF (old archived link) to use global (persistent shared) variables.
On MySQL, you cannot create custom global or session system variables but can change existed global or session system variables as shown below:
SET GLOBAL max_connections = 1000; -- Existed global system variable
SET SESSION sql_mode = 'TRADITIONAL'; -- Existed session system variable
And, you can create user-defined(custom) variables which are removed when you exit(log out) a session as shown below. User-defined variables exist only in the current session so they cannot be seen by other sessions unless you use performance_schema.user_variables_by_thread:
SET #first_name = 'John', #last_name = 'Smith';

mysql can't change a session variable group_concat_max_len

In MySQL help it says that "Setting a session variable requires no special privilege, but a client can change only its own session variables, not those of any other client."
I try to increase the size of group_concat_max_len like this:
SET ##group_concat_max_len = 9999;
In phpmyadmin, the response is positive: 'Your SQL query has been executed successfully'.
Then I check the value like this (in the same window, 2 seconds later):
SHOW SESSION VARIABLES;
And unfortunately, group_concat_max_len = 1024
I am not the admin of this MySQL server, but if changing session variable does not require special privilege, then it should work. On my localhost it works.
Is there any chance to set this variable or at least to know why it can't be changed?
In phpmyadmin it is not guaranteed, that 2 queries (even if they are separated only by a few seconds) go to the same session. So chances are, SET ##group_concat_max_len = 9999; went to one session, but SHOW SESSION VARIABLES; to another.
If you try from the mysql command line client, this will work as expected.