Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two related table.
table 1:
----------------
ID int not null,
FirstName char(10) null,
LastName char(10) null
================

table2:
-----------------
pID int not null,
FK_ID int null,
Mail char(10) null,
Phone char(10)null
=================

now in a sp I want compare values of input parameters with values of these tables:

SQL
CREATE PROCEDURE [dbo].[CompareValus]
(@FirstName char(10),
@LastName char(10),
@Mail char(10),
@Phone char(10),
@result bit output
)
as
begin
  DECLARE @ID INT
  DECLARE @pID INT
  select  @ID=ID from table1 where FirstName=@FirsName and LastName=@LastName)
  select @pID=pID from table2 where FK_ID=@ID

--?????????
 if(FirstName=@FirsName and
    LastName=@LastName and
    Mail=@Mail and
    Phone=@Phone ) 
  @result=1
--?????????
end

the problem is I want comparison happen exactly in @ID in table1 and @pID in table2
but I dont know how.

(I'm using sql 2005 so I cant use temp tables)
Posted
Updated 17-Feb-15 17:47pm
v2
Comments
Rajesh waran 17-Feb-15 23:40pm    
What is your expecting output? i think you are in need of Joins. Can you show some table records?
mit62 17-Feb-15 23:46pm    
I declare Tables in my question, My expecting output is @result.
if all input parameters be equal with Field of two table then @result=1

1 solution

If I understood you correctly, you need to know if a record exists in both tables based on the ID and FK_ID fields. If that's true, you can use a simple join query like
SQL
SELECT COUNT(*)
FROM Table1 a 
     INNER JOIN Table2 b ON a.ID = b.FK_ID
WHERE a.FirstName = @FirstName
AND   a.LastName  = @LastName
AND   b.Mail      = @Mail
AND   b.Phone     = @Phone

That should the amount of rows that are matching and zero if none.
 
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