?
Solved

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

Posted on 2011-02-17
2
Medium Priority
?
544 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

777 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