Hi allBackground: I have a function that splits a string looking like this 'a,b;c,d;e,f' into a two colom tablecol1 col2a -|-bc -|-de -|-fand it works fine.Heres the code--Splits a string into parts at the ';' and then whatever was between the ';' and between commas gets split into a tableCREATE FUNCTION fn_SplitTwoDelimB (@string varchar(8000))RETURNS @ReturnTable TABLE(col1 Varchar(255), col2 Varchar(255))ASBEGINDECLARE @value varchar(8000), @maxidx smallintDECLARE @a table (idx smallint, value varchar(8000))Insert @a (idx, value)SELECT idx, value FROM dbo.fn_SplitOneDelim(@string,';')SELECT @maxidx = Max(idx) FROM @aDeclare @count int, @col1 Varchar(255), @col2 Varchar(255) Set @count = 0WHILE @maxidx >= 0 BEGIN SELECT @value = value FROM @a WHERE idx = @maxidx INSERT INTO @ReturnTable(col1,col2) SELECT col1 = Max(CASE WHEN idx = 0 THEN Convert(Varchar(255), value) END), col2 = Max(CASE WHEN idx = 1 THEN Convert(Varchar(255), value) END) FROM dbo.fn_SplitOneDelim(@value,',') SET @maxidx = @maxidx - 1 SET @count = @count + 1 END RETURNEND
I then changed this Function so I can add a third colom which will be used as a number for each record so that the table will then look like thiscol0 col1 col20 -|-a -|-b1 -|-c -|-d2 -|-e -|-f ectThe code to do this follows and the red part shows how I changed it--Splits a string into parts at the ';' and then whatever was between the ';' and the two values around the commas gets split into a tableCREATE FUNCTION fn_SplitTwoDelim (@string varchar(8000))RETURNS @ReturnTable TABLE(col0 int, col1 Varchar(255), col2 Varchar(255))ASBEGINDECLARE @value varchar(8000), @maxidx smallintDECLARE @a table (idx smallint, value varchar(8000))Insert @a (idx, value)SELECT idx, value FROM dbo.fn_SplitOneDelim(@string,';')SELECT @maxidx = Max(idx) FROM @aDeclare @count int, @col1 Varchar(255), @col2 Varchar(255) Set @count = 0WHILE @maxidx >= 0 BEGIN SELECT @value = value FROM @a WHERE idx = @maxidx SELECT @col1 = Max(CASE WHEN idx = 0 THEN Convert(Varchar(255), value) END), @col2 = Max(CASE WHEN idx = 1 THEN Convert(Varchar(255), value) END) FROM dbo.fn_SplitOneDelim(@value,',') INSERT INTO @ReturnTable(col0, col1,col2) VALUES(@count,@col1,@col2) SET @maxidx = @maxidx - 1 SET @count = @count + 1 END RETURNEND
The Error - Insert Error: Column name or number of supplied values does not match table definition. If you could please tell me why im getting this error and mayb have a solution it will help alot?Here follows the other function that this function calls--Splits a string at a given deliminatorCREATE FUNCTION fn_SplitOneDelim(@sText varchar(8000), @sDelim varchar(20) = ' ')RETURNS @retArray TABLE (idx smallint Primary Key, value varchar(8000))ASBEGINDECLARE @idx smallint, @value varchar(8000), @bcontinue bit, @iStrike smallint, @iDelimlength tinyintIF @sDelim = 'Space' BEGIN SET @sDelim = ' ' ENDSET @idx = 0SET @sText = LTrim(RTrim(@sText))SET @iDelimlength = DATALENGTH(@sDelim)SET @bcontinue = 1IF NOT ((@iDelimlength = 0) or (@sDelim = 'Empty')) BEGIN WHILE @bcontinue = 1 BEGIN--If you can find the delimiter in the text, retrieve the first element and--insert it with its index into the return table. IF CHARINDEX(@sDelim, @sText)>0 BEGIN SET @value = SUBSTRING(@sText,1, CHARINDEX(@sDelim,@sText)-1) BEGIN INSERT @retArray (idx, value) VALUES (@idx, @value) END --Trim the element and its delimiter from the front of the string. --Increment the index and loop.SET @iStrike = DATALENGTH(@value) + @iDelimlength SET @idx = @idx + 1 SET @sText = LTrim(Right(@sText,DATALENGTH(@sText) - @iStrike)) END ELSE BEGIN--If you can’t find the delimiter in the text, @sText is the last value in--@retArray. SET @value = @sText BEGIN INSERT @retArray (idx, value) VALUES (@idx, @value) END --Exit the WHILE loop.SET @bcontinue = 0 END END ENDELSE BEGIN WHILE @bcontinue=1 BEGIN --If the delimiter is an empty string, check for remaining text --instead of a delimiter. Insert the first character into the --retArray table. Trim the character from the front of the string. --Increment the index and loop. IF DATALENGTH(@sText)>1 BEGIN SET @value = SUBSTRING(@sText,1,1) BEGIN INSERT @retArray (idx, value) VALUES (@idx, @value) END SET @idx = @idx+1 SET @sText = SUBSTRING(@sText,2,DATALENGTH(@sText)-1) END ELSE BEGIN --One character remains. --Insert the character, and exit the WHILE loop. INSERT @retArray (idx, value) VALUES (@idx, @sText) SET @bcontinue = 0 END ENDENDRETURNEND
And if you want to test it here is some example queries.Declare @temTable Table (colName Varchar(255), value Varchar(255)) --Declare @temTable Table (idx smallint Primary Key, value Varchar(8000))Insert @temTable(colName,value) SELECT * FROM fn_SplitTwoDelim('tel,12345;home,23451;work,34512')--Insert @temTable(idx,value) SELECT * FROM fn_SplitOneDelim('a,b,c,d',',')SELECT * FROM @temTableSELECT count(*) FROM @temTableYour help will be greatly appriciated!