I want to update a record in a table but based on a condition I will either update one column or another but I do not want to have 2 separate statements because the statements are very long and detailed.
Here is the basic idea with over simplification to get to the point.
PROCEDURE Animal_something(p_updater VARCHAR2)
begin
if p_updater = 'person' then
-- I want to update the modified_by
else
-- if p_updater = 'a process' I want to update modified_by_process
Update table_creatures
set animal_type = 'Dog ,
**modified_by** = 'Bob'
**or do this**
**modified_by_process =** 'creature_package'
where animal_legs = '4'
I don't want:
if p_updater = 'person' then
Update table_creatures
set animal_type = 'Dog ,
modified_by = 'Bob'
where animal_legs = '4';
else
Update table_creatures
set animal_type = 'Dog ,
modified_by_process = 'creature_package'
where animal_legs = '4';
end;
UPDATE table_creatures
SET animal_type = 'Dog',
modified_by = CASE p_updater WHEN 'person' THEN 'Bob' ELSE modified_by END,
modified_by_process = CASE p_updater WHEN 'process' THEN 'creature_package' ELSE modified_by_process END
WHERE animal_legs = 4
You could use dynamic SQL, e.g.:
PROCEDURE Animal_something(p_updater VARCHAR2)
sql_string_pt1 VARCHAR2(2000) := 'UPDATE table_creatures SET animal_type = :1';
sql_string_pt2 VARCHAR2(2000) := NULL;
sql_string_pt3 VARCHAR2(2000) := ' WHERE animal_legs = :3';
begin
if p_updater = 'person' then
sql_string_pt2 := ', modified_by = :2';
else
sql_string_pt2 := ', modified_by_process = :2';
end if;
EXECUTE IMMEDIATE sql_string_pt1 || sql_string_pt2 || sql_string_pt3
USING 'Dog', 'Bob', '4';
end;
This has two advantages over Quassnoi's answer: use of bind variables, and not needing to update both columns on every execution, which would generate redo even though the actual value is not changed.
On the downside, the statement is not validated at all at compile time.
UPDATE table_creatures
SET animal_type = 'Dog',
modified_by = DECODE(p_updater , 'person' , 'BOB' ,
'proces' , 'creature_package' ,
'GIVE DEFAULT VALUE')
WHERE animal_legs = 4;
You can try this.
Related
I have nested for loop as shown below and trying to perform the update statement, but the update command is not working
CREATE or REPLACE FUNCTION public.udf_unit_test_output() RETURNS SETOF text AS
$$
DECLARE
v_expected_proc_name varchar(100);
v_expected_log_severity varchar(25);
v_expected_log_text varchar(8000);
v_expected_occurence int;
v_actual_occurence int;
record_list record;
distinct_unit_test_uuid uuid;
BEGIN
for distinct_unit_test_uuid in
SELECT DISTINCT unit_test_uuid FROM public.unit_test_expected_results
loop
for record_list in select record_id
from public.unit_test_expected_results
WHERE unit_test_uuid = distinct_unit_test_uuid
loop
SELECT proc_name, log_severity, log_text, occurence
FROM public.unit_test_expected_results
WHERE record_id = record_list.record_id
INTO v_expected_proc_name,
v_expected_log_severity,
v_expected_log_text,
v_expected_occurence;
SELECT COUNT(*)
FROM public.unit_test_output uto
WHERE unit_test_uuid = distinct_unit_test_uuid
AND proc_name = v_expected_proc_name
AND log_severity = v_expected_log_severity
AND log_text like v_expected_log_text
INTO v_actual_occurence;
UPDATE public.unit_test_expected_results
SET pass_fail = (CASE WHEN v_expected_occurence = v_actual_occurence
THEN 'PASS' ELSE 'FAIL'
END)
WHERE record_id = record_list.record_id;
RETURN QUERY SELECT is(v_expected_occurence, v_actual_occurence,
'Should contain '||'proc name: ' || v_expected_proc_name || ' severity type: '
|| v_expected_log_severity || ' and log text: ' || v_expected_log_text);
end loop;
end loop;
RETURN;
END;
$$
LANGUAGE plpgsql;
It doesn't update any of the row ?
I converting this Oracle trigger to SQL Server. In Oracle there are using if condition for updating a table. I was wondering is it necessary to use if statement in SQL Server. I'm thinking to use in where clause. Please let me know if that's correct?
Thanks
CREATE OR REPLACE TRIGGER "DB1"."TG_PG_CODES"
BEFORE UPDATE OF "str_number", "op_status"
ON "DB1"."Header_T"
REFERENCING OLD AS oldrow NEW AS newrow
FOR EACH ROW
BEGIN
IF :newrow.op_status = 'X'
THEN
UPDATE DB1.pg_pollutant
SET op_status = 'X',
str_comp_status = '98',
str_mod_person = '3327'
WHERE str_number = :newrow.str_number;
END IF;
IF :newrow.op_status <> 'X'
AND SUBSTR (:oldrow.str_pgcodes, 1, 1) = '1'
AND SUBSTR (:newrow.str_pgcodes, 1, 1) = '0'
THEN
UPDATE DB1.pg_pollutant
SET op_status = 'X',
str_comp_status = '98'
WHERE str_number = :newrow.str_number
AND str_pollutant_key = :newrow.str_number || '0';
END IF;
END;
=============================================
In sql server
CREATE TRIGGER [dbo].[TG_TG_PG_CODES] ON [dbo].[Header_T]
AFTER UPDATE
AS
IF(UPDATE(str_number)
OR UPDATE(op_status))
BEGIN
IF(
(
SELECT op_status
FROM inserted
) = 'X')
BEGIN
UPDATE a
SET
a.op_status = 'X',
a.str_comp_status = '98',
a.str_mod_person = '3327'
FROM DB1.pg_pollutant a
INNER JOIN inserted AS i ON a.str_number = i.str_number;
END;
UPDATE a
SET
a.op_status = 'X',
a.str_comp_status = '98'
FROM DB1.pg_pollutant a
INNER JOIN inserted AS i ON a.[str_number] = i.[str_number]
AND a.str_pollutant_key = i.[str_number]+'0'
INNER JOIN Deleted AS d ON a.[str_number] = d.[str_number]
WHERE i.stroperationalstatus <> 'X'
AND SUBSTRING(d.str_pgcodes, 1, 1) = '1'
AND SUBSTRING(i.str_pgcodes, 1, 1) = '0'; /* I used this condition in where clause */
END;
I'm writing a stored procedure and it's working for if but not for else. For if I'm getting the correct ID value but for else it's just giving me null.
SELECT #ID = ID FROM PRODUCTS WHERE SN = #SN
SELECT #Chip_ID = Chip_ID FROM PRODUCTS WHERE SN = #SN
SELECT #Power = Power From Module_Cycle WHERE ChipID = #Chip_ID
SELECT #Test_ID=Test_ID FROM TestEE_Mod WHERE ID=#ID AND #TypeID = TypeID
IF(#Test_ID IS NOT NULL)
BEGIN
IF(#TypeID = '3')
BEGIN
SELECT #Temp_TestID=TestID FROM TempCycle WHERE ChipID = #Chip_ID AND #Power = 'false'
BEGIN
UPDATE TestEE_Mod SET Temp_TestID = #Temp_TestID WHERE ID = #ID AND TypeID = #TypeID
END
END
ELSE
BEGIN
SELECT #Temp_TestID=TestID FROM TempCycle WHERE ChipID = #Chip_ID AND #Power = 'true'
BEGIN
UPDATE TestEE_Mod SET Temp_TestID = #Temp_TestID WHERE ID = #ID AND TypeID = #TypeID
END
END
END
Most likely the issue lies in the #Power variable. In your two SELECT statements, the only difference is that one includes #Power = 'false' in the WHERE clause, while the other includes #Power = 'true'.
Since #Power is a variable, not an actual column in your TempCycle table, I'm guessing you actually meant to filter by Power = #Power in both of them.
I'm very new to FireBird, but I want to know how I can use a select statement as part of my conditional criteria. I feel like I've been to the internet in back trying to find a way to do this, but haven't come up with much. Below is my attempt at getting this to work. Thanks in advance for any help.
SET TERM ^ ;
ALTER PROCEDURE sp_test (
IPADD Varchar(32),
HN Varchar(32),
NOTE Varchar(200) )
RETURNS ( update_count integer )
AS
BEGIN
IF((SELECT COUNT(*)
FROM ADDRESSES a
WHERE a.ADDRESS_TYPE = 'Reserved'
AND a.ALIVE = 'N'
AND (a.HOST_NAME = '' OR a.HOST_NAME is NULL)
AND (a.DNS_NAME = '' OR a.DNS_NAME is NULL)
AND (a.SYSTEM_NAME = '' OR a.SYSTEM_NAME is NULL)) > 0)
THEN
UPDATE
ADDRESSES a
SET
a.HOST_NAME = :HN,
a.ADDRESS_TYPE = 'Assigned',
a.NOTES = :NOTE
WHERE
a.SHORT_IP_ADDRESS = :IPADD;
update_count = 1;
SUSPEND;
ELSE
update_count = 0;
SUSPEND;
END^
SET TERM ; ^
GRANT EXECUTE
ON PROCEDURE sp_test TO SYSDBA;
Using COUNT to check is there records to update is not the best way, use EXISTS instead, ie your IF would be
IF(EXISTS(SELECT 1 FROM ADDRESSES a
WHERE a.ADDRESS_TYPE = 'Reserved'
AND a.ALIVE = 'N'
AND (a.HOST_NAME = '' OR a.HOST_NAME is NULL)
AND (a.DNS_NAME = '' OR a.DNS_NAME is NULL)
AND (a.SYSTEM_NAME = '' OR a.SYSTEM_NAME is NULL)))
THEN
But there seems to be a problem with your return value, update_count - you return 1 if you execute the UPDATE, but the actual number of rows affected by the statement might be something else. I suggest you use ROW_COUNT context variable instead. So your procedure would be
ALTER PROCEDURE sp_test (
IPADD Varchar(32),
HN Varchar(32),
NOTE Varchar(200) )
RETURNS ( update_count integer )
AS
BEGIN
IF(EXISTS(SELECT 1 FROM ADDRESSES a
WHERE (a.ADDRESS_TYPE = 'Reserved')
AND (a.ALIVE = 'N')
AND (a.HOST_NAME = '' OR a.HOST_NAME is NULL)
AND (a.DNS_NAME = '' OR a.DNS_NAME is NULL)
AND (a.SYSTEM_NAME = '' OR a.SYSTEM_NAME is NULL)))
THEN BEGIN
UPDATE ADDRESSES a SET
a.HOST_NAME = :HN,
a.ADDRESS_TYPE = 'Assigned',
a.NOTES = :NOTE
WHERE a.SHORT_IP_ADDRESS = :IPADD;
update_count = ROW_COUNT;
END ELSE update_count = 0;
SUSPEND;
END^
trigger error:=ORA-04079: invalid
trigger specification
the code causing that error is below:
CREATE OR REPLACE TRIGGER TRGBILLINGADDRESS
AFTER INSERT OR DELETE OR UPDATE ON TBLMACCOUNTADDRESS
add1 wom.tbltaddress.address1%TYPE;
add2 wom.tbltaddress.address2%TYPE;
cityid wom.tbltaddress.city_id%TYPE;
stateid wom.tbltaddress.state_id%TYPE;
pincd wom.tbltaddress.pincode%TYPE;
BEGIN
SELECT address1,address2,city_id,state_id,pincode
INTO add1,add2,cityid,stateid,pincd FROM wom.tbltaddress ta WHERE ta.ID IN (
SELECT vbac.billing_address_id
FROM wom.vw_billaddresschange vbac, wom.tbltaddress ita
WHERE vbac.billing_address_id = ita.ID
AND vbac.lcid = parlcid);
IF add1 = :NEW.address1 AND add2 = :NEW.address2 AND cityid = :NEW.cityid AND stateid = :NEW.stateid AND pincode = :NEW.zip THEN
dbms_output.put_line('Address Already Exist in tbltaddress table');
ELSE
UPDATE wom.tbltaddress ta
SET ta.address1 = :NEW.address1,
ta.address2 = :NEW.address2,
ta.city_id = :NEW.cityid,
ta.country_id = 'CTR0001',
ta.state_id = :NEW.stateid,
ta.pincode = :NEW.zip
WHERE ta.ID IN (
SELECT vbac.billing_address_id
FROM wom.vw_billaddresschange vbac, wom.tbltaddress ita
WHERE vbac.billing_address_id = ita.ID
AND vbac.lcid = parlcid);
END IF;
END;
You should use DECLARE before declaring your variables.
CREATE OR REPLACE TRIGGER TRGBILLINGADDRESS
AFTER INSERT OR DELETE OR UPDATE ON TBLMACCOUNTADDRESS
DECLARE
add1 wom.tbltaddress.address1%TYPE;
add2 wom.tbltaddress.address2%TYPE;
cityid wom.tbltaddress.city_id%TYPE;
stateid wom.tbltaddress.state_id%TYPE;
pincd wom.tbltaddress.pincode%TYPE;
BEGIN