Combining Update Queries in 1 SQL statement - sql

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

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;

Find multiple SQL columns and update based on defined data listed in query

I have an update query in which I am trying to locate data in a column from a single table. All while taking other defined data listed in the query to update another column in the same table once a match has been found with that original search. Below is an example of my update statement. My end goal is to find '003447710' then update AltId to '540112'
UPDATE Site
SET AltId = ('540112'
'540129'
'540142'
'540143')
WHERE CCMFStatus in ('003447710',
'002754540',
'003564370',
'005942870')
I am sure there may already be something like this out there but I am really having trouble on an easy method on how to do this quickly and accurately.
Try this
update site
set altid = a.altid
from
(select altid,CCMFstatus from site) as a
where site.CCMFstatus = a.CCMFstatus
The best way might be multiple update statements:
UPDATE Site
SET AltId = '540112'
WHERE CCMFStatus = '003447710';
And so on.
If not, you can do this with a giant case statement or a join:
WITH values as (
SELECT '003447710' as oldstatus, '540112' as newaltid UNION ALL
SELECT '002754540', '540129' UNION ALL
SELECT '003564370', '540142' UNION ALL
SELECT '005942870', '540143'
)
UPDATE s
SET AltId = va.newaltid
FROM site s JOIN
values v
ON s.CCMFStatus = v.oldstatus;
If you already have the values in a table, then you don't need the WITH statement. You can just use the table.
Have you tried using CASE statement?
UPDATE SITE SET AltID = (CASE
WHEN CCMFStatus = '003447710' THEN '540112'
WHEN CCMFStatus = '002754540' THEN '540129'
END)
WHERE
CCMFStatus in ('003447710', '002754540', '003564370', '005942870');
BR,

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 update more than one column in SQL?

I have a SQL which I am using for updating many rows at the same time using a complex case condition. Currently, I am setting 2 column using the same CASE condition.
For example, I need to do something like:
UPDATE MyTable
SET([MyColumn1], [MyColumn2]) = ('','')
What I am doing now is:
UPDATE MyTable
SET [MyColumn1] = COMPLEX CASE RETURN STRING,
[MyColumn2] = 100% SAME COMPLEX CASE RETURN A Different STRING
I tried but getting error. Is this is possible in SQL SERVER?
It is supported in IBM Db, http://publib.boulder.ibm.com/infocenter/idshelp/v111/index.jsp?topic=/com.ibm.sqls.doc/sqls919.htm
But it seems there is no support in SQL Server
Update command syntax is:
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
update mytable
SET [MyColumn1] = COMPLEX CASE RETURN STRING,
[MyColumn2] = 100% SAME COMPLEX CASE RETURN A Different STRING
WHERE some_column=some_value;// please have the where condition
then only we can update the corresponding raw.
updating means we are changing some existing values so we need to provide the location where we want this change,for that we can use the where condition..
example
update author
set name="onv kurup"
set book="oralude"
where authorid=112;
if we are not giving the where condition all the data of the table will be get updated with same value in the update query we have given
After searching a lot, I have found the answer,
update table1
set col1 = a.col1, col2 = a.col2, col3 = a.col3 from
table1 as a Join on tablefunction
where table1.col1 <expression>
http://geekswithblogs.net/phoenix/archive/2009/10/13/update-multiple-columns-on-sql-server.aspx

Regarding joins and subquery

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