Using a Table Valued Function in a JOIN

During my presentation on "Avoiding Database Entropy" I did a demo of a function to parse a delimited string and return it as a table.  This is extremely helpful when using multi-select controls in your applications.  In the end we still call the stored procedure like this:

GetUsersByID '1,2,7,9'

Most people make their stored procedure like this:

create proc GetUsersByID (@delimitedIDs varchar(200)) as declare @SQL varchar(2000) set @SQL = 'SELECT Employee.EmployeeID, Employee.FirstName, Employee.LastName, Employee.MiddleName FROM Employee WHERE EmployeeID in (' + @delimitedIDs + ')' sp_executesql (@SQL)

Notice all that "RED SQL" -- left over from the Cold War I'm sure.

With a little creativity, we can improve performance, maintenance and clarity by doing it like this:

create proc GetUsersByID (@delimitedIDs varchar(200)) as SELECT Employee.EmployeeID, Employee.FirstName, Employee.LastName, Employee.MiddleName FROM Employee INNER JOIN dbo.ParamParserFn(@delimitedIDs, ',') tblID ON Employee.EmployeeID = tblID.Id

Notice the subtle difference?  There's no dynamic SQL.  It's all good!  It will perform better too.

So here is the parsing function (note: be sure you qualify it correctly when you go to use it--SQL will like you better...wink, wink):

CREATE FUNCTION ParamParserFn( @delimString varchar(255), @delim char(1)) RETURNS @paramtable TABLE ( Id int ) AS BEGIN

DECLARE @len int, @index int, @nextindex int

SET @len = DATALENGTH(@delimString) SET @index = 0 SET @nextindex = 0

WHILE (@len > @index ) BEGIN

SET @nextindex = CHARINDEX(@delim, @delimString, @index)

if (@nextindex = 0 ) SET @nextindex = @len + 2

INSERT @paramtable SELECT SUBSTRING( @delimString, @index, @nextindex - @index )

SET @index = @nextindex + 1

END RETURN END GO

Your mileage may vary as you may need strings delimited and not just integers, but this should get you going down the right path.  I don't claim this as my own code, but I've been using this method for so many years, I can't remember where I first learned the idea.

This article is part of the GWB Archives. Original Author: Brian Sherwin

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.