CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #6
    Join Date
    Jul 2005
    Posts
    1,083

    Re: delimited string as join link?

    I haven't experience with oracle's sql queries, but study the next sp and modify to apply to your case
    Code:
    CREATE PROC dbo.GetOrderList2
    (
    	@OrderList varchar(500)
    )
    AS
    BEGIN
    	SET NOCOUNT ON
    
    	CREATE TABLE #TempList
    	(
    		OrderID int
    	)
    
    	DECLARE @OrderID varchar(10), @Pos int
    
    	SET @OrderList = LTRIM(RTRIM(@OrderList))+ ','
    	SET @Pos = CHARINDEX(',', @OrderList, 1)
    
    	IF REPLACE(@OrderList, ',', '') <> ''
    	BEGIN
    		WHILE @Pos > 0
    		BEGIN
    			SET @OrderID = LTRIM(RTRIM(LEFT(@OrderList, @Pos - 1)))
    			IF @OrderID <> ''
    			BEGIN
    				INSERT INTO #TempList (OrderID) VALUES (CAST(@OrderID AS int)) --Use Appropriate conversion
    			END
    			SET @OrderList = RIGHT(@OrderList, LEN(@OrderList) - @Pos)
    			SET @Pos = CHARINDEX(',', @OrderList, 1)
    
    		END
    	END	
    
    	SELECT o.OrderID, CustomerID, EmployeeID, OrderDate
    	FROM 	dbo.Orders AS o
    		JOIN 
    		#TempList t
    		ON o.OrderID = t.OrderID
    		
    END
    GO
    The above stored procedure receives a list of OrderIDs separated by commas, as an input parameter.
    It then parses the parameter, extracts individual OrderIDs from the comma separated list, inserts the OrderIDs into a temporary table, and then joins the temporary table with the main Orders table, to get the requested results.

    (Found in my sql notebook that says it belongs to Narayana Vyas Kondreddi)

    JG
    Last edited by jggtz; August 6th, 2011 at 06:40 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured