Solved

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

Posted on 2011-02-17
2
530 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

911 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

20 Experts available now in Live!

Get 1:1 Help Now