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.
| 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" |
 |
|
|
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 |
 |
|
|
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 |
 |
|
|
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. |
 |
|
|
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 tablefrom post within the last few months/* Author Rob VolkGenerate Sequence Order By GroupCOLUMN_1 SEQUENCE_ID-------- -----------A NULLA NULLB NULLB NULLB NULLC NULLC NULLC NULLC NULLWhat I want is the #temp table to look like this: COLUMN_1 SEQUENCE_ID-------- -----------A 1A 2B 1 <- The sequence_id needs to be resetB 2 to 1 when the row value in COLUMN_1B 3 changesC 1C 2C 3C 4Seing 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 SequencesCREATE 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 #TempDROP 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, DateStampFROM History HWHERE 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 AutoIDEdited by - ValterBorges on 12/30/2002 18:50:38Edited by - ValterBorges on 12/30/2002 18:52:14Edited by - ValterBorges on 12/30/2002 18:52:38 |
 |
|
|
|
|
|
|
|