I have a table that I want to update that contains a column called 'expiration_days'. What I am doing is trying to update the records in the 'expiration_days' column by using an 'alias column' (not sure what to call it) that is apart of a subquery where I calculated the number of days until a user's password has expired. The column from the subquery that I want to take the values from and update them in the actual table is called 'countdown'. I named the subquery results 'query' (derived table). So far I have this:
UPDATE LOGIN_INFO
SET expiration_days = query.countdown
FROM (
select li.name as name, countdown = 365 - datediff(day, sl.pwdate, getdate())
from master..syslogins sl, LOGIN_INFO li
where li.name = sl.name) query
WHERE LOGIN_INFO.name = query.name
The issue I am having is I get this error: You cannot use a derived table in the FROM clause of an UPDATE or DELETE statement. ( I also get: Incorrect syntax near ')' on the subquery where clause)
Is there a way I can take the results from the calculated column in a select statement and update the column in the LOGIN_INFO table in one query or some other easy clean way?
Perhaps something along the lines of:
update login_info
set expiration_days = (select 365 - datediff(day,s1.pwdate,getdate())
from master..syslogins s1
where s1.name = li.name)
from login_info li
where exists(select 1
from master..syslogins s2
where s2.name = li.name)
NOTES:
the exists() clause is added to insure we don't erroneously update a row in login_info that doesn't have a match in syslogins, otherwise OP will need to modify the logic accordingly (ie, what to set expiration_days to if a matching rows does not exist in syslogins?)
if syslogins.pwdate is NULLable (I don't have access to a running ASE instance at the moment) then OP will need additional logic to handle the scenario where s1.pwdate is NULL; default countdown to some hardcoded value? or perhaps modify the exists() to include the additional clause and s2.pwdate is not NULL?
Related
In this query I inserting records into a new empty table I created. These records are derived from another table where I am left joining that table to itself, in order to output records that are not included in the recent table that is appended on top of an older table. So basically it outputs records that were deleted.
CREATE DEFINER=`definer` PROCEDURE `stored_procedure_name`()
MODIFIES SQL DATA
SQL SECURITY INVOKER
BEGIN
START TRANSACTION;
INSERT INTO exceptions_table (
`insert_date`,
`updated`,
`account_number`,
`id_number`)
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
FROM original_table ot1
LEFT JOIN original_table ot2
ON ot1.`account_number` = vdcaas2.`account_number`
AND ot2.`insert_date` = '2022-12-20'
WHERE ot1.`insert_date` = '2022-12-10'
AND ot2.`account_number` IS NULL;
COMMIT;
END
I get an error stating: "SQL Error: Column "insert_date" in field list is ambiguous.
I'm not sure why because I have specified which table I am grabbing "insert_date" from when INSERTING and when SELECTING and JOINING..
Every row in your query has two columns called insert_date: one from the table you've aliased as "ot1", and one from the table (as it happens, the same table) you've aliased as "ot2".
The database system doesn't know which one you want, so you have to tell it by writing either "ot1.insert_date" or "ot2.insert_date", just as you do elsewhere in the query:
... ot2.`insert_date` = '2022-12-20'
...
... ot1.`insert_date` = '2022-12-10'
The same is true of the other columns you've listed to select.
You need to change this
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
to this
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
or this
SELECT
ot2.`insert_date`,
ot2.`updated`,
ot2.`account_number`,
ot2.`id_number`
or some combination
Issue
SQL Error: Column "insert_date" in field list is ambiguous error means that the query is trying to reference the "insert_date" column from both tables, ot1 and ot2.
Try the following:
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
Also, you have a typo in your query:
ON ot1.`account_number` = vdcaas2.`account_number` -> ON ot1.`account_number` = ot2.`account_number`
checked several threads and some did help but now I'm getting an error:
ERROR: more than one row returned by a subquery used as an expression
SQL state: 21000
Not sure what's the exact issue even though read some explanations. Would appreciate if someone could explain with using my code bellow.
I have a table / view called vw_inv_stock_art_global, and it has a column "stock" with a number.
Then also I have a table dis_orderoutdetails with a column "onstock" which needs to copy the "stock" cells based on article_id both tables have in common.
UPDATE dis_orderoutdetails
SET onstock = (SELECT stock
FROM vw_inv_stock_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id)
WHERE onstock is NULL
AND EXISTS(SELECT stock
FROM vw_inv_stoc_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id);
Additional help if you can be bothered:
I was wondering if there's a possibility to change background colour of a cell just in SQL? Wanted to make an if...else and change colours of a cell depending on the result.
The problem is with this part of the statement:
SET onstock = (SELECT stock
FROM vw_inv_stock_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id)
The SELECT query returns more than one row => there are more than one row in the vw_inv_stock_art_global view that has this article_id. If the result of a SELECT is to be used as a value there may only be one matching row.
Not sure about Postgres SQL syntax but it seems you would use LIMIT 1 to solve this.
SET onstock = (SELECT stock
FROM vw_inv_stock_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id LIMIT 1)
However when you use LIMIT you probably need to use an ORDER BY clause as well to make sure that you use the most relevant row, not just any single row that matches the criterion. The most relevant row is most of the times the latest, so if there is some date column that specifies the entry of the row it is a good bet to be the column you need to order on. This is something you need to review according to your needs.
The ORDER BY would be inserted like this:
(Place holder needs to be replaced)
SET onstock = (SELECT stock
FROM vw_inv_stock_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id
ORDER BY <some column>
LIMIT 1)
You can refactor your SQL without using subqueries, like:
UPDATE dis_orderoutdetails
SET onstock = g.stock
FROM vw_inv_stock_art_global
WHERE vw_inv_stock_art_global.article_id = dis_orderoutdetails.article_id
and onstock is NULL;
But be aware that the correctness of this SQL depends on the relationship between dis_orderoutdetails(article_id) and vw_inv_stock_art_global(article_id): article_id isn't a unique column in vw_inv_stock_art_global, this UPDATE won't be predictable, as each dis_orderoutdetails could be updated more than once, with different stock values.
I have a following SELECT query -
SELECT C.CASE_TITL_NM,RA.V_CUST_NUMBER
FROM KDD_CASES C
JOIN FCT_RA RA
ON RA.N_RA_ID = C.RA_ID
WHERE UPPER(C.CNTRY_KEY_ID) LIKE '%MANUAL%'
AND C.SCORE_CT IN (99,100)
AND C.STATUS_CD = 'CCD'
AND C.CASE_TITL_NM NOT LIKE 'MANUAL%'
I need to update value in col V_CUST_NUMBER with value in col CASE_TITL_NM so I plugged my SELECT inside following UPDATEstatement and ran it only to get ORA01779 -
UPDATE (
SELECT C.CASE_TITL_NM,RA.V_CUST_NUMBER
FROM KDD_CASES C
JOIN FCT_RA RA
ON RA.N_RA_ID = C.RA_ID
WHERE UPPER(C.CNTRY_KEY_ID) LIKE '%MANUAL%'
AND C.SCORE_CT IN (99,100)
AND C.STATUS_CD = 'CCD'
AND C.CASE_TITL_NM NOT LIKE 'MANUAL%'
) X
SET X.V_CUST_NUMBER = X.CASE_TITL_NM;
SQL Error: ORA-01779: cannot modify a column which maps to a non key-preserved table
01779. 00000 - "cannot modify a column which maps to a non key-preserved table"
*Cause: An attempt was made to insert or update columns of a join view which
map to a non-key-preserved table.
*Action: Modify the underlying base tables directly.
Can anybody explain what does this error mean and what would be the right UPDATE query?
What it means is the query as specified results in an output set that has duplicated rows for RA. Seeing as one RA row maps to two different C rows you cannnot update RA, because there is the potential to try and update the sole RA row to have two different values
You can try using a MERGE statement, using SQL-that-writes-SQL to create a bunch of UPDATE statements, or modifying the join condition so duplicate rows from RA are not present in the output and the primary key from RA is covered
I was able to run my query it by using EXIST clause
UPDATE FCT_RA F
SET F.V_CUST_NUMBER = ( SELECT CASE_TITL_NM
FROM KDD_CASES C
WHERE F.N_RA_ID = C.RA_ID
AND UPPER(CNTRY_KEY_ID) LIKE '%MANUAL%'
AND SCORE_CT IN (99,100)
AND STATUS_CD = 'CCD'
AND CASE_TITL_NM NOT LIKE 'MANUAL%')
WHERE EXISTS ( SELECT 1
FROM KDD_CASES C
WHERE F.N_RA_ID = C.RA_ID
AND UPPER(CNTRY_KEY_ID) LIKE '%MANUAL%'
AND SCORE_CT IN (99,100)
AND STATUS_CD = 'CCD'
AND CASE_TITL_NM NOT LIKE 'MANUAL%');
Please look here, especially about key preserved
The updatable view query must unambiguously return each row of the
modified table only one time. The query must be “key preserved”, which
means Oracle must be able to use a primary key or unique constraint to
ensure that each row is only modified once.
This code is only updating one row, why? It has to do with one of the sub-queries but I am not sure. I'm thinking the WHERE..IN in the UPDATE statement but I am not sure.
UPDATE [sde].[sy1].[Valve_evw]
SET [sde].[sy1].[Valve_evw].[MA]
= (SELECT [sde].[sy1].[Valve_Join_evw].[MC]
FROM [sde].[sy1].[Valve_Join_evw])
WHERE [sde].[sy1].[Valve_evw].[PrimaryKey]
IN (SELECT [sde].[sy1].[Valve_Join_evw].[PrimaryKey]
FROM [sde].[sy1].[Valve_Join_evw]
WHERE [sde].[sy1].[Valve_Join_evw].[MA]
!= [sde].[sy1].[Valve_Join_evw].[MC])
Context:
What I am trying to do is update the MA column in Valve_evw using the MC column in Valve_Join_evw. The PrimaryKey in Valve_evw references equivalent rows as the PrimaryKey in Valve_Join_evw. As in, a single row in Valve_Join_evw will have the same PrimaryKey as a single row in Valve_evw, thus that equivalency can be used to update the records in Valve_evw. Also the MA column is equivalent in both tables. [Note: The Valve_Join_evw table is created with ESRI mapping software using the spatial relationship between the Valve_evw and a separate table, this is how the duplicate rows exist]
I am using database views (hence the '_evw') in SQL Server with a default INSTEAD OF UPDATE trigger. This combination, views and trigger, prevents the use of table joins to do this update. I have also tried MERGE but that will not work either. Therefore I am stuck with the ANSI standard, hence the sub-queries. This script runs with no errors but it only updates a single row whereas there are about 9000 thousand rows in the tables.
The output message:
(1 row(s) affected)
(0 row(s) affected)
First of all let's reduce the eye hurting SQL to what it really is:
update sde.sy1.valve_evw
set ma = (select mc from sde.sy1.valve_join_evw)
where primarykey in (select primarykey from sde.sy1.valve_join_evw where ma <> mc)
WHERE clause
We look for all primarykey in valve_join_evw where a record's ma <> mc. We update all valve_evw records with such primarykey.
SET clause
For a record we want to update, we set ma to the value found with:
select mc from sde.sy1.valve_join_evw
But this query has no where clause, so what value does it select to fill the record's ma field? It selects all mc from valve_join_evw, so the DBMS probably picks one of these values arbitrarily. (It would be better, it raised an error.)
Conclusion
It is very easy to see which records the statement will update.
Which primarykey:
select primarykey from sde.sy1.valve_join_evw where ma <> mc
Which rows:
select *
from sde.sy1.valve_evw
where primarykey in (select primarykey from sde.sy1.valve_join_evw where ma <> mc)
As to the SET clause: Add a WHERE clause to your subquery that relates the record to select to the record to update (same ma? same primarykey?) E.g.:
set ma =
(
select mc
from sde.sy1.valve_join_evw vj
where vj.primarykey = valve_evw.primarykey
and vj.ma = valve_evw.ma
)
Hi there i recomend first to do the select statement and when you are ok with te records retrieved use the same where for the update statement
Here is what the final script looks like.
UPDATE [Valve_evw]
SET [Valve_evw].[MA] =
(
SELECT [Valve_Join_evw].[MC]
FROM [Valve_Join_evw]
WHERE[Valve_Join_evw].[PrimaryKey] = [Valve_evw].[PrimaryKey]
)
WHERE [Valve_evw].[PrimaryKey]
IN (
SELECT [Valve_Join_evw].[PrimaryKey]
FROM [Valve_Join_evw]
WHERE [Valve_Join_evw].[MA]
!= [Valve_Join_evw].[MC]
);
I need to update a column in one table with the results from a select sub-query (and they should ultimately be different). But When I do this, I either get the exact same number for the hundreds of records, or I get the ORA-01427: single row sub-query returns more than one row query. error.
Can you please take a look and see what it is that I am overlooking? (I could just be overlooking something simple for all I know)
UPDATE WD_PRODUCT_CLASS
SET CURRENT_CASES = ( WITH STUFF_COUNT AS
(
SELECT sum(CURRENT_DETAIL.COMBINED_QTY) AS TOTAL_CASES
FROM CURRENT_DETAIL, SKU_MAJORS, WD_PRODUCT_CLASS
WHERE CURRENT_DETAIL.LOC_ID =
&PARM_LOC_ID
AND CURRENT_DETAIL.INVEN_ID = SKU_MAJORS.INVEN_ID
AND WD_PRODUCT_CLASS.CATEGORY = SKU_MAJORS.CONT_DESC
GROUP BY WD_PRODUCT_CLASS.CATEGORY
)
(
SELECT SUM(Z.TOTAL_CASES) FROM STUFF_COUNT Z
)
);
Maybe you need someting like this:
UPDATE WD_PRODUCT_CLASS wpc
SET wpc.CURRENT_CASES = (
SELECT sum(cd.COMBINED_QTY)
FROM CURRENT_DETAIL cd join SKU_MAJORS sm ON cd.INVEN_ID = sm.INVEN_ID
WHERE cd.LOC_ID = &PARM_LOC_ID
AND sm.CONT_DESC = wpc.CATEGORY
)
WHERE 1=1; -- if you don't set a condition all the rows will be updated
Your query updates the table with the same values because you're using a not correlated subquery in the SET clause. This subquery don't depends on the parent query, so it's calculated only once.
I suppose you need a correlated subquery so I changed your update + removed some extra parts.