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
 How to check duplicate value Useing IF EXISTS

Author  Topic 

subrata.bauri
Starting Member

17 Posts

Posted - 2011-04-26 : 03:21:29
Dear sir,

I have two tables:

Table Tab1 ( Col1, Col2 )
Table Tab2 ( Col1, Col2 )

I want to check in the table Tab2 Col2 is there any same value with the table Tab1 in column Col2.

How to do that?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-04-26 : 03:23:16
select * from Tab1 t1 join Tab2 t2 on t1.col2 = t2.col2


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

subrata.bauri
Starting Member

17 Posts

Posted - 2011-04-26 : 03:36:38
Thanks for quick reply Sir,

I want to check with IF EXISTS statement like...

IF EXISTS (SELECT t2.col2
FROM TABLE2 t2
WHERE t2.col2 IN (SELECT t1.col2
FROM TABLE1 t1)
)

But it does not work.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-04-26 : 04:08:59
That's an IF without any action taken for true or false.
What should be the output in your example?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

subrata.bauri
Starting Member

17 Posts

Posted - 2011-04-26 : 04:30:55
IF EXISTS (SELECT t2.col2
FROM TABLE2 t2
WHERE t2.col2 IN (SELECT t1.col2
FROM TABLE1 t1)
)

BEGIN
RAISERROR('
For Service Orders, no_of_item is Mandatory',16,1)
END
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-04-26 : 04:34:36
Now we are coming closer...

What is the meaning of "But it does not work"?

Any error message that can point us in any direction?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

subrata.bauri
Starting Member

17 Posts

Posted - 2011-04-26 : 05:08:09
quote:
Originally posted by webfred

Now we are coming closer...

What is the meaning of "But it does not work"?

Any error message that can point us in any direction?


No, you're never too old to Yak'n'Roll if you're too young to die.



Dear Sir,

EXISTS always retuns true why ? When there is no Data.
Go to Top of Page
   

- Advertisement -