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
 Not sure how to get info from other table

Author  Topic 

rett
Starting Member

35 Posts

Posted - 2005-08-31 : 14:30:29
I am having problems with my store procedure displaying my last column name which is "DISPLAY_CATEGORY_DESC". I am trying to display the description from another table. But in order to get to the description I have to go through 2 tables. The tables look like something like this.

First TableTABLE_CATEGORY

PRIMARY_KEY = DIR_CATEGORY_ID = 5000

Column Name
DIR_CATEGORY_LVL_ID1 = 100
DIR_CATEGORY_LVL_ID2 = 200
DIR_CATEGORY_LVL_ID3 = 300

Second TableTABLE_CATEGORY_DESCRIPTION
PRIMARY KEY DIR_CATEGORY_LVL_ID1 = 100

Column Name
DESCRIPTION = "Car Model Company"

This is my DISPLAY_CATEGORY_DESC

Not sure how to get to this description. Thanks for your help.


select cwebs.SESSION_ID, webs.ENTERED_DATE, cwebs.DIR_CATEGORY_ID, DISPLAY_CATEGORY_DESC

FROM di_webclickstats cwebs
left join di_websessions webs on cwebs.session_id = webs.session_id
where cwebs.DIR_CATEGORY_ID <> 0 and webs.ENTERED_DATE between @startDate and @endDate+1

SamC
White Water Yakist

3467 Posts

Posted - 2005-08-31 : 14:40:47
[quote]Originally posted by rett

I am having problems with my store procedure displaying my last column name which is "DISPLAY_CATEGORY_DESC". I am trying to do a select that get the primary key from another table and use that key to do another select to get the description.

Not sure if I am doing this the correct way. Thanks for your help.


DECLARE @startDate datetime
DECLARE @endDate datetime
DECLARE @cat_lvl1_id varchar(10)
DECLARE @lvl1_desc varchar(250)
DECLARE @get_desc varchar(250)

select d.ENTERED_DATE, d.ADV_NAME_ID, d.COUNT_NAME_ID,
select @cat_lvl1_id = (select category_lvl1_id from di_categories where (dir_category_id = get_category_id))

set @get_desc = ''

IF @cat_lvl1_id = ''
BEGIN
SET @lvl1_desc = ''
END
ELSE
BEGIN
SET @lvl1_desc = (SELECT TOP 1 CATEGORY_DESCRIPTION FROM DI_CATEGORYDESCS WHERE (CATEGORY_LVL_ID = @cat_lvl1_id))
set @get_desc = @lvl1_desc
END

select @get_desc as DISPLAY_CATEGORY_DESC -- Return a single column

from ( -- Subquery returns 3 colums, none of which are used in the outer select...
select cwebs.SESSION_ID, webs.ENTERED_DATE, DIR_CATEGORY_ID as GET_CATEGORY_ID,1 as COUNT_AME_ID
FROM di_webclickstats cwebs
inner join di_websessions webs on cwebs.session_id = webs.session_id) d
where d.ENTERED_DATE between @startDate and @endDate+1


Looks like the query above has some problems. Sounds like you're trying to dynamically select the name of the 3rd column. Not possible without resorting to Dynamic SQL.
Go to Top of Page

rett
Starting Member

35 Posts

Posted - 2005-08-31 : 14:49:50
Thanks SamC for a quick response.


Do you have any example or way to be able to get my DISPLAY_CATEGORY _DESC. I have another store procedure that can return me the description but I am not sure how to execute another store procedure within a store procedure.
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-09-01 : 00:03:54
quote:
Originally posted by rett

Do you have any example or way to be able to get my DISPLAY_CATEGORY _DESC. I have another store procedure that can return me the description but I am not sure how to execute another store procedure within a store procedure.

You can execute a stored proc like this:

EXEC dbo.MyStoredProc @Parm1=123,@Parm2=@AnotherValue, etc

But dynamically naming a column will take dynamic SQL, something to use only as a last resort...

SET @MySQL = 'SELECT Col1, Col2, Col3 As ' + @MyColumnName +
'FROM MyTable'

EXEC @MySQL

Go to Top of Page
   

- Advertisement -