| Author |
Topic |
|
Jothikannan
Starting Member
36 Posts |
Posted - 2006-01-25 : 08:56:40
|
| hi,Delete tst1 from tst1 a(nolock)where a.col1=2The above delete Stmnt Deleting only rows where "col1=2" in table tst1but the query donot have any joins between tst1 and alias aHow it is deleting only col1=2 ,instead of deleting all rows in tst1??Plz, Can Anyone help me !? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-25 : 08:59:30
|
| The query Delete tst1 from tst1 a(nolock)where a.col1=2is equivalent toDelete from tst1 (nolock)where col1=2MadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-25 : 08:59:59
|
"a" is an alias for "tst1" in the above example. So no second table is required.Perhaps consider the more longhand version:Delete tst1 from tst1 AS a(nolock)where a.col1=2However, having said that, I would have expected all references to "tst1" to need changing to "a" - i.e.Delete tst1 aKristen |
 |
|
|
Jothikannan
Starting Member
36 Posts |
Posted - 2006-01-25 : 09:24:36
|
| see, the following query Delete tst1 from tst2 a(nolock)where a.col1=1In "from " i have used table "tst2" if a.col1=1 is true in table "tst2",it is Deleting all rows in "tst1"and if a.col1=1 is false in table "tst2",it is Deleting Zero rows in "tst1"the above activity is fine, but i m expecting the same behaviour for the previous Query .But it is Deleting abnormally. so it is confusing!!! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-25 : 09:28:14
|
| Can you post some sample data and the result you want?MadhivananFailing to plan is Planning to fail |
 |
|
|
Jothikannan
Starting Member
36 Posts |
Posted - 2006-01-25 : 09:37:45
|
| thnks madi...Create table tst1 (col1 int)insert tst1 select 1insert tst1 select 2insert tst1 select 3Create table tst2 (col1 int)insert tst2 select 4insert tst2 select 5insert tst2 select 6Begin Tran Delete tst1from tst2 a(nolock)where a.col1 = 5rollback tranthe above deleting 3 rows ,ok thats fineBegin Tran Delete tst1from tst2 a(nolock)where a.col1 = 1rollback tranthe above deleting zero rows ,ok thats fineBegin Tran Delete tst1from tst1 a(nolock)where a.col1 = 2rollback tranHere is my problem ,it should delete 3 records why it deleting 1 record? |
 |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2006-01-25 : 10:53:20
|
| The last record is deleting only the 1 row because it is matching the criteria to the 2nd row in table tst1, the rest are deleting all rows in tst1 because the where clause is basically trying to do a logical operation rather than a true test of the data due to a lack of referential constraints.. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-01-27 : 05:04:05
|
| There are no relations between the tables. As said there should be referencial integrityMadhivananFailing to plan is Planning to fail |
 |
|
|
Jothikannan
Starting Member
36 Posts |
Posted - 2006-01-27 : 10:09:32
|
| tnk Ricky & Madii m Still getting Confusing is any syntax available for Update like this?how its putting Proper join b/w 2 tables?Pls.. Help ME |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-28 : 02:06:27
|
What I normally do is to update an Alias, rather than a table name.So I would do:UPDATE USET MyColumn = 'foo'FROM MyTable AS UWHERE MyOtherColumn = 'bar' orUPDATE USET MyColumn = 'foo'FROM MyTable1 AS U JOIN MyTable2 AS T2 ON T2.MyPrimaryKey = U.MyPrimaryKeyWHERE MyOtherColumn = 'bar' I find this easy to read, and easy to change from single table to multi-table JOIN if/when I need to.I always use U for the update table alias, so there is no chance of me accidentally using a tablename instead of an alias, etc.Kristen |
 |
|
|
|