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
 Help needed in logic writing

Author  Topic 

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2011-04-08 : 12:21:50
Hi,
the following is my requirement.

Table A
Key value
100 31
102 23

Table B

Key M_Value

100 1A105
101 1A153

Condition :
User will provide the key. Using the key i have to check the Table A
"value" column. If the "value" = 31 then i will have to fetch the Table B "M_Value" for the key.

simply i can say if the user gives the key as 100 then it should check for the value column of Table A and if it has 31 then i have to get the respective key data from column M_value of TableB that is 1A105.

Is is possible to do this requirement in suing query rather than writing stored procedure.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-04-08 : 12:49:10
You can write a query like this to do what you are trying to do:
declare @userKey int;
set @userKey = 102;

select
b.[Key],
b.M_Value
from
TableB b
where
b.[Key] = @userKey
and exists (select * from TableA a where a.[Key] = b.[Key] and a.[Value] = 31)

Then you probably do want to wrap it in a stored procedure with @userKey as a parameter which could then be called.
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2011-04-08 : 12:57:59
[code]SELECT B.M_value
FROM TableA AS A
JOIN TableB AS B
ON A.key = B.key
WHERE A.value = 31
AND A.key =@key;[/code]

______________________
Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2011-04-08 : 13:01:24
Thanks a lot friends
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2011-04-08 : 13:04:47
you are welcome

______________________
Go to Top of Page
   

- Advertisement -