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 |
|
cepeterson
Starting Member
3 Posts |
Posted - 2007-05-31 : 10:35:27
|
| First, I appreciate any guidance any of you can provide on this.I have three tables, one a list of device types, one a list of devices, and one a relational table of device_devicetypes. What I'm trying to accomplish is a single SQL stored procedure that will take in a deviceID, select *all* device types, and on device types that are in the recordset of selecting deviceTypeIDs from device_deviceTypes where the DeviceID equals the parameter, add a column to the output such as "SELECTED" as a bit type. In other words, if I have 5 device types and I call this stored procedure via something like:EXEC getAllDeviceTypesWithSelected 33 (where 5 is a device ID)And get an output looking something likeDeviceTypeName DeviceTypeID SELECTEDDeviceType 1 1 FalseDeviceType 2 2 FalseDeviceType 3 3 TrueDeviceType 4 4 TrueDeviceType 5 5 TrueDoes that make sense to anybody or am I in dire need of additional (or less) caffeine this morning? |
|
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2007-05-31 : 11:06:28
|
Assuming that the PK of the intersection table is a composite of DeviceTypeID and DeviceIDDECLARE @DeviceIDSET @DeviceID = 5SELECT dt.DeviceTypeName, dt.DeviceTypeID, CASE WHEN dtd.DeviceTypeID IS NULL THEN 'False' ELSE 'True' END AS SelectedFROM dbo.DeviceTypes AS dtLEFT JOIN dbo.Device_DeviceTypes AS dtdON dtd.DeviceTypeID = dt.DeviceTypeID AND dtd.DeviceID = @DeviceID Mark |
 |
|
|
cepeterson
Starting Member
3 Posts |
Posted - 2007-05-31 : 11:36:53
|
| mwjdavidson -I'm definitely feeling closer now, but I get:Msg 208, Level 16, State 1, Procedure test, Line 6Invalid object name 'dbo.DeviceType'.Msg 208, Level 16, State 1, Procedure test, Line 6Invalid object name 'dbo.Device_DeviceType'.As a response to EXEC test 14Where test is a stored procedure defined as:PROCEDURE [Medic].[test] @DeviceID varchar(100) = 0ASBEGINSELECT dt.DeviceTypeName, dt.DeviceTypeID, CASE WHEN dtd.DeviceTypeID IS NULL THEN 'False' ELSE 'True' END AS SelectedFROM dbo.DeviceType AS dtLEFT JOIN dbo.Device_DeviceType AS dtdON dtd.DeviceTypeID = dt.DeviceTypeID AND dtd.DeviceID = @DeviceIDEND |
 |
|
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2007-05-31 : 12:11:05
|
| Check that the names of your tables correspond with those in the query. Check also that they are owned by dbo (I was making assumptions!) I'm a bit concerned by the definition of @DeviceID as a VARCHAR(100)!Mark |
 |
|
|
cepeterson
Starting Member
3 Posts |
Posted - 2007-05-31 : 12:40:57
|
| Wooohoooo You're a genius... Got it with:[Medic].[test] @DeviceID int = 0ASBEGINSELECT dt.Name, dt.ID, CASE WHEN dtd.DeviceTypeID IS NULL THEN 'False' ELSE 'True' END AS SelectedFROM Medic.DeviceType AS dtLEFT JOIN Medic.Device_DeviceType AS dtdON dtd.DeviceTypeID = dt.ID AND dtd.DeviceID = @DeviceIDEND |
 |
|
|
mwjdavidson
Aged Yak Warrior
735 Posts |
Posted - 2007-06-01 : 08:56:13
|
quote: Wooohoooo You're a genius...
I'm blushing Glad I could help.Mark |
 |
|
|
|
|
|
|
|