Sybase update only the last record that fulfill conditions - sql

I´m trying to update a record that fulfills some conditions but I need it to be the last one, I've read that I should use an order by and a limit. (I don't want to use a subquery to first fetch the last reccord that fulfill conditions ... I feel aint cool, should be a better way ...)
I've tried:
UPDATE table_1
SET some_field= 'value'
FROM table_1 t1
INNER JOIN table_2 t2 ON t1.field_1 = t2.field_1
WHERE t1.some_field = 'some_value' and t1.seome_other_field = 'some_other_value'
ORDER BY t1.some_field DESC limit 1
But I get Error (156) incorrect syntax near keyword 'ORDER'
For what I can see in Sybase docs I'm using the keywords in the correct order ...
http://infocenter.sybase.com/archive/index.jsp?topic=/com.sybase.infocenter.dc38151.1260/html/iqref/Update.htm
Am I breaking some rule ?
I´m sure is something pretty obvious ... but right now can't see it ...
Sybase 15.7.0.501.1011

With out using a subquery, or changing your query you could use set rowcount to limit the number of updates:
set rowcount 1
go
UPDATE table_1
SET some_field= 'value'
FROM table_1 t1, table_2 t2
WHERE t1.field_1 = t2.field_1
AND t1.some_field = 'some_value' and t1.seome_other_field = 'some_other_value'
ORDER BY t1.some_field DESC
go
set rowcount 0
go
This is a session level setting, and 0 is the default value to return all rows affected.

Related

Updating all the record for a column in a table with value from another table

Hi I want to update all the values of RequestId column of IIL_CHANGE_REQUEST Table with the RequestId of TABLE_NAME_4MINS Table where REQUESTBY column in both tables are same. I am trying to do this in oracle daatabalse(sql developer)
My query:
Update IIL_CHANGE_REQUEST
set REQUESTID=(SELECT TABLE_NAME_4MINS.REQUESTID
FROM TABLE_NAME_4MINS
WHERE IIL_CHANGE_REQUEST.REQUESTBY = TABLE_NAME_4MINS.REQUESTBY)
WHERE EXISTS (SELECT TABLE_NAME_4MINS.REQUESTID
FROM TABLE_NAME_4MINS
WHERE IIL_CHANGE_REQUEST.REQUESTBY = TABLE_NAME_4MINS.REQUESTBY)
But every time I do this I get an error saying:
Error starting at line : 1 in command -
Update IIL_CHANGE_REQUEST
set REQUESTID=(SELECT TABLE_NAME_4MINS.REQUESTID
FROM TABLE_NAME_4MINS
WHERE IIL_CHANGE_REQUEST.REQUESTBY = TABLE_NAME_4MINS.REQUESTBY)
WHERE EXISTS (SELECT TABLE_NAME_4MINS.REQUESTID
FROM TABLE_NAME_4MINS
WHERE IIL_CHANGE_REQUEST.REQUESTBY = TABLE_NAME_4MINS.REQUESTBY)
Error report -
ORA-01427: single-row subquery returns more than one row
Please anyone help how can I do it.
It depends on what you want to do in such cases. If you don't really care, take any value, for example minimum:
update iil_change_request a
set a.requestid = (select min(b.requestid) --> here
from table_name_4mins b
where a.requestby = b.requestby)
where exists (select c.requestid
from table_name_4mins c
where a.requestby = c.requestby);
If that's not what you want, then you'll have to figure out what to do with those "duplicates". Perhaps you'll have to include yet another WHERE condition, or fix data, or ... who knows? I don't, while you should.
You need to find a condition to narrow down returned rows to the only per REQESTSTBY, for example you can replace ... with a column name in the query below to return just first row as per order-by:
MERGE INTO IIL_CHANGE_REQUEST r
USING (
SELECT *
FROM (
SELECT REQUESTBY, REQUESTID
,row_number()over(partition by REQUESTBY order by ...) rn
FROM TABLE_NAME_4MINS
)
where rn=1
) t
ON (r.REQUESTBY = t.REQUESTBY)
WHEN MATCHED THEN UPDATE
set REQUESTID=t.REQUESTID;

"Scalar subquery produced more than one element"

I need to update a column's value by simply inserting a column of values according to another table in this way:
UPDATE `inbound-summit-278521.MET689.fact_posts`
SET `inbound-summit-278521.MET689.fact_posts`.accepted =
(SELECT (CASE WHEN id IN (SELECT T2.id
FROM `inbound-summit-278521.MET689.fact_posts` T1
JOIN `inbound-summit-278521.MET689.fact_posts` T2
ON T1.accepted_answer_id = T2.post_id)
THEN 1 ELSE 0 END)
FROM `inbound-summit-278521.MET689.fact_posts`)
WHERE `inbound-summit-278521.MET689.fact_posts`.accepted IS NULL;
The output error is shown as the title goes but no syntax error was detected. What did it go wrong? How should I fix it?
It is really hard to tell what you are trying to do. It seems to be that you want to set a flag to 1 if a corresponding row exists in the same table based on an "answer".
If so, you can use a correlated subquery. Something like this:
UPDATE `inbound-summit-278521.MET689.fact_posts` fp
SET accepted = 1
WHERE fp.accepted IS NULL AND
EXISTS (SELECT 1
FROM `inbound-summit-278521.MET689.fact_posts` fpa
WHERE fpa.accepted_answer_id = fp.post_id
);

