Solved

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

Posted on 2011-02-17
2
528 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
2 Comments
 
LVL 142

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now