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.
| Author |
Topic |
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2011-04-08 : 12:21:50
|
| Hi,the following is my requirement.Table AKey value100 31102 23Table BKey M_Value100 1A105101 1A153Condition :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_Valuefrom TableB bwhere 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. |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2011-04-08 : 12:57:59
|
| [code]SELECT B.M_valueFROM TableA AS AJOIN TableB AS B ON A.key = B.keyWHERE A.value = 31 AND A.key =@key;[/code]______________________ |
 |
|
|
sqllover
Constraint Violating Yak Guru
338 Posts |
Posted - 2011-04-08 : 13:01:24
|
| Thanks a lot friends |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2011-04-08 : 13:04:47
|
| you are welcome______________________ |
 |
|
|
|
|
|