Solved

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

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

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.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

697 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