Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Old Forums
 CLOSED - General SQL Server
 Array building and populating in SQL Server

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-09-06 : 09:47:26
Ram writes "Hi,

I have a requirement Wherin there are three String passed to the procedure. (All are comma separated). These strings are position based. i.e. Each string has same set of values. I need to maintain a relation between the value at each position of the string.
For example
Str1 = '1,2,3,4,5'
Str2 = '10,20,30,40,50'
Str3 = 'aaa,bbb,ccc,ddd,eee'

The values '1' '10' and 'aaa' neds to go together.Similarly for the othr values (2,10 and bbb, etc).

So I need to develop three arrays so that I could refer them through indexes. How to I go about doing this? Please help me to populate three arrays.

Thanks in advance.

Regards,
Ramesh"

1fred
Posting Yak Master

158 Posts

Posted - 2002-09-06 : 09:54:40
There is no array in T-SQL, what you can do is to insert the values in temp table or the use the Table data type. Make a search on this site, you'll find something interesting

Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2002-09-06 : 13:57:03
Believe it or not, I just did this a couple of weeks ago with 2 CSV lists. I'm sure you can expand it to do with 3.

Step #1: READ Rob Volk's Article on parsing CSV's at [url]http://www.sqlteam.com/item.asp?ItemID=2652[/url]

Step #2: Here's the script I used...
If OBJECTPROPERTY(OBJECT_ID('ParseCSVLists'), 'IsProcedure') = 1
drop procedure ParseCSVLists
GO

/*****************************************************************************
Author: Mark Caldwell
Date: 08/19/02
Desc: Parse two correlated CSV lists into a result table

*****************************************************************************/

CREATE PROCEDURE ParseCSVLists
@GameID int,
@ItemList varchar(5000),
@AnswerList varchar(5000)
AS
SET NOCOUNT ON

-- Create working tables for input & output of parsing statement.
DECLARE @TempSource TABLE (
ListType varchar(10),
List varchar(5000)
)

DECLARE @TempItemList TABLE (
RecID int NOT NULL IDENTITY(1,1),
ListType varchar(10),
OneValue varchar(100)
)

DECLARE @TempAnswerList TABLE (
RecID int NOT NULL IDENTITY(1,1),
ListType varchar(10),
OneValue varchar(10)
)

-- Insert lists passed in as parameters into source table.
INSERT INTO @TempSource (ListType, List) VALUES ('Item', @ItemList)
INSERT INTO @TempSource (ListType, List) VALUES ('Answer', @AnswerList)

-- Use CSV Parsing logic from Rob Volk's article on SQLTeam (http://www.sqlteam.com/item.asp?ItemID=2652)
-- First, do Items and assign a record number for joining to Answers later.
-- Because programmer concatenates comma at end of string, REMOVE the end-comma concatenation from Rob's Code.
INSERT INTO @TempItemList
SELECT
ListType,
NullIf(SubString(',' + List, ID , CharIndex(',' , ',' + List, ID) - ID) , '') AS OneValue
FROM Tally, @TempSource
WHERE ListType = 'Item'
AND ID <= Len(',' + List)
AND SubString(',' + List, ID - 1, 1) = ','

-- Next, do Answers and assign a record number for joining to Itemss later.
INSERT INTO @TempAnswerList
SELECT
ListType,
NullIf(SubString(',' + List, ID , CharIndex(',' , ',' + List, ID) - ID) , '') AS OneValue
FROM Tally, @TempSource
WHERE ListType = 'Answer'
AND ID <= Len(',' + List)
AND SubString(',' + List, ID - 1, 1) = ','

-- Now Join the Items to their Answers and Insert into final Results Table with Game ID.
INSERT INTO Results
SELECT
@GameID,
I.OneValue as Item,
A.OneValue as Answer
FROM @TempItemList I
JOIN @TempAnswerList A on I.RecID = A.RecID

SET NOCOUNT OFF
GO

GRANT EXECUTE ON ParseCSVLists TO WebUser
GO


NOTE: Because the string I was being provided by an outside developer had a trailing comma already, I took that part out of Rob's script. You might need to add it back in.

Also, note that the INSERT INTO RESULTS... part at the end is updating our permanent table with the correlated arrays in multiple records.

Edited by - ajarnmark on 09/06/2002 15:58:44
Go to Top of Page
   

- Advertisement -