I wanted to SET a value into a VARCHAR but I don't understand how to combine it.
DECLARE id INTEGER;
DECLAR action VARCHAR(100);
SET id = (SELECT MAX(ID) FROM Test.Test);
SET action = "This is a test" + id;
Somehow I am losing the textpart "This is a test".
Is this normal behaviour for MySQL, am I doing something wrong ?
You can use MySQL functions.
SET action = CONCAT('This is a test',id);
Related
i try to create some trigger for my web database but there is not working code for my trigger here the problem
BEGIN
DECLARE t INT;
DECLARE b INT;
DECLARE ir INT;
DECLARE tgl INT;
DECLARE kol VARCHAR(3);
SET tgl = "(SELECT RIGHT(booking_checkin, 2) FROM pt_bookings WHERE booking_id = NEW.booking.id)";
SET ir = "(SELECT booked_room_id FROM pt_booked_rooms WHERE booking_id = NEW.booking.id)";
SET b = "(SELECT MID(booking_checkin , 6,2) FROM pt_bookings WHERE booking_id = NEW.booking_id)";
SET t = "SELECT IF((SELECT LEFT(booking_checkin, 4) FROM pt_bookings WHERE booking_id = NEW.booking_id) = 2019 ,'0','1')";
SET kol = "d" + tgl;
UPDATE pt_rooms_availabilities
set kol = kol + (SELECT `booking_nights` FROM `pt_bookings` WHERE booking_id = NEW.booking_id)
WHERE room_id = ir AND y = t AND m= b;
END
the problem is var kol do not read as a column name
maybe anyone can help me ?
It is not possible to do this in a trigger. In general, you could use dynamic sql to use variable column names, see e.g. Dynamic conversion of string into column name. This is however not allowed in stored functions or triggers.
You could use if then else to have a different insert statement for each situation, something like if tgl = 7 then update ... set d7 = d7 + ... elseif tgl = 122 then update ... set d122 = d122 + ....
In most situations though, you can and should avoid this by table design.
I am not sure what you are trying to do exactly, but you could just add a column tglcode to your table, store that value there and then add where tglcode = tgl to your update statement. It looks as if you (or your predecessor) did that or something similar with the variables b and t stored in columns m and t.
If that is a viable solution (or a proper table design) for your problem is hard to say though without more information. But you definitely cannot use the variable kol the way you did in a trigger.
Stored Proc Definition:
DECLARE dbName varchar(255);
DECLARE tableName varchar(255);
DECLARE fullPath varchar(255);
DECLARE conditions varchar(255);
SET dbName = idbname;
SET tableName = itablename;
SET fullPath = CONCAT("'",dbName,"'",'.',"'",tableName,"'");
SET checkExists = 0;
I am creating a stored proc where the dbname and tablename are dynamic, however I am stuck on the select aspect of this query.
I am trying to repalce the _test.user with values passed into the stored proc.
SELECT count(*) INTO checkExists FROM `_test`.`user` WHERE id = 1;
However this line throws an error
SELECT count(*) INTO checkExists FROM fullPath WHERE id = 1;
Error:
Procedure execution failed
1146 - Table 'dbname.fullpath' doesn't exist
I have also tried CONCAT() like this
set conditions = CONCAT('SELECT count(*) INTO ',checkExists, ' FROM ', fullPath, ' WHERE id=', 1);
However I can't figure out even how to use this in a select? Help is appreciated.
I like to do these modifications using replace(). Something like this:
replace(replace('SELECT count(*) INTO checkExists FROM `<dbname>`.`<tname>` WHERE id = 1',
'<tname>', v_tablename
), '<dbname>', v_databasename
)
You may also want to use v_fullpath somewhere. I'm not really sure what query you actually want to create.
I'm not sure why you have a variable called checkExists, when it seems to be the destination file. However, I would suggest that you prepend all your local variables with something to distinguish them from column names.
I have recently been able to produce a procedure where if a variable is not set I can set it to null. Now I am now looking to have multiple variables, but if a value has not been set to that variable, for it then to return all rows.
BEGIN
DECLARE ps_Project_Leader VARCHAR(15);
DECLARE ps_RD_Plan VARCHAR (15);
DECLARE ps_Approval_Status VARCHAR (15);
DECLARE ps_Design_Plan VARCHAR (15);
SET ps_Project_Leader = ifnull(Project_Leader,null);
SET ps_RD_Plan = ifnull(RD_Plan,null);
SET ps_Approval_Status = ifnull(Approval_Status,null);
SET ps_Design_Plan = ifnull(Design_Plan,null);
SELECT pp.pid,
pp.description,
pp.approval_status,
pp.design_plan,
pp.rd_plan,
pp.estimated_completion,
pp.project_leader,
pp.actual_completion
FROM project_register pp
WHERE pp.project_leader =Project_Leader
OR Project_Leader is null
and pp.rd_plan =RD_Plan
OR RD_Plan is null
and pp.approval_status = Approval_Status
OR Approval_Status is null
and pp.design_plan = Design_Plan
OR Design_Plan is null
and
PP.actual_completion is null;
end
For instance if i have set 2 of the variables and not the other 2, I do not want it to search on the variables that have not been set.
Many Thanks in advance, if i have not made complete sense (i am new to this so i appologies) I will be happy to clear things up.
You need to parenthesize your WHERE expression correctly:
WHERE (pp.project_leader = ps_Project_Leader
OR ps_Project_Leader is null)
and (pp.rd_plan = ps_RD_Plan
OR ps_RD_Plan is null)
and (pp.approval_status = ps_Approval_Status
OR ps_Approval_Status is null)
and (pp.design_plan = ps_Design_Plan
OR ps_Design_Plan is null)
and PP.actual_completion is null;
because AND has higher precedence than OR.
You aren't referencing the local variables, only the procedure arguments. (It doesn't look like you actually need local variables.)
I prefer to use parens around the AND and OR predicates, even if they aren't required. I never have to lookup if AND or OR takes precedence when I use parens, because it doesn't matter, because I'm always specifying the precedence.
I'd help the reader out, and format my SQL like this:
WHERE ( pp.project_leader = Project_Leader OR Project_Leader IS NULL )
AND ( pp.rd_plan = RD_Plan OR RD_Plan IS NULL )
AND ( pp.approval_status = Approval_Status OR Approval_Status IS NULL )
AND ( pp.design_plan = Design_Plan OR Design_Plan IS NULL )
That way, each line is a "check" of a single column, which is either enabled (with a non-NULL value) or disabled with NULL value.
Really just personal preference, I just find it easier to read that way, even if the line is a little bit longer, I'd rather have the check all one one line.
Again, the local variables aren't needed.
But, you could just set local variables equal to the parameter values, and then reference the local variables in your SQL statement. That really helps out when a variable has the same name as a column, because if the are named the same, MySQL is going to assume it's a reference to column name rather than a variable name. Using a local variable gives you a chance to rename it so it won't be confused with a column name.
UPDATE
I just noticed that the parameter variables names ARE the same as the column names, and that's going to be a problem.
You want your variable names to be DIFFERENT than the column names. You want to make sure that the datatypes of the variables match the columns... later, when you change a column from VARCHAR(15) to VARCHAR(30), you'll need to revisit the procedure and change the definitions of the procedure arguments as well as the local variables.
BEGIN
-- local variable names are DISTINCT from any column name
-- in any table referenced by a query these are used in
DECLARE ps_Project_Leader VARCHAR(15);
DECLARE ps_RD_Plan VARCHAR(15);
...
-- copy parameter values to local variables
SET ps_Project_Leader = Project_Leader ;
SET ps_RD_Plan = RD_Plan ;
...
-- query references local variable names
...
WHERE ( pp.project_leader = ps_Project_Leader OR ps_Project_Leader IS NULL )
AND ( pp.rd_plan = ps_RD_Plan OR ps_RD_Plan IS NULL )
...
I thought this would be a simple task but i cannot figure out why this gives me an error in my syntax. Any help is appreciated.
DECLARE #usernameid VARCHAR(20);
declare #UserIDParam VARCHAR(20);
SET #usernameid = 'myid';
SET #UserIDParam =
(SELECT userid
FROM tblusers
WHERE unid = usernameid);
SELECT *
FROM tblusers
WHERE tblusers.userID = #useridparam
One doesn't DECLARE user variables: one just uses them. (You DECLARE local variables e.g. in a procedure).
Is it possible to set a varaible to a query result such as:
DECLARE result INT;
SET result = (SELECT index FROM table WHERE data = 'xxxx' LIMIT 1);
Assuming of course you know that there will only be one result set
only within a procedure.