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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 SQL array

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-12-26 : 10:39:53
Cris writes "Hello,

I want to create an array in a store procedure and I don't know how to do it. Can anybody please help ?

Thanks a lot,

Cris"

mfemenel
Professor Frink

1421 Posts

Posted - 2002-12-26 : 10:48:06
Well, bad news is, you can't. What you could do is use a temp table, or if you're using SQL2k, you could use a table variable.

Mike
"oh, that monkey is going to pay"
Go to Top of Page

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2002-12-26 : 19:14:00
Chris,

An array is nothing more than set of memory addresses which you use as a data structure to read and write data.

In sqlserver server you data structure of choice would be a table.

If you want an array create a table with 2 columns, 1 for the id and one for the value.







Edited by - ValterBorges on 12/26/2002 19:14:48
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2002-12-27 : 11:51:08
Once you starting using tables instead of arrays, you'll never want to go back!

- Jeff
Go to Top of Page

GreatInca
Posting Yak Master

102 Posts

Posted - 2002-12-30 : 12:55:04
They should have an auto-incrment value for use in select or an insert. The auto-incrment in the select would be most useful in selecting the top 20 of a grouping (top 20 for each day ...) without using temp tables / table vars or cursors. It is much faster to insert 100 rows with 1 statement than with 100 indivudal statements.

Go to Top of Page

ValterBorges
Master Smack Fu Yak Hacker

1429 Posts

Posted - 2002-12-30 : 18:30:30
I'm sure if you post an example someone will be able to figure out a better way to solve it than having to do 100 statements.

quote:

They should have an auto-incrment value for use in select or an insert.


Use an identity data type for insert.

For select statements here two methods one without a temp table
from post within the last few months


/* Author Rob Volk

Generate Sequence Order By Group

COLUMN_1 SEQUENCE_ID
-------- -----------
A NULL
A NULL
B NULL
B NULL
B NULL
C NULL
C NULL
C NULL
C NULL


What I want is the #temp table to look like this:


COLUMN_1 SEQUENCE_ID
-------- -----------
A 1
A 2
B 1 <- The sequence_id needs to be reset
B 2 to 1 when the row value in COLUMN_1
B 3 changes
C 1
C 2
C 3
C 4


Seing from the above, the sequencing for SEQUENCE_ID is by COLUMN_1 aggregation.

*/

DECLARE @col1 varchar(1), @seq int
SELECT @seq=0
UPDATE #temp
SET @seq = SEQUENCE_ID = CASE @col1 WHEN COLUMN_1 THEN @seq+1 ELSE 1 END,
@col1 = COLUMN_1

/*
Author Onamuji
*/
CREATE TABLE Sequences (Value INT NOT NULL PRIMARY KEY)

WHILE (SELECT ISNULL(MAX(Value), 0) FROM Sequences) < 8192
INSERT INTO Sequences SELECT ISNULL(MAX(Value), -1) + 1 FROM Sequences

CREATE TABLE #temp (Column_1 CHAR(1), Sequence_ID INT)

INSERT INTO #temp (Column_1) VALUES ('A')
INSERT INTO #temp (Column_1) VALUES ('A')
INSERT INTO #temp (Column_1) VALUES ('B')
INSERT INTO #temp (Column_1) VALUES ('B')
INSERT INTO #temp (Column_1) VALUES ('B')
INSERT INTO #temp (Column_1) VALUES ('C')
INSERT INTO #temp (Column_1) VALUES ('C')
INSERT INTO #temp (Column_1) VALUES ('C')
INSERT INTO #temp (Column_1) VALUES ('C')

SELECT totals.Column_1, seq.Value
FROM (SELECT Column_1, COUNT(*) AS TotalCount FROM #temp GROUP BY Column_1) AS totals
INNER JOIN Sequences AS seq ON seq.Value <= totals.TotalCount AND seq.Value > 0



DROP TABLE #Temp
DROP TABLE Sequences


quote:

The auto-incrment in the select would be most useful in selecting the top 20 of a grouping (top 20 for each day ...) without using temp tables / table vars or cursors.



From a sql team post within the last 3 months.

/* This will return records grouped and only those records with a certain count and matching criteria */
/* ie : This particular example returns all AutoID's that have other AutoID's and 3 DateStamps which are greater than
them */

SELECT AutoID, DateStamp
FROM History H
WHERE EXISTS (
SELECT 1
FROM History
WHERE AutoID = H.id and DateStamp = H.DateStamp
GROUP BY AutoID
HAVING (
SELECT COUNT(*)
FROM History
WHERE AutoID = H.AutoID And DateStamp >= H.DateStamp
) > 3
)
ORDER BY AutoID







Edited by - ValterBorges on 12/30/2002 18:50:38

Edited by - ValterBorges on 12/30/2002 18:52:14

Edited by - ValterBorges on 12/30/2002 18:52:38
Go to Top of Page
   

- Advertisement -