Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 delete from a table

Author  Topic 

rjhe22
Constraint Violating Yak Guru

283 Posts

Posted - 2012-09-06 : 07:07:13
hi
i want to delete stuff from a table based on a column

so eg

id first last
1 john joe
2 paul smith
1 ronan waldron

so what i want is to say delete the 1 from the table without have to put
is like this
delete from tbale 1
where id ='1'

any ideas

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-06 : 07:32:23
in this example: delete ALL rows with id=1?


Too old to Rock'n'Roll too young to die.
Go to Top of Page

rjhe22
Constraint Violating Yak Guru

283 Posts

Posted - 2012-09-06 : 07:45:50
ya all rows that are 1 but with out actually putting the number into the query
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-06 : 07:57:18
Try this:

delete t1
from YourTable as t1
join (select id from YourTable group by id having count(*) > 1)t2
on t1.id = t2.id



Too old to Rock'n'Roll too young to die.
Go to Top of Page

rjhe22
Constraint Violating Yak Guru

283 Posts

Posted - 2012-09-06 : 09:26:25
hi ya thats looks good
but say i want to do it where its not a number its a name will the code still work then
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-09-06 : 09:31:49
Yes. Just replace each occurence of "id" with the needed column name and be sure to have a test...


Too old to Rock'n'Roll too young to die.
Go to Top of Page

xploddbash
Starting Member

2 Posts

Posted - 2012-09-06 : 19:40:09
Just base it to the name when they have the same "id". The column would be deleted with that.
Go to Top of Page

rjhe22
Constraint Violating Yak Guru

283 Posts

Posted - 2012-09-13 : 05:35:33
hi
sorry just getting back to this now
that delete still deletes all the data from table
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-13 : 10:23:49
what do you mean by all data?

are you looking at specifically removing data from a column in a row having a value or is your attempt to remove entire row having the required data?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -