Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the values as below and i need to check the duplication.

empno deptid
----- ------
101 1
102 2
103 3

checking duplicates condition
-----------------------------

1) if empno is null and depid = '1'
ERROR "Dept Already Exists"
2) if empno = '101' and depid = ''
ERROR "Emp Already Exists"
3) if empno = '101' and depid = '1'
ERROR "Emp/Dept already exist"
ELSE
INSERT the values into table
Posted
Comments
Corporal Agarn 28-Nov-12 12:28pm    
What have you tried? Also is for #2 is depid ever null?
So you can have only unique empno numbers AND unique depid numbers?
DaleMoz 28-Nov-12 19:04pm    
Are you dealing with only one table here? What is the relationship between Emp and Dep?

1 solution

SQL
IF (SELECT COUNT(*) FROM tb WHERE empno=@empno AND deptid=@deptid ) > 0
 PRINT "Emp/Dept already exist&";
ELSE IF (SELECT COUNT(*) FROM tb WHERE empno IS NULL AND deptid=@deptid ) > 0
 PRINT "ERROR Dept already exists";
ELSE IF  (SELECT COUNT(*) FROM tb WHERE empno=@empno AND deptid='') > 0
 PRINT"Emp Already Exists&";
ELSE
 INSERT INTO tb VALUES(@empno, @deptid)


tb - your table, @empno,@deptid - stored procedure input parameters
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900