SQL: Updating a field with a sub-query - sql-server-2005

I'm trying to change all the values I have in one column (string) in my table to all lowercase letters. I've tried this statement:
update LoanerHeader t1 set Requester = (select LOWER(Requester) from LoanerHeader t2 where t2.ISO_ID = t1.ISO_ID)
But am getting the following error: Incorrect syntax near 't1'.
I don't really understand why this isn't working, because the answer to this following question has similar syntax. Can anyone help me out? Thx.

You don't need to use a subquery. You can just do:
UPDATE LoanerHeader SET Requester = LOWER(Requester);

Related

Beginning SQL - adding the values of one column to another from separate tables

I am currently taking my first course in SQL and have encountered a bit of a problem. I will now try to explain what I am trying to do. I have this select statement which properly displays what I need. MY problem arises when I try to convert it into an UPDATE statement instead.
SELECT infobb02.uni+tempbb02.sal
from infobb02 JOIN tempbb02 ON infobb02.empno=tempbb02.empno;
In case its not obvious im adding value of uni from table infobb02 to sal in table tempbb02. I have tried all sorts of things to get it to be a permanent update but keep getting errors mostly
"SQL command not properly ended"
Any help is appreciated! Thanks.
Assuming that your query is:
SELECT i.uni + t.sal
FROM infobb02 i JOIN
tempbb02 t
ON i.empno = t.empno;
If you want to update tempbb02, then:
update tempbb02 t
set t.sal = t.sal +
(select i.uni from infobb02 i where i.empno = t.empno)
where exists (select 1 from infobb02 i where i.empno = t.empno);
Instead of using an UPDATE statement, you could use a MERGE:
merge into tempbb02 tgt
using infobb02 src
on (tgt.empno = src.empno)
when matched then
update set tgt.sal = tgt.sal + src.uni;

Merge Query in DB2

I need to update few columns in one table with the very convoluted calculation.
I'm not good enough in SQL so I tried to use "with" clause in combination with update, but It threw error.
Then I found a post online which suggested to use MERGE so I came up with Merge query. But that one was also throwing an error.
So I removed all other column and updating only one column to remove complexity, but no avail still errors
Below is my query, select query inside working perfectly fine.
Please suggest.
MERGE INTO TABLE_1 AS O
USING (
SELECT ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE)) - ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE))*TO_NUMBER(TABLE_1.AUC_MILEAGE))/100000 ) as CORRECT_FLOOR_PRICE
FROM TABLE_1, TABLE_2,TABLE_3
WHERE TABLE_2.Primary_ID= TABLE_1.Primary_ID
AND TABLE_2.option_code = 'FSDS'
AND TABLE_1.FLOOR_PRICE <> '0.00'
and TABLE_3.Primary_ID=TABLE_1.Primary_ID
and TABLE_3.Primary_ID=TABLE_2.Primary_ID
) AS CORRECT
ON(
O.Primary_ID = CORRECT.Primary_ID
)
WHEN MATCHED THEN
UPDATE
set O.FLOOR_PRICE =CORRECT.CORRECT_FLOOR_PRICE
Error is
An error occurred when executing the SQL command:
MERGE INTO ........
DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=SELECT;VALUES, DRIVER=3.61.75 [SQL State=42601, DB Errorcode=-199]
Try this instead, I think you forgot your identifier in your select statement because after the "ON" statement "CORRECT.Primary_ID" doesn't associate to anything.
MERGE INTO TABLE_1 as O
USING (
SELECT ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER
(TABLE_2.OPT_BASE_WHSLE)) - ((TO_NUMBER(TABLE_3.Total_Whsle_Price)
-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE))*TO_NUMBER
(TABLE_1.AUC_MILEAGE))/100000 ) as CORRECT_FLOOR_PRICE,
TABLE_1.Primary_ID AS Primary_ID
FROM
TABLE_1, TABLE_2,TABLE_3
WHERE TABLE_2.Primary_ID = TABLE_1.Primary_ID
AND TABLE_2.option_code = 'FSDS'
AND TABLE_1.FLOOR_PRICE <> '0.00'
AND TABLE_3.Primary_ID=TABLE_1.Primary_ID
AND TABLE_3.Primary_ID=TABLE_2.Primary_ID
) AS CORRECT
ON(
O.Primary_ID = CORRECT.Primary_ID
)
WHEN MATCHED THEN
UPDATE
set O.FLOOR_PRICE = CORRECT.CORRECT_FLOOR_PRICE

