Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split Function 1

Status
Not open for further replies.

Naoise

Programmer
Dec 23, 2004
318
IE
I have a table tblA

tblA
===================
fldA | fldB | fldC


and a stored procedure spAddData which receives a list as follows 1,abc,100¬1,def,101¬1,ghi,103

Basically the row delimiter is '¬' and field delimiter is ','

How could I best go about inserting a delimited string like this into tblA? No Split UDF function I have seen on the web has been helpful yet. Thanks in advance for replies.
 
What follows pertains to Microsoft SQL Server.

The problem is that SPLIT is a table valued function (because it returns multiple records and/or fields) vs. a scalar valued function which returns just a single value (eventhough that value can be any data type). The problem with table valued functions is that you cannot apply them to multiple records with a single statement. Usually, this requires the use of a cursor or a loop.

Do you have the ability to modify how the data is sent in? If you sent XML data to the stored procedure instead, this functionality would be a lot easier. Let me show you how.

First, the stored procedure:
Code:
Alter Procedure TestXMLInput
	@XML Text
As
SET NOCOUNT ON

Declare @iDoc Integer
Exec sp_xml_preparedocument @iDoc OUTPUT, @XML

[green]-- uncomment the next 2 lines to insert into table
--Insert
--Into    tblA(fldA, fldB, fldC)[/green]
Select	fldA, fldB, fldC
From	OpenXML(@iDoc, '//root/data', 1)
With	(
        fldA Int,
        fldB Varchar(10),
        fldC int
        )

You can test this function, like so...

Code:
TestXMLInput 
[red]'<root>
  <data fldA="1" fldB="abc" fldC="100"/>
  <data fldA="1" fldB="def" fldC="101"/>
  <data fldA="1" fldB="ghi" fldC="103"/>
</root>'[/red]

The XML does not require any formatting (carriage returns or extra spaces). I did it this way to make it easier to see how the data was put together.

I hope this helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Has this helped?

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I couldn't fit it into my solution currently but I have saved it for future reference, so in that respect it has helped. Thanks again.
 
Do you have a solution to your problem, or are you still looking for advice?

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I orchestrated a different solution in the business layer rather than the data access layer so all is good. Thanks for following up though :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top