Update more than one row in oracle - sql

I want to update the table values by using hard coded values.here is my code :
BEGIN
UPDATE emp_table
SET expiry_dt = TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),
WHERE emp_id = '78629160';
UPDATE emp_table
SET expiry_dt = TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),
WHERE emp_id = '78629160';
END
I want to do it in the single update statement. Can anyone tell me the solution?

UPDATE emp_table SET expiry_dt = TO_DATE
('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),
WHERE emp_id IN ('78629160','111020102','88888888');
Should do you. Edited the IN clause with various employee id's as yours were identical.

UPDATE emp_table
SET expiry_dt =
CASE
WHEN emp_id = '78629160' THEN TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS')
WHEN emp_id = '78629161' THEN TO_DATE('21.10.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS')
END
WHERE emp_id IN ('78629160', '78629161')
I assume the fact that you have the same ID twice was just a copy and paste error, just like the fact that both dates are identical.
Btw: what data type is emp_id? If that is a numeric type, get rid of the single quotes for the literals (numeric literals should not be quoted). They will prevent the usage of an index on that column!

Begin UPDATE emp_table SET expiry_dt = TO_DATE ('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS'),-- why you are using ',' here??
WHERE emp_id = '78629160';
Begin
UPDATE emp_table SET expiry_dt = TO_DATE('21.09.2009:00:00:01','DD.MM.YYYY:HH24:MI:SS')
WHERE emp_id = '78629160';
end;
It is running in my machine whithout any error and problem.. adn giving expected result also..
try once again without using that ','.

Related

SELECT INTO STATEMENT INSIDE A SQL PROCEDURE THROWING AN ERROR

Hi I want to store a avalue of select statement into a variable and then update my table using that variable within the procedure but there is an error . I still dont know its only returning one column then also below error exists.
exact fetch returns more than requested number of rows.
Here is the example of demo code.Can anyone please give me an alternative of what else I can do here to make it work since I have many such plsql statement to populate table columns
create or replace procedure pcountry (country IN Varchar) is
var_date Date;
begin
select date into var_date from countrytable where country=country;
update newtable
set date=var_date
where country=country
commit;
end pcountry;
You need to change the name of your procedure'input argument since you have a column with the same name in your table. Oracle is interpretting your where clause
where country = country as where 1 = 1 which is always true. So it returns more rows instead of one row.
create or replace procedure pcountry (country_in IN Varchar) is
var_date Date;
begin
select date into var_date from countrytable where country = country_in ;
update newtable
set date=var_date
where country= country_in
commit;
end pcountry;
Skip select, switch to merge.
CREATE OR REPLACE PROCEDURE pcountry (par_country IN varchr2)
IS
BEGIN
MERGE INTO newtable n
USING countrytable c
ON (c.country = n.country)
WHEN MATCHED
THEN
UPDATE SET n.date_column = c.date_column
WHERE n.country = par_country;
END;
/
Don't name your PL/SQL variables/arguments with the same name as columns. The SQL engine will look for the country value in the local SQL scope in preference to the outer PL/SQL scope so country=country is (assuming non-NULL values) the same as 1=1 and you will match all rows.
Assuming each country is unique then:
create or replace procedure pcountry (
v_country IN COUNTRY_TABLE.COUNTRY%TYPE
) is
var_date COUNTRY_TABLE."DATE"%TYPE;
begin
select "DATE"
into var_date
from countrytable
where country=v_country;
update newtable
set "DATE"=var_date
where country=v_country
end pcountry;
/
Also, DATE is a reserved word and you cannot use is as an unquoted identifier.
You can combine it into a single SQL statement using MERGE:
CREATE PROCEDURE pcountry (
v_country IN COUNTRY_TABLE.COUNTRY%TYPE
) is
var_date COUNTRY_TABLE."DATE"%TYPE;
BEGIN
MERGE INTO newtable n
USING (SELECT *
FROM countrytable
WHERE country = v_country) c
ON (c.country = n.country)
WHEN MATCHED THEN
UPDATE
SET "DATE" = c."DATE";
END pcountry;
/
The issue, as has been explained in other answers, is that it makes no sense to expect country in a condition like country = country to mean different things on the two sides of the equal sign. The name country has more than one meaning - then a set of rules is applied to figure out which meaning is to be accepted each time the name is used. That is usually the narrowest context ("scope") in which the name exists; in this case, the name exists in the table referenced in the SQL statement, so that's what country means there.
One solution is simple - use a different name for the parameter used in the procedure. This has also been shown in the other answers.
There is another solution though. It might be preferred if your procedure was already very long, it used a parameter name like country, and now you would need to add some code where you need to use this name in a SQL statement. It would be pretty time-consuming to change the parameter name everywhere. Happily, PL/SQL understands qualified names. country (where you used it in the where clause) is the column name for the table referenced in the query. But if you write pcountry.country on the right-hand side, qualifying the variable name with the name of the procedure, no confusion would arise anymore.
... where country = pcountry.country
will achieve the same result as the other proposed answers in this thread. The right-hand side is the parameter or variable coming from the procedure, not the column name from the table.
Note that you could also qualify the left-hand side:
... where countrytable.country = pcountry.country
and perhaps this would be clearer to future readers.
However, this would not help:
... where countrytable.country = country
can you see why?

What is wrong with my UPDATE statement?

I just want to update the status of a booking using bookingid.
UPDATE flightbooking
SET status 'C' AS cancelledbooking
FROM flightbooking
WHERE bookingid = 10001;
I get the following error:
ERROR: syntax error at or near "'C'"
LINE 2: SET status 'C' AS cancelledbooking
Any help?
I would suggest:
UPDATE flightbooking
SET status = 'C'
WHERE bookingid = 10001;
This should work in any database, assuming you have the right table and columns.
FROM is not part of the FROM clause necessarily. In addition, how it gets interpreted varies among databases. Once you fix the SET clause, Postgres would update all rows (I think); SQL Server would update the matching row; Oracle and MySQL would generate an error.
You cannot use an alias within an update statement.
UPDATE flightbooking
SET status = 'C'
FROM flightbooking
WHERE bookingid = 10001;
AS is used for aliasing in SELECT context, not UPDATE.
This will work:
UPDATE flightbooking
SET status = 'C'
FROM flightbooking
WHERE bookingid = 10001;
Simply:
UPDATE flightbooking SET status = 'C' WHERE bookingid = 10001

Update all rows with one SQL query

I'm using this PostgreSQL table to store configuration variables:
CREATE TABLE SYS_PARAM(
SETTING_KEY TEXT NOT NULL,
VALUE_TYPE TEXT,
VALUE TEXT
)
;
How I can update all configuration settings values using one SQL statement?
you can use where true at the end and it update all rows in your table.
for example:
UPDATE table_name set table_column = value where true;
it will be update all rows in one SQL query.
If you plan on performing these updates more than once or twice over time, it would be good to have a function handle this for you. You could use the table itself as a type for a variadic parameter within a function, like so:
-- The function
CREATE OR REPLACE FUNCTION update_sys_param(VARIADIC params sys_param[])
RETURNS VOID
AS $$
BEGIN
UPDATE sys_param
SET value_type = upd.value_type, value = upd.value
FROM
sys_param src
INNER JOIN
UNNEST(params) upd
ON (src.setting_key = upd.setting_key);
END; $$ LANGUAGE PLPGSQL;
-- To call it
SELECT update_sys_param(('SMTP_PORT','int','123'),('SMTP_OTHER','text','435343'));
However, if this is a one-time update you can try either of these two:
UPDATE using JOIN
UPDATE sys_param
SET
value_type = new.value_type,
value = new.value
FROM
sys_param src
INNER JOIN
new_params new --< this table/view/cte must already exist or you must create it.
ON (src.setting_key = new.setting_key);
UPDATE using CASE
UPDATE sys_param
SET value = CASE setting_key
WHEN 'SMTP_PORT' THEN '2100'
(..and so on..)
END;
-- You would need to repeat the case statement if you intend on updating the value_type, too.
I guess you can achieve this by doing correlated update.
Please refer to the posts below:
https://www.ibm.com/support/knowledgecenter/ssw_i5_54/sqlp/rbafyexsub4.htm
You can join against a list of values and update with that:
update sys_param
set value = v.new_value
from (
values
('SMTP_PORT', '123'),
('SMTP_SERVER', 'new_server'),
('SMTP_USERNAME', 'Arthur')
) as v(skey, new_value)
where v.skey = sys_param.setting_key;
This assumes that setting_key is the primary key of that table.

Oracle SQL - ORA-04079: invalid trigger specification

I have an EMPLOYEE table, which I want to create a trigger to log for when the employee commissions change (EMPCOMM). I have created an EMPLOYEE_COMM_AUDIT table to handle this. I have come up with the following code:
CREATE OR REPLACE TRIGGER EMPLOYEE_COMM_AUDIT_TRIGGER
BEFORE DELETE OR INSERT OR UPDATE OF EMP_COMM ON EMPLOYEE
IF (NEW.EMP_COMM != OLD.EMPCOMM)
BEGIN
UPDATE EMPLOYEE_COMM_AUDIT
SET EMPLOYEE_COMM_AUDIT.EMP_NUM = EMPLOYEE.EMP_NUM;
SET EMPLOYEE_COMM_AUDIT.CHANGE_DATE = (SYSDATE, 'MM-DD-YYYY HH24:MI:SS') FROM DUAL;
SET EMPLOYEE_COMM_AUDIT.USERNAME = CURRENT_USER;
SET EMPLOYEE_COMM_AUDIT.ACTION = NULL;
SET EMPLOYEE_COMM_AUDIT.OLD_COMM = OLD.EMPCOMM;
SET EMPLOYEE_COMM_ADUDIT.NEW_COMM = NEW.COMM;
DBMS_OUTPUT_LINE("Employee Commisions Audit has been updated);
END;
However Oracle SQL tells me: ORA-04079: invalid trigger specification, but I'm not getting any red underlines anywhere to indicate where the fault is.
Can somebody please help me out? I have tried to have a look on these forums, but I can't seem to find a solid reply anywhere.
Thanks in advance.
Your UPDATE syntax is all wrong:
UPDATE EMPLOYEE_COMM_AUDIT
SET CHANGE_DATE = SYSDATE,
USERNAME = CURRENT_USER,
ACTION = NULL,
OLD_COMM = OLD.EMPCOMM,
NEW_COMM = NEW.COMM
WHERE EMP_NUM = :NEW.EMPNUM;
Changes and assumptions:
SET only appears once.
The table name does not need to be repeated.
The SET expressions are separated by commmas not semi-colons.
There is no need to convert sysdate to a date to assign it to a date column.
You need a WHERE clause to specify what row should be updated.
This fixes the update (or attempts to); there may be other issues.

In SQL, How to add values after add a new column in the existing table?

I created a table and inserted 3 rows. Then I added a new column using alter. How can I add values to the column without using any null values?
Two solutions.
Provide a default value for the column. This value will be used initially for all existing rows. The exact syntax depends on your database, but will will usually look like ..
this:
ALTER TABLE YourTable
ADD YourNewColumn INT NOT NULL
DEFAULT 10
WITH VALUES;
Add the column with null values first. Then update all rows to enter the values you want.
Like so:
ALTER TABLE YourTable
ADD YourNewColumn INT NULL;
UPDATE YourTable SET YourNewColumn = 10; -- Or some more complex expression
Then, if you need to, alter the column to make it not null:
ALTER TABLE YourTable ALTER COLUMN YourNewColumn NOT NULL;
Why don't you use UPDATE statement:
UPDATE tablename SET column=value <WHERE ...>
WHERE is optional. For instance in T-SQL for table:
I can update column NewTestColumn by this statement:
UPDATE [dbo].[Table] SET [NewTestColumn] = 'Some value'
Suppose you have a Employee table with these columns Employee_ID, Emp_Name,Emp_Email initially. Later you decide to add Emp_Department column to this table. To enter values to this column, you can use the following query :
Update *Table_Name* set *NewlyAddedColumnName*=Value where *Columname(primary key column)*=value
Example update TblEmployee set Emp_Department='Marketing' where Emp_ID='101'
I think below SQL useful to you
update table_name set newly_added_column_name = value;
update table_name
set new_column=value
Update table_name set column_name = value where 'condition';
suppose emp is the table and Comm is the new column then fire the below query .
update emp set Comm=5000
For Microsoft SQL (T-SQL):
UPDATE TABLE_NAME SET COLUMN_NAME=10;
here 10 means it will set all values by default to 10