I am writing SQL query in MS Access. I came across a need for the following IF statement
IF (branch= 'TEST123') THEN (branch = '123' and subbranch='456')
ELSE branch = 'TEST0'
Looking in google, I only see the IIF statementmki
I am not sure if I can write my IF statement in IIF, correct? is there another way to do this?
I don't think I can write
iif( branch= 'TEST123', branch = '123' AND subbranch='456', 'TEST0')
Any help is appreciated.
Two ways:
UPDATE MyTable SET Branch = IIF([Branch] = 'TEST123','123',[Branch]),
SubBranch = IIF([Branch] = 'TEST123','456',[SubBranch ])
Or with just a simple WHERE
UPDATE MyTable SET Branch = '123', SubBranch = '456' WHERE Branch = 'TEST123'
2nd option is better.
try the below query
Update test set test.branch=iif(branch= "TEST123", "123", branch = "TEST0"), test.subbranch=iif(branch= "TEST123", "567", branch = "TEST0")
Related
I am using switch() function for a conditional Update in MS Access query. Below is my query
UPDATE T_Generated_OpportunityLine SET PlContact_c = Switch(
LowestlevelValue_c='PTFDS - FD ENCLOSURE SYSTEMS',Select OpportunityLine_PostCode.SESA from OpportunityLine_PostCode,T_Generated_OpportunityLine where OpportunityLine_PostCode.Postcode=T_Generated_OpportunityLine.Selected_Zip AND
OpportunityLine_PostCode.OpptyLine_Ref='PTFDS',
LowestlevelValue_c='DOOR ENTRY SYSTEM',Select OpportunityLine_PostCode.SESA from OpportunityLine_PostCode,T_Generated_OpportunityLine where OpportunityLine_PostCode.Postcode=T_Generated_OpportunityLine.Selected_Zip AND
OpportunityLine_PostCode.OpptyLine_Ref='Door Entry System'
);
I am aware with the syntax of switch() function:
Switch ( expression1, value1, expression2, value2, ... expression_n, value_n )
I have just made one modification i.e. instead of passing Static value for "Value" section I am fetching this value Dynamically through Select Query.
But when I am trying to execute the query Its stating an error message that:
The query must be an updatable query
I am sure that problem is for Select Statement inside Switch() function,but I need this conditional update on account of my project.
Can anyone provide any suitable solution to this? I also want to know can we pass dynamic value for "Value" Portion in Switch() syntax?
I don't know if this is your problem, but normally subqueries need to be enclosed in parentheses:
UPDATE T_Generated_OpportunityLine
SET PlContact_c = Switch(LowestlevelValue_c='PTFDS - FD ENCLOSURE SYSTEMS',
(Select SESA
from OpportunityLine_PostCode
where OpportunityLine_PostCode.Postcode = T_Generated_OpportunityLine.Selected_Zip AND
OpportunityLine_PostCode.OpptyLine_Ref = 'PTFDS'
),
LowestlevelValue_c = 'DOOR ENTRY SYSTEM',
(Select SESA
from OpportunityLine_PostCode
where OpportunityLine_PostCode.Postcode = T_Generated_OpportunityLine.Selected_Zip AND
OpportunityLine_PostCode.OpptyLine_Ref='Door Entry System'
));
However, I would write this with just one subquery:
UPDATE T_Generated_OpportunityLine
SET PlContact_c = (Select SESA
from OpportunityLine_PostCode as pc
where pc.Postcode = T_Generated_OpportunityLine.Selected_Zip AND
(T_Generated_OpportunityLine.LowestlevelValue_c = 'PTFDS - FD ENCLOSURE SYSTEMS' AND pc.OpptyLine_Ref = 'PTFDS' OR
T_Generated_OpportunityLine.LowestlevelValue_c = 'DOOR ENTRY SYSTEM' AND op.OpptyLine_Ref='Door Entry System'
)
);
I have this following query for CodeIgniter:
$q = $this->db->where('(message_from="'.$user_id.'" AND message_to="'.$this->auth_model->userdata['user_id'].'")')
->or_where('(message_from="'.$this->auth_model->userdata['user_id'].'" AND message_to="'.$user_id.'")')
->get('messages');
I want to write this query with completely active record.
I have tried something like this:
$from_where = array('message_from'=>$user_id, 'message_to'=>$this->auth_model->userdata['user_id']);
$to_where = array('message_from'=>$this->auth_model->userdata['user_id'],'message_to'=>$user_id);
$q = $this->db->where($from_where)
->or_where($to_where)
->get('messages');
die($this->db->last_query());
The above code produces this query:
SELECT * FROM (`messages`) WHERE `message_from` = '2' AND `message_to` = '1' OR `message_from` = '1' OR `message_to` = '2'
But this is what I want to produce:
SELECT * FROM (`messages`) WHERE (message_from="2" AND message_to="1") OR (message_from="1" AND message_to="2")
There are similar questions here and here, but thosedid not provide a real solution for me.
How's this possible, If not via core libraries, is there an extension which allows writing such queries?
Thanks,
You can use sub query way of codeigniter to do this for this purpose you will have to hack codeigniter. like this
Go to system/database/DB_active_rec.php Remove public or protected keyword from these functions
public function _compile_select($select_override = FALSE)
public function _reset_select()
Now subquery writing in available And now here is your query with active record
$this->db->where('message_from','2');
$this->db->where('message_to','1');
$subQuery1 = $this->db->_compile_select();
$this->db->_reset_select();
$this->db->where('message_from','1');
$this->db->where('message_to','2');
$subQuery2 = $this->db->_compile_select();
$this->db->_reset_select();
$this->db->select('*');
$this->db->where("$subQuery1");
$this->db->or_where("$subQuery2");
$this->db->get('messages');
Look at this answer of mine. This shows how to use sub queries. This will help
Using Mysql WHERE IN clause in codeigniter
EDITES
Yes i have done it
Rewrite the query this way exactly you want
$this->db->where('message_from','2');
$this->db->where('message_to','1');
$subQuery1 = $this->db->_compile_select(TRUE);
$this->db->_reset_select();
$this->db->where('message_from','1');
$this->db->where('message_to','2');
$subQuery2 = $this->db->_compile_select(TRUE);
$this->db->_reset_select();
$this->db->select('*');
$this->db->where("($subQuery1)");
$this->db->or_where("($subQuery2)");
$this->db->get('messages');
Compile select is with true parameter. Will not produce select clause. This will produce
SELECT * FROM (`messages`) WHERE (`message_from` = '2' AND `message_to` = '1') OR (`message_from` = '1' AND `message_to` = '2')
I've got the folloing tables:rashodz, naklrashodz, transport, trans_task_load, shipment_plan.
What I need is to update tbl:rashodz.id_ship whith tbl:shipment_plan.id_ship as follows
rashodz.id2 = shipment_plan.id2
and rashodz.nsthet = naklrashodz.nsthet
and naklrashodz.nsthet = trans_task_load.nsthet
and shipment_plan.idts = trans_task_load.idts
Just do not have an idea how to do it.
When there are two tables under consideration there is no problem, but how to do it when multiple tables are involved?
I would appreciate any help.
Is the code snippet you give the condition for the update?
Then how about:
update rashodz r
set id_ship = nvl(
(select id_ship
from shipment_plan,
naklrashodz,
transport,
trans_task_load
where r.id2 = shipment_plan.id2
and r.nsthet = naklrashod.nsthet
and naklrashod.nsthet = trans_task_load.nsthet
and shipment_plan.idts = trans_task_load.idts
), id_ship)
The purpose of the NVL in this case is that if there is no match, id_ship remains unchanged from its original value.
I am getting the following error while executing the following query in an Stored Procedure. Could anyone help in finding the fault?
UPDATE is not allowed because the statement updates view "sup_item" which participates in a join and has an INSTEAD OF UPDATE trigger.
UPDATE si
SET
name = mc.name,
sup_item_cat_id = mc.res_sup_item_cat_id,
xf_value = mc.xf_value,
ava_start_date = mc.ava_start_date,
ava_end_date = mc.ava_end_date,
status_code = mc.status_code,
last_mod_us_id = CASE WHEN mc.last_mod_us_id = 42 THEN #posting_us_id
ELSE mc.last_mod_us_id END,
last_mod_tsp = CURRENT_tsp
FROM sup_item AS si
JOIN merch_cat_imp_sup_item AS mc
ON mc.sup_id = si.sup_id
AND mc.res_sup_item_id = si.sup_item_id
AND mc.cat_imp_event_id = #cat_imp_event_id
AND mc.accept_flag = 'y'
WHERE si.shi_flag = 'n'
I found the reference: http://msdn.microsoft.com/en-us/library/ms177523.aspx
A view with an INSTEAD OF UPDATE trigger cannot be a target of an
UPDATE with a FROM clause.
So, I have to rewrite the UPDATE statement (it still can be in a procedure) to NOT use sup_item (which is a view), but keep the underlying table(s) as needed.
Could someone please rewrite it, if anyone knows what to do?
You can use MERGE to achieve this. Try:
MERGE INTO sup_item si
USING merch_cat_imp_sup_item AS mc
ON mc.sup_id = si.sup_id
AND mc.res_sup_item_id = si.sup_item_id
AND mc.cat_imp_event_id = #cat_imp_event_id
AND mc.accept_flag = 'y'
AND si.shi_flag = 'n'
WHEN MATCHED
THEN UPDATE
SET
name = mc.name,
sup_item_cat_id = mc.res_sup_item_cat_id,
xf_value = mc.xf_value,
ava_start_date = mc.ava_start_date,
ava_end_date = mc.ava_end_date,
status_code = mc.status_code,
last_mod_us_id = CASE WHEN mc.last_mod_us_id = 42 THEN #posting_us_id
ELSE mc.last_mod_us_id END,
last_mod_tsp = CURRENT_tsp
The issue is not within your query. As per comments on your question, the entity you are updating [sup_item], isn't actually a table, it's a view. That view has an INSTEAD OF UPDATE trigger on it.
Are you able to post the SQL for the View and for the Trigger(s)?
I would also be interested, because I have a stored procedure in a database that I have inherited which tries to do this. It won't let me create the sproc in SQL 2014, but the fact that it is there in the sproc indicates to me that an earlier version of SQL server must have allowed this.
Maybe in earlier versions your procedure operated on a table, which was later replaced by a view.
You should replace your "update from" syntax by standard ANSI syntax of update.
This is my SQL statement:
UPDATE sysdba.CONTACT, sysdba.ADDRESS
SET sysdba.ADDRESS.Address1 = '123 Tech Parway'
WHERE sysdba.CONTACT.AddressID = sysdba.ADDRESS.AddressID
AND sysdba.CONTACT.contactID = 'CRBD'
What is wrong here?
TSQL solution
UPDATE sysdba.ADDRESS
SET Address1 = '123 Tech Parway'
FROM sysdba.ADDRESS
INNER JOIN sysdba.CONTACT
ON sysdba.CONTACT.AddressID = sysdba.ADDRESS.AddressID
WHERE sysdba.CONTACT.contactID = 'CRBD'
I can't see any problem with this syntax according to MySQL UPDATE command reference. Maybe you're getting errors from MSSQLServer? Could you supply the errors for further investigation?