How to get the latest inserted identity from a table? - sql-server

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'))

Related

Query Using != On LEFT JOIN Table Returns No Result

I am querying a Microsoft SQL Server 2012.
The primary table (T1) structure contains account details:
AccountID, Name, Address
This table is dropped and recreated using external data nightly. We need to display this information but also need to exclude some of the records. Since we have no access to the external data we can't just add a column.
So we created a table (T2) to mark all the accounts we would like to exclude. It just has 2 fields:
AccountNo, Type
So we populated T2 and for every account we wanted to exclude from the display we gave the Type field a value of 'ex' (for exclude). We have no entries for the account we want to display.
When I execute the following query:
select T1.AccountID as acct, T1.Name as name, T1.Address as add
from T1
left join T2 on T1.AccountID = T2.AccountNo
WHERE T2.Type != 'ex'
The above query returns and empty set.
If I run a query to look for the value 'ex' (remove the !):
select T1.AccountID as acct, T1.Name as name, T1.Address as add
from T1
left join T2 on T1.AccountID = T2.AccountNo
WHERE T2.Type = 'ex'
The query returns the rows with that field populated with 'ex', as you expect.
I can search for NULL or NOT null with success but we need to use this extra table to do some other data manipulation in the future. In other words, we will not just be populating this field with "ex".
I'm wondering why I can't query the field in the joined table by looking for a Boolean false for a string. Is is because since the column doesn't exist in the table that is joined (T2) that it doesn't actually exist in the data set?
If that's the case how would I execute a query to return the records that do not equal a value in the joined table, whether that record exists in the joined table or not.
You can use the ISNULL solution like mentioned in the comments.
Another way you could write the query is this:
SELECT #t1.AccountID AS acct, #t1.Name AS [name], #t1.Address AS [add]
FROM #t1
LEFT JOIN #t2 ON #t1.AccountID = #t2.AccountNo
AND #t2.type = 'ex' --In case you add additional types to #t2
WHERE #t2.AccountNo IS NULL;

TSQL Copy New Contents to Archive Table Only

I have an ArchiveTable that I want to periodically copy any new records from OriginalTable. This is something I thought may work.
INSERT INTO OriginalTable
SELECT *
FROM ArchiveTable
WHERE NOT EXISTS (SELECT *
FROM OriginalTable ot
INNER JOIN ArchiveTable at ON ot.email = at.email)
Simply doing something like..
INSERT INTO ArchiveTable
SELECT * FROM OriginalTable
Of course, only works for the initial copy.
Your current query:
INSERT INTO OriginalTable
SELECT * FROM ArchiveTable
WHERE NOT EXISTS
(SELECT * FROM OriginalTable ot
INNER JOIN ArchiveTable at
ON ot.email = at.email)
Uses an EXISTS subquery that isn't related to the outer query. So it's saying, "if no row exists in the original table that has the same email as any row in the archive table, then insert everything in the archive table into the Original table."
Probably not what you want. You probably want to insert the specific rows that do not already exist in the original table. So you would want to correlate the subquery to the outer query:
INSERT INTO OriginalTable
SELECT * FROM ArchiveTable at
WHERE NOT EXISTS
(SELECT * FROM OriginalTable ot
WHERE ot.email = at.email)
This query says, "insert into the original table, any rows in the archive table where I don't already have the Email in the Original table".

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)

Delete Data from One Table using multiple tables in SQL Server

I am trying to delete some data from one table using multiple table. The problem i am having with this query is that it is deleting data that is not suppose to delete. I want to delete only the Data where ID's in both tables are DIFFERENT. In other words, i want to keep when the ID's in both tables are the same. Here is my query:
delete Tabel1
from Table1 r join
Table2 w on r.ID <> w.ID
and w.Date_Assigned is not null
You probably want to do:
delete Tabel1 where Id not in (select Id from Table2)
Your statement will probably delete all records from Tabel1, because for every record it will find at least one in Tabel2 with a different ID.
To test this, just run
select r.ID
from Table1 r join
Table2 w on r.ID <> w.ID
and w.Date_Assigned is not null
and you'll understand what the problem is.

Iterating 1 row at a time with massive amounts of links/joins

Ok, basically what is needed is a way to have row numbers while using a lot of joins and having where clauses using these rownumbers.
such as something like
select ADDRESS.ADDRESS FROM ADDRESS
INNER JOIN WORKHISTORY ON WORKHISTORY.ADDRESSRID=ADDRESS.ADDRESSRID
INNER JOIN PERSON ON PERSON.PERSONRID=WORKHISTORY.PERSONRID
WHERE PERSONRID=<some number> AND WORKHISTORY.ROWNUMBER=1
ROWNUMBER needs to be generated for this query on that one table though. So that if we want to access the second WORKHISTORY record's address, we could just go WORKHISTORY.ROWNUMBER=2 and if say we had two address's that matched, we could cycle through the addresses for one WORKHISTORY record using ADDRESS.ROWNUMBER=1 and ADDRESS.ROWNUMBER=2
This should be capable of being an automatically generated query. Thus, there could be more than 10 inner joins in order to get to the relevant table, and we need to be able to cycle through each table's record independently of the rest of the tables..
I'm aware there is the RANK and ROWNUMBER functions, but I'm not seeing how it will work for me because of all the inner joins
note: in this example query, ROWNUMBER should be automatically generated! It should never be stored in the actual table
Can you use a temp table?
I ask because you can write the code like this:
select a.field1, b.field2, c.field3, identity (int, 1,1) as TableRownumber into #temp
from table1 a
join table2 b on a.table1id = b.table1id
join table3 c on b.table2id = c.table2id
select * from #temp where ...

Resources