Question : SQL SPLIT Function Clarification

Hello Experts,
 
A previous problem that I had was creating a SPLIT table-valued function that took, basically, a record from a table called tblExtract which had tabs as the deliniator character.  With assistance I was able to come up with a solution that would break up one row at a time into the respective fields and place it in a Table which was returned.  I have uploaded the code for your review.  Most of the fields were commented out to test the function.

This code appears to work, but seams stuck.  The table I pull in from now has only one row for testing purposes.  When I run this code in a stand-a-loan query I get an error indicating that I have called the function incorrectly.  At that point I pass a string directly in the call.

What my end result is going to be is a @tmpTable that has each field that I can access.  I want to write these fields to a new table called NewExtract.  There are a number of fields in the @tmpTable, but right now I commented out all, but a few as you can see from the function code.  This code should be relatively fast since there is only one call per row to the SPLIT function.

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
Table-valued Function:
USE [ICMTS-ACTables]
GO
/****** Object: UserDefinedFunction [dbo].[fncBuildTableRecord] Script Date: 11/11/2009 15:32:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
-- =============================================
-- Author: Peter J. Allen
-- Create date: 11/09/2009 11/11/2009
-- Description: Split function that breaks up a passed row from tblExtract
-- to the temporary table to be returned to the calling
-- Stored Procedure
-- =============================================
 
ALTER FUNCTION [dbo].[fncBuildTableRecord]
(
@String varchar(1000),
@Deliniator char(1)
)
 
RETURNS @tmpTable TABLE
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Asset_UniqueID] [nchar](20) NOT NULL,
[Asset_SPIFCode] [nchar](4) NULL
--[Asset_Classification] [nchar](1) NOT NULL,
--[Asset_Assignment] [nchar](7) NOT NULL,
--[Asset_BuildingCode] [nchar](8) NULL,
--[Asset_Location] [nchar](15) NULL,
--[Asset_OrganizationalCode] [nchar](16) NULL,
--[Asset_Barcode] [nchar](10) NOT NULL,
--[Asset_SerialNumber] [nchar](30) NOT NULL,
--[Asset_InventoryType] [nchar](5) NULL,
--[Asset_Manufacturer] [nchar](40) NOT NULL,
--[Asset_ProductName] [nchar](80) NOT NULL,
--[Asset_Category] [nchar](32) NOT NULL,
--[Asset_User] [nchar](10) NULL,
--[Asset_SystemName] [nchar](26) NULL,
--[Asset_ContactName] [nchar](40) NULL,
--[Asset_CostCenter] [nchar](20) NULL,
--[Asset_DateReceived] [nchar](20) NULL,
--[Asset_DateInventoried] [nchar](30) NULL,
--[Asset_DateBarcodeScan] [nchar](50) NULL,
--[Asset_DateTivoliScan] [nchar](50) NULL,
--[Asset_UserFirstName] [nchar](26) NULL,
--[Asset_UserLastName] [nchar](26) NULL,
--[Asset_UserSEID] [nchar](10) NULL,
--[Asset_UserPhoneNumber] [nchar](14) NULL,
--[Asset_UnitPrice] [nchar](30) NULL,
--[Asset_DisposalCode] [nchar](2) NULL,
--[Asset_ConditionCode] [nchar](2) NULL,
--[Asset_IRSReportNumber] [nchar](20) NULL,
--[Asset_DateRetired] [nchar](50) NULL,
--[Asset_DisposalType] [nchar](10) NULL,
--[Asset_DateDisposal] [nchar](50) NULL,
--[Asset_DisposalReportNumber] [nchar](20) NULL,
--[Asset_CPUType] [nchar](10) NULL,
--[Asset_CPUSpeed] [nchar](6) NULL,
--[Asset_SystemDiskSize] [nchar](10) NULL,
--[Asset_SystemMemory] [nchar](10) NULL,
--[Asset_ComputerName] [nchar](20) NULL,
--[Asset_DeviceID] [nchar](20) NULL,
--[Asset_LANMACAddress] [nchar](30) NULL,
--[Asset_OperatingSystem] [nchar](40) NULL,
--[Asset_OSVersion] [nchar](10) NULL
)
 
AS
BEGIN
DECLARE @Index int
DECLARE @Pointer int
DECLARE @FieldCount int
DECLARE @strLen int
DECLARE @PosStart int
DECLARE @PosCopy int
DECLARE @PosEnd int
DECLARE @LoopChar char(5)
DECLARE @Field varchar(100)
 
SET @Index = 0
SET @LoopChar = 'TRUE'
SET @FieldCount = 0
 
WHILE @LoopChar = 'TRUE'
BEGIN
SET @Index = @Index + 1
SET @Pointer = CHARINDEX(@Deliniator, @String, @Index)
IF @Pointer > 0
 
BEGIN
SET @PosStart = @Index
SET @strLen = @Pointer - @PosStart + 1
SET @PosCopy = @PosStart
SET @Field = SUBSTRING(@String, @PosStart, @strLen)
END
 
SET @Index = @Pointer
IF @Pointer > 0 SET @FieldCount = @FieldCount + 1
IF @Pointer = 0 AND @Index < LEN(@String)
 
BEGIN
SET @PosStart = @PosCopy + @strLen
SET @strLen = LEN(@String) - @PosStart + 1
SET @FieldCount = @FieldCount + 1
SET @LoopChar = 'FALSE'
SET @Field = SUBSTRING(@String, @PosStart, @strLen)
END
 
IF @FieldCount = 2
 
BEGIN
INSERT @tmpTable (Asset_UniqueID)
VALUES(@Field)
END
END
 
RETURN
END
 
Stored Procedure:
USE [ICMTS-ACTables]
 
GO
 
/****** Object: StoredProcedure [dbo].[NewExtract] Script Date: 11/12/2009 05:29:51 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
-- =============================================
-- Author: Peter J. Allen
-- Create date: 11/09/2009 11/11/2009
-- Description: Call SPLIT Function to break up tblExtract rows into the
-- corresponding fields to be returned in a table.
-- =============================================
 
ALTER PROCEDURE [dbo].[NewExtract] 
AS
 
DECLARE @iValue int
DECLARE @Counter int
DECLARE @String varchar(1000)
 
WHILE @@RowCount < 11
BEGIN
SELECT @String = tbl.TableLine
FROM [ICMTS-ACTables].[dbo].[tblExtract] tbl
JOIN dbo.fncBuildTableRecords(@String, char(9)) ntbl
ON tbl.ID = ntbl.ID
 
END

Answer : SQL SPLIT Function Clarification

run below given full script.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
CREATE FUNCTION [dbo].[SplitData] 
( 
@RowData nvarchar(2000), 
@SplitOn nvarchar(5) 
) 
RETURNS @RtnValue table 
( 
Id int identity(1,1), 
Data nvarchar(100) 
) 
AS 
BEGIN 
Declare @Cnt int 
Set @Cnt = 1 
While (Charindex(@SplitOn,@RowData)>0) 
Begin 
Insert Into @RtnValue (data) 
Select 
Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1))) 
Set @RowData = Substring(@RowData,Charindex(@SplitOn,@RowData)+1,len(@RowData)) 
Set @Cnt = @Cnt + 1 
End 
Insert Into @RtnValue (data) 
Select Data = ltrim(rtrim(@RowData)) 
Return 
END
GO 
CREATE TABLE EMPSplitTest
(
FullName varchar(50),
firstname varchar(20)
)
GO 
insert into EMPSplitTest
select 'ritesh shah','ritesh' union all
select 'rajan jain','rajan'
GO 
--split
select * from EMPSplitTest e cross apply dbo.splitData(e.FullName,' ') as f
where e.firstname=f.data
Random Solutions  
 
programming4us programming4us