04 July 2014

Logic for two tables mismatched columns

create table staging (clientid int primary key,addressdetails varchar(250));
insert into staging
select 100,'hyderbad,india' union all
select 101,'banglore,india' union all
select 102,'banglore,india'

create table oltp (client_id int primary key,address_details varchar(250));
insert into oltp
select 104,'newyork,usa' union all
select 105,'chicago,usa' union all
select 106,'washington,usa'

select * from oltp where client_id in (select client_id from staging)
o/p:
It returns all 2nd table rows.. instead of raising error that column-name is not existing in table


It will fetch all the values from outer query as inner query referring the same column from outer query .If a column is referenced in a subquery that does not exist in the table referenced by the subquery's FROM clause, but exists in a table referenced by the outer query's FROM clause, the query executes without error. SQL Server implicitly qualifies the column in the subquery with the table name in the outer query.

No comments: