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)
 inc value during select,

Author  Topic 

marconi8
Yak Posting Veteran

73 Posts

Posted - 2003-05-14 : 06:55:34
 
INSERT DOSKA_FORMED_GPGRPR
SELECT (SET @TMP=@TMP+1),a.RAZDEL_NAME,a.ID
FROM DOSKA_RAZDEL a
WHERE EXISTS (
SELECT b.ID
FROM DOSKA_GRUPA b
WHERE a.ID=b.RAZDEL_ID
)

it is possible make insert during select is performed with value which increments





thanks

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2003-05-14 : 07:24:31
Nope. You can't set a variable and return a rowset with the same SELECT statement.

Other options.
1.) Use an IDENTITY.
2.) Use a Tally table.
3.) Use a loop.

Jay White
{0}
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-05-14 : 09:49:09
[head_exploding]2.) Use a Tally table.[/head_exploding]



Marek,

IDENTITY should do you just fine.

Sample from BOL:


CREATE TABLE jobs
(
job_id smallint
IDENTITY(1,1)
PRIMARY KEY CLUSTERED,
job_desc varchar(50) NOT NULL
DEFAULT 'New Position - title not formalized yet',
min_lvl tinyint NOT NULL
CHECK (min_lvl >= 10),
max_lvl tinyint NOT NULL
CHECK (max_lvl <= 250)
)



Brett

8-)
Go to Top of Page

marconi8
Yak Posting Veteran

73 Posts

Posted - 2003-05-14 : 09:51:45
listen your advice was

1.) Use an IDENTITY.
2.) Use a Tally table.
3.) Use a loop.

1 point dont match to my task, but about 2 and third point i dont now anything

here is my solution, at this mssql learning i can find only this way,



DELETE FROM DOSKA_FORMED_GPGRPR
GO

DECLARE @ORIG_ID D_MAIN_ID,
@ARRAY_IDX D_MAIN_ID,
@ORIG_NAME D_DOSKA_RAZDEL_NAME

SET @ARRAY_IDX=0


DECLARE odbor_cr CURSOR FOR
SELECT a.RAZDEL_NAME,a.ID
FROM DOSKA_RAZDEL a
WHERE EXISTS (
SELECT b.ID
FROM DOSKA_GRUPA b
WHERE a.ID=b.RAZDEL_ID
)
AND a.ID NOT IN (
SELECT ORIG_ID
FROM DOSKA_FORMED_GPGRPR
WHERE SECTION_NAME='razdel'
)

OPEN odbor_cr

FETCH NEXT FROM odbor_cr
INTO @ORIG_NAME,@ORIG_ID

WHILE @@FETCH_STATUS = 0
BEGIN
SET @ARRAY_IDX=@ARRAY_IDX+1

PRINT 'CURRENT ID='+CAST(@ORIG_ID AS VARCHAR)

INSERT INTO DOSKA_FORMED_GPGRPR(A_TYPE,CAPTION,ARRAY_M,ARRAY_IDX,ORIG_ID,SECTION_NAME)
VALUES('razdel_1',@ORIG_NAME,1,@ARRAY_IDX,@ORIG_ID,'razdel')

FETCH NEXT FROM odbor_cr
INTO @ORIG_NAME,@ORIG_ID
END


CLOSE odbor_cr
DEALLOCATE odbor_cr


--for checking
SELECT * FROM DOSKA_FORMED_GPGRPR





how you think ( looking at my example ) it is normal or my task ( see my example ) can write more normally

thanks,


this my task will be a trigger



Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-05-14 : 09:57:35
Marek,

Do you know what an identity column is?



Brett

8-)
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-05-14 : 10:06:03
You guys are missing his point. In a query, (not something stored in a table) he is trying to assign row #'s to the results, I'm guessing so he can page them (as in his other questions at this site).

A tally table does NOT help at all in that case.

An identity column CAN help, but you have to create a temp table or put the results into another table with the idenitity, and then use that. Does that make sense, marconi? just create another table with an identity, and put your results into that table. Then you have a row# field you can page by.



- Jeff
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2003-05-14 : 10:19:03
Isn't that what I was suggesting?

quote:

An identity column CAN help, but you have to create a temp table or put the results into another table with the idenitity, and then use that. Does that make sense, marconi? just create another table with an identity, and put your results into that table. Then you have a row# field you can page by.



But I belive we have a different problem here..

quote:

Marek,

Do you know what an identity column is?



Still waiting on that answer.



Brett

8-)
Go to Top of Page

marconi8
Yak Posting Veteran

73 Posts

Posted - 2003-05-14 : 11:04:35

yes also it look like that in my situation i can use ident_current +1 or something similar, i know but my task is next

there is 4 tables

---------------------
table_1 //r1
table_2 //r2
table_3 //r3
table_4 //r4
---------------------

table_collector // table that collects data from each of previous 4 tables
---------------------


when into table_4 is proceed some inserts then fires a trigger that trigger must to collect specially selecteed data (from all 4 tables ) into table table_collector

in table collector i have for example field section_name

foreach table selected data result field.section_name will be different


people sorry i cannot realy explain on english ehat i' am trying to make, i will try to finish my task at this moment with cursors





















Go to Top of Page
   

- Advertisement -