Alternative update SQL query

I am looking for an alternative for this query. I know that such query will end up with invalid identifier in Oracle. So please give me the same query for updating one filed from another field in another table.
update RBT_CMP_RECOM_9304
set FIRST_RECOM_NAME=(select rbt_cmp_base_code.RBT_NAME
from rbt_cmp_base_code
where rbt_cmp_base_code.RBT_CODE=RBT_CMP_RECOM_9304.FIRST_RECOM)
where rbt_cmp_base_code.RBT_CODE=RBT_CMP_RECOM_9304.FIRST_RECOM;
FYI:
RBT_CMP_RECOM_9304=(firt_recom,first_recom_name)
RBT_CMP_BASE_CODE = (rbt_code, rbt_name)
I get this error when I try it:
ORA-00904: RBT_CMP_BASE_CODE.RBT_CODE: invalid identifier
Regards.
One way is to repeat the subquery:
update RBT_CMP_RECOM_9304 r
set FIRST_RECOM_NAME = (select bc.RBT_NAME
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
)
where exists (select 1
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
);
EDIT:
If you are getting an error that more than one row is returned, then you have to decide which value. Nothing in your code suggests that this might be an issue (hint: sample data and desired results always help a question).
The easiest solution is to use and aggregation function:
update RBT_CMP_RECOM_9304 r
set FIRST_RECOM_NAME = (select max(bc.RBT_NAME)
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
)
where exists (select 1
from rbt_cmp_base_code bc
where bc.RBT_CODE = r.FIRST_RECOM
);
But you might want to fix the rbt_cmp_base_code table so it doesn't have duplicates. From the table name, it sounds like there should be one row per code.

Update table from another table for certain fields

I need you help in updating changes a table.
the table i want to update is IP_VISIT_EASTERN_REGION, the field I want to update is VISIT_STAT
so I wrote this query
UPDATE IP_VISIT_EASTERN_REGION SET
IP_VISIT_EASTERN_REGION.VISIT_STAT=TEST_IP_VISIT_EASTERN_REGION.VISIT_STAT
WHERE IP_VISIT_EASTERN_REGION.VISIT_ID = TEST_IP_VISIT_EASTERN_REGION.VISIT_ID
The Issue is I get an error
ORA-00904 INVALID IDENTIFIER
and is this the correct way to do it.
your help will be really appreciated.
thank you
You can't write an UPDATE from a SELECT by using a join. Try this approach instead.
update set from inner join query thowing error
In your case:
UPDATE IP_VISIT_EASTERN_REGION REGION
SET REGION.VISIT_STAT= ( SELECT TEST.VISIT_STAT
FROM TEST_IP_VISIT_EASTERN_REGION TEST
WHERE REGION.VISIT_ID = TEST.VISIT_ID )
WHERE EXISTS ( SELECT TEST.VISIT_STAT
FROM TEST_IP_VISIT_EASTERN_REGION TEST
WHERE REGION.VISIT_ID = TEST.VISIT_ID );
Fiddle:
http://sqlfiddle.com/#!4/2178ae/1

SQL CASE WHEN in WHERE clause

searched for this problem, but couldn't find a question for exactly this problem.
I have a stored procedure that fetches data from a large amount of tables.
In my SP I have a parameter indicating the ID. Default Value of that ID is zero.
I want to return all values regarding this parameter in my query, unless a value is provided.
SELECT mT.*
FROM myTable mT
(various JOINS)
WHERE
CASE #myParameter
WHEN 0 THEN 1 = 1
ELSE mT.ID = #myParameter
END
Unfortunately I get the error message "Incorrect syntax near '='".
Things that are fixed: It has to be a SP, I get just one parameter where I can set a default value in case it is not filled. If it is not filled this part of the where clause shall not apply (there are more statements in the WHERE clause.
If I did not see a question that contained this problem please give me a hint where to find it.
Best Regards and thanks in advance!
You don't need CASE:
SELECT mT.*
FROM myTable mT
(various JOINS)
WHERE
#myParameter= 0 OR mT.ID = #myParameter