SQL Server 2008: Update columns if its a null - sql-server-2008

I have 100 columns in my table and I want to update only columns that have NULL value.
I am comparing master table columns with temp table and trying to update master table column value with temp table column value.
Please help me in this regards.
Thanks in advance

Something like:
Update t1
set field1 = coalesce(t1.field1, 'test')
, field2 = coalesce(t1.field2, t1.field1)
, field3 = coalesce(t1.field3, t2.field1)
, field4 = coalesce(t1.field4, t2.field1, t2.field3)
FROM table1 t1
join table2 t2
on t1.someid = t2.someId
I have given you three examples of differnt ways you might update if the field is null. The first shows how to set it to a text value, the second how to set it to another field in the same table and third is the one where you get the value from a different table. The forth shows what to do if the value you are setting it to is also nul and thus want to use still another value in its place. You will need to write a coalesce update for each of the 100 columns.

The following should update a row in the master table with either its previous value (if it is not null) or the value from the temp table if the value of the master table is null.
This requires that master and temp table can be joined on a key field named Key in my case.
UPDATE m
SET
m.field1 = ISNULL(m.field1, t.field1),
...
FROM
MasterTable m
INNER JOIN TempTable t ON t.Key = m.Key

UPDATE B SET B.value = T.value
FROM
tblMaster B
INNER JOIN tblTemp T ON B.ID = T.ID
WHERE B.value IS NULL

Related

mysql copy data from table to another with the same value?

I'm trying to update my table from another table
I need to update the IP in table1 from the new_IP in table changeip according to the SIM number (whcih is the same on both tables)
I have try to do it from what it say here :
mysql update column with value from another table
I get error "IP can't be null"
this is what I wrote in the command line
UPDATE table1
SET table1.IP = (
SELECT changeip.New_IP
FROM changeip
WHERE table1.SIM_NEW = changeip.SIM_Number
);
what am I doing wrong ?
****************update
this is table1
'10.226.202.169', '8997250000031944123'
'10.226.202.170', '8997250000031944131'
'10.226.202.173', '8997250000031944164'
'10.136.136.101', '8997250400019201597'
'10.136.136.102', '8997250400019201589'
'10.136.136.103', '8997250400019201571'
'10.136.136.104', '8997250400019201563'
and so on........
this is changeip table
'10.226.202.169', '8997250000031944123', '10.136.137.221'
'10.226.202.170', '8997250000031944131', '10.136.137.222'
'10.226.202.173', '8997250000031944164', '10.136.137.223'
'10.226.202.174', '8997250000031944172', '10.136.137.224'
'10.226.202.175', '8997250000031944180', '10.136.137.225'
'10.226.202.177', '8997250000031944206', '10.136.137.226'
Thanks ,
you need join and update
UPDATE table1 t
inner join changeip p
on t.SIM_NEW= p.SIM_Number
and t.IP=p.old_ip
SET t.IP =p.New_IP
ALTER table1 MODIFY IP varchar(20) null;
alter the column from not null to null if the other table consist null values
This should work if you want to update those that have an IP on the changeip table and leaves those that don't have a new IP with the old IP:
UPDATE table1
SET table1.IP = (
SELECT changeip.New_IP
FROM changeip
WHERE table1.SIM_NEW = hangeip.SIM_Number
AND hangeip.New_IP IS NOT NULL
);
An easy way to achieve the required result is as follows:
Based on your data the start tables are
and
If you run the query
UPDATE table1, changeip
SET table1.IP = changeIP.old_IP
WHERE table1.SIM = changeip.SIM_Number
The outcome is as follows:
Which I believe is the desired result. Most of the suggestions above are missing the WHERE clause in the UPDATE statement and that is why they fail.

How do I add the result of a SELECT as a new column in an existing table?

In SQL (whichever variant you like, say MySQL or MonetDB) it's very intuitive and straightforward to do:
CREATE TABLE t2 AS SELECT c1 FROM t1;
and get the selection result as a new column in a new table. But what if you want the result as a new column in the same table (table t1)? Let's assume c1 is of type INT and may be null. We would need to write:
ALTER TABLE t1 ADD c2 INT;
UPDATE TABLE t1 SET c2 = c1;
and that's the easy version, since if it's a non-null column we would need to initialize the new column with some value; and if the data comes from multiple tables I'd need some kind of inner query in the UPDATE (if it would be at all possible).
Can I select-into-a-column with a single command somehow?
You use join:
UPDATE t1 JOIN
t2
ON t2.? = t1.?
SET t1.c2 = t2.c1;
The ? is a placeholder for the column used to identity which row in t2 should update which row in t1.

MySQL You can't specify target table X for update in FROM clause

Any SQL gurus out there who can rewrite the following query:
UPDATE cmsTemplate
SET master = NULL
WHERE master IS NOT NULL
AND master NOT IN (SELECT nodeId
FROM
( SELECT * FROM cmsTemplate a) b
)
So that it does not produce the following error:
You can't specify target table 'cmsTemplate' for update in FROM clause
Issue documented here:
http://dev.mysql.com/doc/refman/5.6/en/update.html
Thanks,
Steve
UPDATE: Description of query
The idea of the query is to do as follows:
SET the master field TO NULL
WHERE the master field IS NOT NULL
AND WHERE the master field IS NOT EQUAL TO ANY of the values for the nodeId field records (within the same table)
You can use update with join:
update cmsTemplate c1 left join cmsTemplate c2
on c1.`master` = c2.nodeId
set c1.`master` = null
where c2.nodeId is null;

Moving Data from Table to Table with new Column

I have been trying to move some data which relates to a specific column from one table to another. They both have a matching objectID.
So what I am trying to do is:
TABLE 1
ObjectID
Field with Data
TABLE 2
ObjectID
FIELD with NEW column
So the object ID relate to each other. All it is I am trying to do is move the data from Table 1 to Table 2 with the new column.
I have tried following but cant seem to get it all working. Is there anything that can be suggested that may help or point in me the right direction.
update Table2 a
Set a.NewColumn = (Select *
From Table1 b WHERE a.OBJECTID = b.OBJECTID
)
Hm, maybe I don't get it but why don't you use a INSERT?
INSERT INTO TableB(...columns...)
SELECT ...columns...
FROM TableA
You can use join update syntax for this, need to make sure that the Table2 already has data and you are updating a new column in there from Table1
update Table2 t2
join Table1 t1 on t1.OBJECTID = t2.OBJECTID
set t2.NewColumn = t1.Field
You can write query like this.
INSERT INTO Table2(ObjectID,Field)
SELECT ObjectID,Field
FROM Table1.
And you can put any default value in extra column.

updating multiple blank fields in one table with data from another table

I am trying to find all fields in one table that are empty and populate them with data from another table. Here is what I have:
UPDATE t1 SET col1 = (
SELECT col99
FROM t2
WHERE t1.product_ID = t2.product_ID
) WHERE col1 IS NULL
which works perfectly for updating all the blank fields in col1 of t1. But I also need to check for blanks in other fields and update them. I don't want the query to update all the fields each time any one of them is blank, just the blank field. I could run multiple queries but I have to imagine there is a cleaner, better way.
Thanks,
Matthew
Try:
UPDATE t1
SET
col1 = COALESCE(col1, (SELECT col99 FROM t2 WHERE t1.product_ID = t2.product_ID)),
col2 = COALESCE(col2, (SELECT ...)),
-- etc.
Should not perform better than your initial solution. It's just doing the job with only one UPDATE query instead of several.