Regarding joins and subquery - sql

I have below query that I am using ..
select * from app_subsys_param where assp_name like '%param_name%'
where param_name is the name of the parameter. From this query we will get the assp_id corresponding to the parameter. With this id we look up into app_subsys_parmval table to get the value of the parameter.
update app_subsys_parmval set aspv_value = 'true' where assp_id = id_val
Now instead of separately launching the two sql statements , I want to combime both of them as one is there any sub query or join mechanism that can combine both of them in one statement , please advise

You need to use UPDATE .. FROM syntax:
UPDATE app_subsys_paramval
SET aspv_value = 'true'
FROM app_subsys_param
WHERE app_subsys_param.id = app_subsys_paramval.id
AND app_subsys_param.value LIKE '%param_name%';

Use a subselect in your update statement:
UPDATE app_subsys_parmval
SET aspv_value = 'true'
WHERE id_val = (SELECT assp_id
FROM app_subsys_param
WHERE assp_name LIKE '%param_name%')
Note, I am assuming a bit about what's in the * of your select *.

Look at the MERGE statement. This is the ANSI SQL:2003 standard for UPDATE … FROM.
Documentation:
MERGE for DB2 for Linux/UNIX/Windows
MERGE for DB2 z/OS 9.1

Related

Oracle SQL: The equivalent way to execute the following expression from PostgreSQL?

The following thread successfully showed how to use a an UPDATE SET and FROM clause together, to update an entire row of a specific column with a value derived from a different table.
When executing following expression in Oracle SQL:
UPDATE territory2_t
SET total_sales_person = t.total_count
FROM (
SELECT salesterritoryid, count(*) as total_count
FROM salesperson_t
group by salesterritoryid
) t
WHERE territoryid = t.salesterritoryid;
Oracle states: SQL Error: ORA-00933: SQL command not properly ended
In Oracle you can use merge to do the job:
merge into territory2_t
using (
SELECT salesterritoryid, count(*) as total_count
FROM salesperson_t
group by salesterritoryid
) t
on (territoryid = t.salesterritoryid)
when matched then
update SET total_sales_person = t.total_count
This can be done with MERGE (in Oracle and most other DB systems - those that implement the MERGE statement from the SQL Standard). MERGE is the preferred solution in most cases.
There is a misconception that this cannot be done with an UPDATE statement in Oracle. I show below how it can be done - not to encourage its use (MERGE is better), but to show that UPDATE can be used as well. This is the transformation of the Postgre SQL the OP solicited in the original post.
update ( select t2.total_sales_person, t.total_count
from territory2_t t2 inner join
( select salesterritoryid, count(*) as total_count
from salesperson_t
group by salesterritoryid
) t
on t2.territoryid = t.salesterritoryid
)
set total_sales_person = total_count;

sql server if statement not working

Can anyone advise me as to what is wrong with the following SQL server update statement:
IF (SELECT * FROM TBL_SystemParameter WHERE code='SOUND_WRONG_GARMENT') = ''
GO
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID = (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
You don't need an if statement - you can just run the update statement, and if the subquery returns no rows, no rows will be updated. The if won't really save anything - you're performing two queries instead of one.
You either want
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID In (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
if there are multiple ID's with that code OR use
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE code = 'SOUND_WRONG_GARMENT'
either way and lose the IF statement as #Mureinik said.
Although Mureinik's answer is the logical solution to this, I will answer why this isn't actually working. Your condition is wrong, and this approach will work instead using IF EXISTS:
IF EXISTS (SELECT * FROM TBL_SystemParameter WHERE code='SOUND_WRONG_GARMENT')
BEGIN
UPDATE TBL_SystemParameter
SET [Value] = 'Ping.wav'
WHERE ID IN (SELECT ID
FROM TBL_SystemParameter
WHERE code = 'SOUND_WRONG_GARMENT')
END
As a side note, you're using an = sign instead of IN, which means you'll be matching to an arbitrary singular ID and only update 1 row based on this. To use a set based operation, use the IN clause.
You could actually 'golf' this by doing away with the derived query altogether, and using a simple WHERE code='SOUND_WRONG_GARMENT' on the table you're updating on.

How to re-use a result set produced by executing a sql query?

I'm using PostgreSQL 8.4. I need to assign the result set produced by the query to a variable that I can use in another query. For instance, I have the following query:
SELECT *
FROM
-- some_table_expression
I need to use the result of that query in another query like the following:
FOR i IN 1 .. array_upper(players, 1)
LOOP
UPDATE partner.daily_profit
SET banner_id = (SELECT banner_id FROM _RESULT_OF_THE_QUERY_ WHERE id = players[i]),
WHERE partner.daily_profit.player_id = players[i]
END LOOP
Is there a way to do this in PostgreSQL 8.4? I would prefer to not insert the query string as subquery...

Need help converting If/then to CASE in SQL

I'm having some trouble grasping the use of CASE statements (used to using If/Then). I would like to convert this to CASE format:
If DATEPART(Month,Datetime) = 04
Then UPDATE DB1
SET column1 = (SELECT Value FROM DB2)
So if the month of the current datetime matches 4 (April), then update column1 of DB1 with the values in the Value column of DB2. How would this look using CASE?
I am unclear what case has to do with this. The case statement would normally be used in a select to execute conditional statements. Your update seems more like:
update db1
set column1 = db2.value
from db1 join
db2
on db1.foo = db2.bar
where DATEPART(Month, db1.Datetime) = 4;
But it is a bit hard to divine from your question what you are really trying to do.
EDIT: (in response to comment)
For todays date, the where clause should be:
where datepart(month, getdate()) = 4
Instead of the where, you can use if (datepart(month, getdate()) = 4) . . ..
The join (or subquery) is needed because the question refers to two tables.
IF ((SELECT DATEPART(MONTH, GetDate())) = 04)
UPDATE
db1
SET
column1 = db2.value
FROM
db2
WHERE
db1.key = db2.key
The DATEPART() is embedded in a SELECT which is itself enclosed in ().
The WHERE clause is needed to specify which rows in db2 are used to update which rows in db1.
In T-SQL there is no THEN. See here for details.
I don't think there is equal CASE statement for your query.
Check CASE express in MSDN
Evaluates a list of conditions and returns one of multiple possible result expressions.
Your query is a conditional UPDATE operation which doesn't return anything. Could you clarify your intention here?

Combining Update Queries in 1 SQL statement

I am trying to combine two update queries into one sql statement. I thought I could use a union query to combine the queries but I was not able to. Any advice? The queries I want to combine are as follows:
UPDATE PromotionDatabase
SET PromotionDatabase.PromotionWeekEndingSunday = [PromotionDatabase]![Start Date]+-(Weekday([PromotionDatabase]![Start Date])-1)+7
UPDATE PromotionDatabase
SET PromotionDatabase.PromotionEndingWeekEndingSunday = [PromotionDatabase]![End Date]+-(Weekday([PromotionDatabase]![End Date])-1)+7;
Are you just trying to update 2 fields in the same UPDATE statement:
UPDATE PromotionDatabase
SET PromotionWeekEndingSunday = [Start Date]+-(Weekday([Start Date])-1)+7,
PromotionEndingWeekEndingSunday = [End Date]+-(Weekday([End Date])-1)+7;
The syntax for an update is the following. See w3schools
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value