Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

comma-delimited foreign key help!

Hi folks:

I have a Profiles table that, among other things, contains all the groups a member can be a part of.  I have a field called [Member Groups] that contains a comma-seperated value of all the foreign keys from the table [Member Group List].  So, in the field, I have this:

Member Groups
------------------
4, 5, 6, 7

But what I really want is something like this to return:

Member Groups
------------------
Baseball Club, Basketball Club, Hockey Club, Football Club

I know already that this is not good database practice, but I'm working on existing design restrictions.  Does anybody know of a stored procedure, or a query, or some way of getting from where I am to where I want to be?  Thanks in advance,

Dan
0
dwaldner
Asked:
dwaldner
  • 9
  • 6
1 Solution
 
bukkoCommented:
You could write a function to do it.
0
 
bukkoCommented:
If dynamic SQL is ok, the query could be something like:

DECLARE @query varchar(32)
SET @query = 'SELECT * FROM memberGroups WHERE id IN (' + @member_groups + ')'
EXEC( @query )

Then build the return string from the results.

0
 
dwaldnerAuthor Commented:
@bukko,

That last bit I think is exactly what I need, however:

I'm terrible with writing stored procedures...how do you modify your code to do what I need.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
bukkoCommented:
I have assumed the column Name in [Member Group List] table.

CREATE PROCEDURE dbo.ids_to_names (
    @id_list    varchar(32),
    @results    varchar(1024) OUTPUT
)
AS
    DECLARE @query varchar(256), @name VARCHAR(32)
    SET @query = 'SELECT [member Group List].Name INTO #temp FROM [Member Group list] WHERE id IN (' + @member_groups + ')'
    EXEC( @query )
    DECLARE temp_cursor CURSOR FOR
    SELECT #temp.name FROM #temp
    OPEN temp_cursor
    FETCH NEXT FROM temp_cursor INTO @name
    WHILE @@FETCH_STATUS = 0
        BEGIN
        IF @results = ''
            SET @results = @name
        ELSE
            SET @results = @result + ',' + @name
        FETCH NEXT FROM temp_cursor INTO @name
        END
    CLOSE temp_cursor
    DEALLOCATE temp_cursor
GO

Call this stored proc as follows:
DECLARE @results varchar(1024)
EXEC dbo.ids_to_names @id_list = '1,2,3,4,5', @results OUTPUT
SELECT @results

You may need to change some of the object names, etc.
Plus there is probably a better way to do this.

Regards

bukko
0
 
bukkoCommented:
Whoops - forgot to drop the temp table.

CREATE PROCEDURE dbo.ids_to_names (
    @id_list    varchar(32),
    @results    varchar(1024) OUTPUT
)
AS
    DECLARE @query varchar(256), @name VARCHAR(32)
    SET @query = 'SELECT [member Group List].Name INTO #temp FROM [Member Group list] WHERE id IN (' + @member_groups + ')'
    EXEC( @query )
    DECLARE temp_cursor CURSOR FOR
    SELECT #temp.name FROM #temp
    OPEN temp_cursor
    FETCH NEXT FROM temp_cursor INTO @name
    WHILE @@FETCH_STATUS = 0
        BEGIN
        IF @results = ''
            SET @results = @name
        ELSE
            SET @results = @result + ',' + @name
        FETCH NEXT FROM temp_cursor INTO @name
        END
    CLOSE temp_cursor
    DEALLOCATE temp_cursor
    DROP TABLE #temp
GO
0
 
dwaldnerAuthor Commented:
I'm getting the following error:

The stored procedure completed successfully, but did not return any records.

Any hints?
0
 
bukkoCommented:
Did you pass a valid id list?
0
 
dwaldnerAuthor Commented:
Yup...double checked and still no results...
0
 
bukkoCommented:
Don't forget, call this stored proc as follows:

DECLARE @results varchar(1024)
EXEC dbo.ids_to_names @id_list = '1,2,3,4,5', @results OUTPUT
SELECT @results
0
 
bukkoCommented:
Ok, so I left an incorrect variable name in there.
Try this version:

CREATE PROCEDURE dbo.ids_to_names (
    @id_list    varchar(32),
    @results    varchar(1024) OUTPUT
)
AS
    DECLARE @query varchar(256), @name VARCHAR(32)
    SET @query = 'SELECT [member Group List].Name INTO #temp FROM [Member Group list] WHERE id IN (' + @id_list + ')'
    EXEC( @query )
    DECLARE temp_cursor CURSOR FOR
    SELECT #temp.name FROM #temp
    OPEN temp_cursor
    FETCH NEXT FROM temp_cursor INTO @name
    WHILE @@FETCH_STATUS = 0
        BEGIN
        IF @results = ''
            SET @results = @name
        ELSE
            SET @results = @result + ',' + @name
        FETCH NEXT FROM temp_cursor INTO @name
        END
    CLOSE temp_cursor
    DEALLOCATE temp_cursor
    DROP TABLE #temp
GO
0
 
bukkoCommented:
You can test the logic by running just this bit in query analyser: (with a valid id list of course)

    DECLARE @query varchar(256)
    SET @query = 'SELECT [member Group List].Name FROM [Member Group list] WHERE id IN (1,2,3,4)'
    EXEC( @query )

That should return the actual records.
0
 
dwaldnerAuthor Commented:
Yes, its returning the actual records, so now I'll try adjusting the code as per your suggestion....
0
 
dwaldnerAuthor Commented:
Almost there, bukko....

Message I get when I execute the SP as per your instructions:

Server: Msg 208, Level 16, State 1, Procedure ids_to_names, Line 1
Invalid object name '#temp'.

It appears to have a problem with the temp table...any thoughts?
0
 
bukkoCommented:
I think I know why.
Because the temp table is being created in the dynamic SQL string, it's probably out of scope when referenced by the rest of the proc.

You could name the table ##temp instead, but this assumes that no-one else will be running this proc or you will get concurency issues.

A much better alternative would be to create the table explicitly before the EXEC('SELECT... etc.
The dynamic SQL should still be able to reference the table as it is within scope.

This will be something like:
CREATE TABLE #temp (
    id int,
    name varchar(32)
)
What does the source table look like exactly?

bukko
0
 
dwaldnerAuthor Commented:
That's okay bukko, I did as you suggested and it worked just fine...

I don't think we'll be getting concurrency issues; the chances of that happening on our servers are VERY small.

Thanks for all your help

Dan
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now