SQL DELETE query with NOT IN clause not working - sql-server

i have a simple delete query i have two tables table1 and table2
the query is
DELETE FROM table1
WHERE end_time NOT IN (SELECT end_time
FROM table2
WHERE comp_name = 'component')
AND id = 'id'
the query does not delete the record what am i missing here i tried to change the first condition with second but it deletes without checking the condition 2
what are the alternatives here?
i am wondering what is the actual problem here the query runs without errors but it is not correct.

May be alias should fix your problem since the both the tables have same column name end_time. Outer query column can be referred inside the subquery that might be the problem.
DELETE FROM table1
WHERE end_time NOT IN (SELECT b.end_time
FROM table2 B
WHERE b.comp_name = 'component')
AND id = 'id'
Update : Try this delete because you may have null values in your sub query where NOT IN will fail. Check this post link
DELETE a
FROM table1 a
WHERE NOT EXISTS (SELECT 1
FROM table2 B
WHERE b.comp_name = 'component'
AND a.end_time = b.end_time)
AND id = 'id'

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

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;

Subquery returned more than 1 value error after executing update query

I am using sql-server 2012 and i have a strange problem in updating a table.
My select query returns tree rows and is like below:
select * from
TAble1 p join
(select ProductId=max(ProductId) from Table2 s group by s.ProductId) pin on p.id=pin.ProductId
where p.categoryid=238
and the returned row is:
Now, When i run this update query:
update TAble1 set sizing=0 from
TAble1 p join
(select ProductId=max(ProductId) from TAble2 s group by s.ProductId) pin on p.id=pin.ProductId
where p.categoryid=238
I got this error:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
The statement has been terminated.
Where is the problem in my query?
Looks like the problem that generates an exception is somewhere else (inside of a trigger for example).
This line could be the reason why there is more than one row updated
(select ProductId=max(ProductId) from TAble2 s group by s.ProductId)
If you want to obtain max ProductID (a single value) - remove it from GROUP BY clause. Currently you are requesting server to return maximum from a single value - which is absurd. It simply returns a list of all ProductID values from Table2. Which is the same as
select distinct ProductID from Table2
This select ProductId=max(ProductId) from Table2 s group by s.ProductId
will give you DISTINCT ProductId's, but not MAX. And You dont have link with TAble1 In fact your query will update all TAble1.sizing column.
Try this:
UPDATE TAble1
SET sizing = 0
FROM TAble1 p
JOIN
(SELECT max(s.ProductId) AS ProductId
FROM TAble2) pin
ON p.id = pin.ProductId AND p.categoryid=238
WHERE p.categoryid = categoryid
I think a better question is what isn't the problem in your query. In your SELECT what exactly are you supposed to be joining? And what index are you joining it to? You're using a GROUP BY but not including the GROUP BY as a column in your SELECT. You don't need to alias the 'TAble2 s' in the subquery. TAble p doesn't have a categoryid column based on what you've shown. You shouldn't need the FROM clause in the UPDATE query in most cases, especially since you're just setting a column to a static value.
But to answer your question: the subquery: "select ProductId=max(ProductId) from TAble2 s group by s.ProductId" returns all ProductId rows, so it fails when you're trying to join.
Since you're not using info from Table2 why not just simply update like this:
update TAble1 set sizing=0 where categoryid=238

Where statement with conditions is multiple tables

I need my code to set values of a column based on two criteria: the value of a different column in the same table and a value of a column in a different table. Is there a way to use the where statement to look at criteria in different tables?
Below is pseudo code that doesn't work but captures what I am trying to do.
UPDATE Table1
SET CustomerStatus_2003 = 'New'
FROM Table1, Table2
WHERE Table1.Column1 = 'New'
AND Table2.Column1 = '2003'
AND Table1.Column2= Table2.Column2
Thanks for your help!
You should always alias your tables and use a JOIN when you are using more than one table in a statement. Here is some code that does what you are looking for
UPDATE t
SET myColumn = 'New'
FROM MyTable t
JOIN OtherTable t2 ON t.Column = t2.Column
WHERE t.SomeCondition
AND t2.SomeOtherCondition

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)

Resources