SQL Server : update old ID to new ID (SELF JOIN in UPDATE) - sql-server

I have a SQL Server table, and column category IDs (kategori) needs to be updated.
I need to find id_old first and update as new id instead:
How can I do this in batch with a T-SQL statement?

use SELF JOIN. You need to use a different alias on the table.
UPDATE t2
SET kategori = t1.id
FROM yourtable t1
INNER JOIN yourtable t2 ON t1.id_old = t2.kategori
WHERE t2.kategory <> 0 -- not sure but you might need this condition

Related

Updating a column in SQL table where items are identified in another table that is linked

I have two tables in MSSQL.
Table1
Table2
I want to update the Status column in Table 1 to "YES" if the same Ticket ID, House and Part Number exists in Table 2. After updating, Table 1 should be like,
How can I achieve this?
Thanks
a simple EXISTS() will do the job
UPDATE t1
SET Status = 'Yes'
FROM Table1 t1
WHERE EXISTS
(
SELECT *
FROM Table2 t2
WHERE t1.TicketID = t2.TicketID
AND t1.House = t2.House
AND t1.PartNumber = t2.PartNumber
)
or an INNER JOIN will gives you the query that you want

Update a column of a table with a column of another table in SQL Server

I am trying to update column from one table to another table of different database.
I searched on stack but found answers for PostgreSQL and SQLite. Those code ain't worked for me properly in SQL Server.
Say,
D1,D2 are the two different databases
T1 is the table
C1,C2,C3 are the columns in T1
I want to update like
UPDATE T1
SET D2.T1.C1 = D1.T1.C1
WHERE D2.T1.C2 = D1.T1.C2
Everything except the where clause works fine.
Here is some code I tried:
use DB2
GO
UPDATE TABLE1
SET COL1 = (SELECT COL1 FROM DB1.dbo.TABLE1)
WHERE COL2 = DB1.dbo.TABLE1.COL2
How shall I write this query?
Note: D1 and D2 are identical. Both have the exact same schema. Just the name and data are different. Both databases are on the same server.
SQL Server supports an update join syntax:
UPDATE t1
SET COL1 = t2.COL1
FROM TABLE1 D1.t1
INNER JOIN TABLE2 D2.t2
ON t1.COL2 = t2.COL2;
Actually, your current approach might work, but you should try changing it to this:
UPDATE TABLE1 D1.t1
SET COL1 = (SELECT t2.COL1 FROM TABLE2 D2.t2 WHERE T1.COL2 = T2.COL2);
You can simply use an INNER JOIN cause sql-server support Cross-Database Queries, so you can do as:
UPDATE T1
SET T1.Col1 = T2.Col1
FROM DataBase1.Schema.TableName T1 INNER JOIN DataBase2.Schema.TableName T2
ON T1.ID = T2.ID;
Please, visit and read Cross-Database Queries.
Or update tableName set column Name = (select columnName from second table where 1=1) i.e condition is either true or false optional though

Update table based on join multiple rows SQL Server

I'm trying to update some rows in a table joined to another table. The left table is indexed by ID; the right table is joined on the same ID but has multiple rows per ID. My query looks like:
UPDATE t1
SET t1.modified = t2.created
FROM table1 t1
INNER JOIN table2 t2
ON t1.ID = t2.ID
Note that t1.modified and t2.created are both datetime.
As stated, t2 has several rows per ID, each with a different created value (so the primary key is t2, created). What I want to do is set the max value of t2.created=t1.modified. However, when joining, the t1.modified value gets updated in no particular order, so whichever row is updated last, that's the value that t1.modified gets. I tried using t1.modified=max(t2.created), but apparently I can't use aggregate functions in an update query, nor can I use an order clause (i.e. order the rows so that the last row updated will effectively be the latest value of t2.created) in the update query.
Any help you can provide me is much appreciated! Thanks!!
how about this? will this fit your need?
UPDATE t1
SET modified = isnull((SELECT max(t2.created)
FROM table2 t2
WHERE t2.ID = t1.ID), modified)
FROM table1 t1;
Use the isnull function to set modified to be itself if the value returned is null. that should take care of the null issue.
You could do it this way, although it may not be standard SQL. However it should work for SQL Server.
update t1
set t1.modified = (select max([created]) from t2 where t1.ID = t2.id)

How to get the latest inserted identity from a table?

I would like to retrieve the latest inserted or even updated identity from a table. But whenever I write the following code there is no results at all.
select *
from PersonHowEducation prh
inner join HowzeEducation he on prh.HowzeEducationId = he.HowzeEducationId
where he.HowzeEducationId = ##IDENTITY
What shall I really do ?
If this were part of a batch script, and you have only just inserted it in the same session, then you would use SCOPE_IDENTITY() to retrieve the last inserted HowzeEducationId value.
select * from PersonHowEducation prh inner join HowzeEducation he on
prh.HowzeEducationId=he.HowzeEducationId
where he.HowzeEducationId=SCOPE_IDENTITY()
However, if this is from a completely different session, one that DID NOT JUST perform the insert, then you can get the last record inserted using
select * from PersonHowEducation prh inner join HowzeEducation he on
prh.HowzeEducationId=he.HowzeEducationId
where he.HowzeEducationId=(select top(1) HowzeEducationId from HowzeEducation
order by HowzeEducationId desc)
Note: There could be rare cases where an identity insert does not create the largest id in the table, for example if someone decided a table should have 1 record of id= 1,000,000,001 and manually reseeded the identity back to 10,001.
Another alternative, which may fizz if it is possible for the record to be deleted after insertion.
select * from PersonHowEducation prh inner join HowzeEducation he on
prh.HowzeEducationId=he.HowzeEducationId
where he.HowzeEducationId=(SELECT IDENT_CURRENT('HowzeEducation'))

Trigger in SQL Server 2008 R2

I am trying to create a trigger in SQL Server 2008 R2. I have three tables and they have PK-FK -FK relationship.
I have a column in first table where column name is STATUS that column has value 'inactive' or 'active' and in second table I have column FKNEWLoanreviewfieldid which has multiple values C2,C4,C6 and in third table I have FKWarningtypeID has different value like 4,3,2,1.
I have to create a trigger so that if FKwarningtypeID is updated to value 4 then in first table (where I have status column) the column value of status will be modified to 'active'.
Can any body help me please.
You need something like this:
CREATE TRIGGER trg_UpdateYourColumn
FOR UPDATE ON dbo.YourThirdTableHere
AS
-- update the first table
UPDATE
dbo.YourFirstTableHere
SET
-- set [Status] to 'Active'
-- (I sure hope this isn't *REALLY* stored as a *STRING* value!)
[Status] = 'Active'
FROM
dbo.YourFirstTableHere t1
INNER JOIN
-- join first and second table on some common column
dbo.YourSecondTableHere t2 ON t1.XX = t2.XX
INNER JOIN
-- join second and third table on some common column
dbo.YourThirdTableHere t3 ON t2.YY = t3.YY
INNER JOIN
-- join third table with the "Inserted" pseudo table
Inserted i ON t3.ID = i.ID
INNER JOIN
-- join third table with the "Deleted" pseudo table
Deleted d ON t3.ID = d.ID
WHERE
i.FKwarningtypeID = 4 -- new value is 4
AND d.FKwarningtypeID <> 4 -- old value was *NOT* 4

Resources