update tbl_1 set tbl_1.valueA = tbl2.valueB when tbl_2.valueC is equal to tbl_1valueD

As the title indicates - I'm trying to update a table.value with another table.value based on a common value in the 2 tables (but not same column names)
First I delete any duplicate records in the source table.
And count the remaining rows - that total is 93.
Select rowid, xfmid_value from w_valve_reftest;
Delete from w_valve_reftest a
where rowid> (select min(rowid)
from w_valve_reftest b where b.xfmid_value=a.XFMID_VALUE);
Select count(*) from w_valve_reftest;
Next I want to update the target.reference_1 with the value from source.ref1_value and target.reference_2 with source.ref2_value where source.xfmid_value=target.xfm_id
Here is what I have, but for some reason it is updating 17,000+ records. Rather than the 93 I expect.
update w_isolationvalve
set w_isolationvalve.reference_1=(select ref1_value from w_valve_reftest where w_isolationvalve.xfm_id=w_valve_reftest.xfmid_value);
update W_isolationvalve
set w_isolationvalve.reference_2=(select ref2_value from w_valve_reftest where w_isolationvalve.xfm_id=w_valve_reftest.xfmid_value);
I'm no expert, but not a rookie any longer. I've hacked it this far using google. Thanks for any assist.
Your second query should be:
update a
set reference_1=b.ref1_value,
refrence_2=b.ref2_value
FROM w_isolationvalve a
JOIN w_valve_reftest b ON a.xfm_id=b.xfmid_value
As for the update counts, it will only be 93 only if xfm_id and xfmid_value are unique.
Also be careful of non-deterministic updates.
If your select will have multiple results for your join condition, the update will be executed once for each of the multiple results and you will not know what you end up with.
This should work in Oracle:
MERGE INTO w_isolationvalve a
USING
(
SELECT * FROM w_valve_reftest
)b
ON(a.xfm_id = b.xfmid_value)
WHEN MATCHED THEN UPDATE SET
a.reference_1 = b.ref1_value,
a.refrence_2 = b.ref2_value ;
Since you do not like merge. I think this should also work in Oracle:
UPDATE
(SELECT a.reference_1, a.refrence_2, b.ref1_value , b.ref2_value
FROM w_isolationvalve a
INNER JOIN w_valve_reftest b
ON a.xfm_id = b.xfmid_value
) t
SET t.reference_1 = t.ref1_value ,
t.refrence_2 =t.ref2_value

updating sql query value with select statement

I am trying to execute a query which is something like:
update table set column=(select column1 from table1);
I just want to store the value from other table to my column
but when i try my sql query it says
ERROR 1242 (21000): Subquery returns more than 1 row
definitely this means my table1 contains more than 1 row so i want to know that is there any way to store data into column from other table with multiple row.
or basically saving content of other table as a text something like
update table set column='Data in text from other table';
You probably need a correlation clause:
update table
set column = (select column1 from table1 where table.col = table1.col);
You need to decide what column(s) are used for the correlation.
it will work as i am getting your requirement.Please let me know if your requirement is other i ll make changes in query
update table_name t1
inner join table1 t2 on t1.id =t2.id
set column =column1
Your nested query returns multiple rows so you encountered this error.
Try in this way
UPDATE FirstTable
SET FirstTable.ColumnName =tbl2.ColumnName
FROM SecondTable tbl2 WHERE tbl2.Id = FirstTable.Id
There should be a common id or something that will help to find exact row.

SQL Sybase Query Strange Behaviour

I've got 2 tables with exactly the same structure in the same Sybase database but they're separate tables.
This query works on one of the 2:
select * from table1 where
QUOTA_FIELD >
(SELECT
count(ACCOUNT) FROM
table1 As t1
where SECTOR = t1.SECTOR
AND
STATUS = 'QUOTA'
)
But for the second table I have to change it to this:
select * from table2 as tref where
QUOTA_FIELD >
(SELECT
count(ACCOUNT) FROM
table2 As t2
where tref.SECTOR = t2.SECTOR
AND
STATUS = 'QUOTA'
)
There's a restriction on where this will execute which means it needs to work like in the first query.
Does anyone have any ideas as to why the first might work as expected and the second wouldn't?
Since I am not yet allowed to comment, here as an answer to the question "does anyone...?":
No. I couldn't find anyone :)
This first query cannot work correctly, since it compares a column with itself (as long as the column names are all normal ASCII characters and not some similar looking UNICODE ones). Please give a proof that the result of this query is in every case the same as of query 2.
Also, the second query would normally be done like that: where SECTOR = tref.SECTOR...
You might be looking for something like this in query #1 :
select * from table1 t2 where
QUOTA_FIELD >
(SELECT
count(ACCOUNT) FROM
table1 As t1
where t2.SECTOR = t1.SECTOR
AND
t1.STATUS = 'QUOTA'
)
This explicitly specifies that the table in subquery is joining with the table in outer query ( co-related subquery ).
If this works, use the same idea in query #2