Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to return integers with "," delimiter

Status
Not open for further replies.

KatherineF

Technical User
Mar 3, 2003
124
US
I have this function. The result of this fucntion I should insert into IN Statement ( Example: PartnerID IN (dbo.Function_Name(1)) ) Now IN statement looks like this IN ('1,2'), but I need IN (1,2)

ALTER FUNCTION dbo.Function_Name (@PartnerID INT)
RETURNS VARCHAR(8000) AS
BEGIN

DECLARE
@ParentID AS INT,
@ChildID AS INT,
@ParentName AS VARCHAR(50),
@LEVEL AS VARCHAR(8000),
@RootPartnerID INT


SET @ChildID = @PartnerID
SET @Level = ''

SET @RootPartnerID = 1111

WHILE @ChildID <> @RootPartnerID
BEGIN
SELECT @ParentID = PartnerID_Parent, @ParentName = PartnerName_Parent FROM dbo.Partner_Relationship_Info_V WHERE PartnerID = @ChildID

SET @LEVEL = CAST(@ParentID AS VARCHAR(50)) + ',' + @LEVEL

SET @ChildID = @ParentID
END

IF RIGHT(@LEVEL,1)= ',' SET @LEVEL=LEFT(@LEVEL,LEN(@LEVEL)-1)


EndOfFunction:
RETURN (@LEVEL)
END

Thank you!
 
Have your function return a table of PartnerID_Parent. Then instead of writing a PartnerID IN do an inner join with the table that the function returns.
-Karl
 
Actually, I think you'll find that either solution is a performance nightmare on anything but small dbs, but good luck.
-Karl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top