Solved

Repeat Primary Table field for every record returned by Table Valued function

Posted on 2011-02-17
2
539 Views
Last Modified: 2012-08-13
Hi
I have the following parts data in a table in SQL Server
OEM_PartNumber      Device_List
001R00588                            | WCP7132 | WorkCentre 7132 | WCP7132 | WorkCentre 7132 |
001R00593                            | WorkCentre 7232 | WorkCentre 7242 | WorkCentre 7232 |

I have a split string function and if I pass in the string from the Device List field above
select * from dbo.fn_SplitString('|', '| WCP7132 | WorkCentre 7132 | WCP7132 | WorkCentre 7132')
I get the records returned as follows

1       WCP7132
2       Xerox WorkCentre 7132
3       Xerox WCP7132
4       WorkCentre 7132

i.e a record for every string delimited by the |

using this function(maybe I need to modify the function) or some other trick(maybe CTE) I want to achive the the data in the following format for every record in the parts table

001R00588        WCP7132
001R00588        WorkCentre 7132
001R00588       WCP7132
001R00588       WorkCentre 7132
001R00593        WorkCentre  7232
001R00593        WorkCentre 7232
001R00593        WorkCentre 7232P
001R00593        WorkCentre 7242
001R00593        WorkCentre Pro 7232
001R00593       WorkCentre 7232

so I get a record for each string delimited by the pipe in the parts table, for every record in the parts table and the relevant part number beside it

This is the code for the split string function
ALTER FUNCTION [dbo].[fn_SplitString](@sep char(1), @s varchar(8000))
RETURNS TABLE
AS
RETURN
(
      WITH Pieces(pn, start, stop)
      AS (      
      SELECT 1, 1, CHARINDEX(@sep, @s)      
      UNION ALL      
      SELECT pn + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)      
      FROM Pieces      
      WHERE stop > 0    
      )    
      SELECT pn,      
      SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 8000 END) AS s    
      FROM Pieces
)
0
Comment
Question by:Barry Cunney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34918178
CROSS APPLY:
select t.OEM_PartNumber , f.s Device     
  from parts_table t
  cross apply dbo.fn_SplitString('|',t.device_list ) f

Open in new window

0
 
LVL 17

Author Comment

by:Barry Cunney
ID: 34920324
Thanks Angel - I will give this a shot when back in the office tomorrow, and get back to you then -
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question