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 |
|
Johnny1985
Starting Member
26 Posts |
Posted - 2008-09-06 : 05:58:46
|
| Hello !-----------Ports table-----------SwitchIDPortIDPortNameVLAN_IDVLAN_Name-----------VLANs table-----------SwitchIDVLAN_IDVLAN_NameI have a stored procedure that accepts @switchID and @portID in order to retrieve results from Ports table. The problem is that I need to get the VLAN_Name from the VLANs table also (without creating a new @vlanID variable of course). I've tried INNER JOIN on SwitchID but it then returned also the VLAN_Names which don't belong to the port.I need to have the port's VLAN_ID seperated so that I can query the VLANs table to get the right record.Therefore I was wondering if there's a way to seperate the SP into two: 1) Will get the port record based on the variables given,2) Will get the VLAN record based on the 1)'s result set VLAN_ID field.I would really appreciate any help what-so-ever .Thanks. |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2008-09-06 : 07:04:09
|
| Why are you storing the VLAN_Name in the ports table? It's VLans table as well. Assuming the VLAN_ID is the primary key of the VLANs table, that's what should appear in the Ports table, with a appropriate foreign key.Likewise, why's the Swithc_ID in both tables. Unless it's a foreign key (in which case the VLANID isn't) it should only be in one of those tables.Assuming that the VLAN_ID is the foreign key between the two tables (which is in no way clear from what you said) this should get what you want.SELECT V.VLAN_Name, p.* FROM Ports p inner join VLANs V ON p.VLAN_ID = V.VLAN_ID--Gail ShawSQL Server MVP |
 |
|
|
Johnny1985
Starting Member
26 Posts |
Posted - 2008-09-06 : 07:20:02
|
| Oh sorry, you're right ..Let me rephrase:-----------Ports table-----------SwitchID Primary Key 1PortID Primary Key 2PortNameVLAN_ID-----------VLANs table-----------SwitchID Primary Key 1VLAN_ID Primary Key 2VLAN_NameThis design was due to the fact that there may be more than one switch and each one might have the same VLAN_ID or PortID, for example:SwitchID PortID PortName VLAN1 1 Fa 1/1 22 3 Fa 1/3 2SwitchID VLAN VLAN_Name1 2 RB Pov2 2 TestI want to have a result like:SwitchID PortID PortName VLAN VLAN_NameThanks for helping out !! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-06 : 23:44:16
|
| [code]SELECT p.SwitchID, p.PortID, p.PortName, v.VLAN, v.VLAN_NameFROM Ports pINNER JOIN VLANs vON v.SwitchID=p.SwitchIDAND v.VLAN=p.VLAN [/code] |
 |
|
|
|
|
|