Unusual request behavior

I found this request from the developer:

DELETE FROM [MYDB].[dbo].[MYSIGN] where USERID in
(select USERID from [MYDB].[dbo].[MYUSER] where Surname = 'Rossi');

This query deletes each entry in the MYSIGN table .

The USERID field does not exist in the MYUSER table . If I run only the subquery:

select USERID from [MYDB].[dbo].[MYUSER] where Surname = 'Rossi'

It throws the correct error because the column is missing.

We adjusted the query using the right column, but we did not find out:

  • Why does the first request work?
  • Why does he delete every entry?

Specifications: the database is located on SQL SERVER 2016 SP1, CU3.

+4
source share
2 answers

-, USERID [MYDB].[dbo].[MYSIGN], sql-server unprefixed USERID (select USERID from [MYDB].[dbo].[MYUSER] where Surname = 'Rossi') - [MYDB].[dbo].[MYSIGN].USERID

DELETE FROM [MYDB].[dbo].[MYSIGN] where USERID in
(select t.USERID from [MYDB].[dbo].[MYUSER] t where Surname = 'Rossi');

- " ", @NenadZivkovic, .

+6

. , SQL - ( SQL Server).

, , . , ( ) . :

DELETE
    FROM [MYDB].[dbo].[MYSIGN] m
    WHERE m.USERID IN (SELECT u.USERID FROM [MYDB].[dbo].[MYUSER] u WHERE u.Surname = 'Rossi');

.

+1

Source: https://habr.com/ru/post/1680801/


All Articles