I'm trying to update all SQL rows in the [Temp_LTGData] table setting the [CORP_REG_NO] value to the value in another row in the same table where the [CUSTOMER_NUMBER] matches.
Ultimately I need to do this with quite a few columns, does anyone know if this can be done?
I can't seem to use the LTGSource alias like in a select query :(
Update [MandS].[dbo].[Temp_LTGData] LTGSource
Set [CORP_REG_NO] = (SELECT [CORP_REG_NO]
FROM [MandS].[dbo].[Temp_LTGData]
WHERE ([CORP_REG_NO] IS NULL
AND [CUSTOMER_NUMBER] = LTGSource.[CUSTOMER_NUMBER] ))
where [CORP_REG_NO] IS NULL
Thanks for the feedback guys that's some really awesome stuff, I even learnt some different approaches to this problem (voted for you all).
Try the following to get exactly what you were doing
UDPATE [MandS].[dbo].[Temp_LTGData] LTGSource
SET [CORP_REG_NO] = (SELECT [CORP_REG_NO]
FROM [MandS].[dbo].[Temp_LTGData]
WHERE ([CORP_REG_NO] IS NULL
AND [CUSTOMER_NUMBER] = ToUpdate.[CUSTOMER_NUMBER] ))
FROM {MandS].[dbo].[Temp_LTGData] ToUpdate
where [CORP_REG_NO] IS NULL
However, you have a mistake in your query I believe the subquery should be IS NOT NULL.
Something like this which will allow you to deal with many columns with a source and target row
If you need to link different rows for different columns, then it's more complex
If I understand correctly, filtering to CORP_REG_NO IS NULL would only work for CORP_REG_NO of course so you don't want to filter except to restrict target and source rows independently of specific column filters as per you're question.
Update
target
Set
[CORP_REG_NO] = CASE WHEN target.[CORP_REG_NO] IS NULL THEN source.[CORP_REG_NO] ELSE target.[CORP_REG_NO] END,
...and again...
FROM
[MandS].[dbo].[Temp_LTGData] target
JOIN
[MandS].[dbo].[Temp_LTGData] source ON target.[CUSTOMER_NUMBER] = source.[CUSTOMER_NUMBER]
WHERE
a filter to restrict rows perhaps
Try this:
UPDATE Temp_LTGData LTGSource
SET Col1 = L2.Col1, Col2 = L2.Col2, Col3 = L2.Col3
FROM LTGSource L1
JOIN LTGSource L2 ON L2.CORP_REG_NO IS NOT NULL AND L1.CUSTOMER_NUMBER = L2.CUSTOMER_NUMBER
WHERE L1.CORP_REG_NO IS NULL
That should do it for you. You're joining the updateable table to itself so you can have access to both the old row and the new row for the update. This way, you can update multiple columns at once.
Related
I have a table A with null dates (CREATED_ON_DT) in BI database. I need to update those nulls with the right dates from AFLDEV DB using a DB link mtl_system_items_b#afldev. Common key is inventory_item_id in AFLDEV and integration_id in BI DB. I have framed the following query but it does not work:
UPDATE w_product_d
SET w_product_d.CREATED_ON_DT = (SELECT min(creation_date)
FROM mtl_system_items_b#afldev B
where to_char(B.inventory_item_id)=w_product_d.integration_id
and B.organization_id = '102'
AND w_product_d.CREATED_ON_DT IS NULL
and w_product_d.integration_id in (SELECT T.integration_id
FROM (SELECT * FROM w_product_d ORDER BY w_product_d.integration_id )T
WHERE T.CREATED_ON_DT IS NULL)
);
If I run this query it updates all the dates to nulls but I need the opposite to happen i.e. replace null with the right dates.
Please help me out with this! I am doing this on SQL Developer for Oracle DB.
I think you've gotten all tied up between the rows you're updating and the rows you're using to update the column values with.
If you think about it, you're wanting to update rows in your w_product_d table where the created_on_dt is null, which means that your update statement will have a basic structure of:
update w_product_d wpd
set ...
where wpd.created_on_dt is null;
Once you have that, it's easy then to slot in the column you're updating and what you're updating it with:
update w_product_d wpd
set wpd.created_on_dt = (select min(creation_date)
from mtl_system_items_b#afldev b
where to_char(b.inventory_item_id) = wpd.integration_id)
where wpd.created_on_dt is null;
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,
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.
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
I have two databases.
Alarm
TMP
I have a table in Alarm, where in a table there is one empty column with null values.
And I have a single column table in TMP.
I want to copy this single column values to my table in Alarm database.
What I tried so far is,
update [Alarm].[dbo].[AlarmDetails] set Alarm_Message = (select * from [TMP].[dbo].[AlarmDetails$])
where 1=1
The error is
Subquery returned more than 1 value.
Please note this,
NOTE: There is no id column in source table. Only one table & one column, Alarm Message.
I know the cause of error, but how should I modify my SQL.
Thank You.
Here's an example of copying a column:
update dst
set Alarm_Message = src.AlarmMessage
from Alarm.dbo.AlarmDetails dst
join TMP.dbo.AlarmDetails src
on dst.id = src.id
You did not specify how the tables are related, so I assumed they both have an id column.
You need something like this.
update t1
set
t1.<something1> = t2.<something2>
from
[Alarm].[dbo].[AlarmDetails] t1
join [TMP].[dbo].[AlarmDetails] t2 on (t1.<cols1> = t2.<cols2>)
UPDATE results SET results.platform_to_insert = (
SELECT correct_platform
FROM build
WHERE results.BuildID=build.BuildID LIMIT 1
);