How to pass a collection of data to SQL Server

Hi all. If you are faced with a situation where you wish to send an array of strings(say) to the Server, then you have stumbled to the correct page :-)
To start off with, SQL server up to 2005 does not support Arrays. So, it becomes a bit difficult for Java,C#,C++ programmers who are very fond of arrays to get their job done.

Fortunately there is a detour which enables us to get the job done.
Instead of sending the list as an array, we can append a delimiter(here I used the comma) and send the complete string to the server side.
At the server side, it splits the separate strings by the delimiter and inserts them into a table(her I used it as a single column table of the form
tmpTable(_name varchar(50)))
You can use the values inserted into this table and use the results similar to having an array.

NOTE: It does not support the sophisticated array functions you are used to, but it will let you get the job done.

/*
PROCEDURE NAME : readCSV
AUTHOR : Ruchira Randana
PROCEDURE DESCRIPTION : Used to seperate a Comma Seperated List and input the values to a table
INPUT : Comma Seperated string
OUTPUT : Seperated values as the comma for the delimitter
*/
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------

create procedure readCSV

@CSValueString varchar(2500)

as begin

declare @index int
declare @length int
declare @nextCommaIndex int
declare @value varchar(50)

----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------

set @length=len(@CSValueString)
set @nextCommaIndex=charindex(',',@CSValueString,1)

----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------

while((@nextCommaIndex>0) or (len(@CSValueString)>0))
begin

if(len(@CSValueString)>0 and @nextCommaIndex=0)
begin
set @value=ltrim(rtrim(@CSValueString))
set @nextCommaIndex=-1
set @CSValueString=''
end
else
begin
set @value=substring(@CSValueString,1,@nextCommaIndex-1)
set @value=ltrim(rtrim(@value))
set @CSValueString=substring(@CSValueString,@nextCommaIndex+1,len(@CSValueString))
set @nextCommaIndex=charindex(',',@CSValueString,1)
end

insert into tmpTable values(@value)
end

end

----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------
--exec readCSV 'Ruchira,randana,galappaththi,ruch'
----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------

To run this code, first create a table named 'tmpTable' schema(_name varchar(50)) on your Data Base. This can be done by the following.

create table tmpTable(_name varchar(50))

After creating the table, run the above code on your database. To test the procedure, uncomment the bottom line 'exec readCSV 'Ruchira,randana,galappaththi,ruch'' and execute.

With a little bit of effort, you can get this up and running.

Comments

Popular posts from this blog

Encrypt and Decrypt Images Using Java

kSoap2 printing request dump

ASP Response.Write newline