all groups > sql server new users > february 2007 >
You're in the

sql server new users

group:

Newbie: Sequence Number


Re: Newbie: Sequence Number Bandit
2/23/2007 12:00:00 AM
sql server new users: Thank you!

I will do it the way you recommended:

[quoted text, click to view]

However - for later use - is it possible to combine sub-queries with actual
input in the way I tried? What is the correct way to do this?

[quoted text, click to view]

Bandit

Re: Newbie: Sequence Number Roy Harvey
2/23/2007 12:00:00 AM
[quoted text, click to view]

Simply add the other values to the SELECT list.

insert into mytable (SequenceNumber, Field2, Field3)
select max(SequenceNumber)+1 as SequenceNumber,
'Text for field 2',
'Text for field 3'
from transactiontable,

Roy Harvey
Newbie: Sequence Number Bandit
2/23/2007 1:22:40 AM
I have made a query to add a record with increasing sequence numbers, and it
seems to work properly.

insert into mytable (SequenceNumber)
select max(SequenceNumber)+1 as SequenceNumber from transactiontable

PROBLEM 1
--------------

However I also need to add some data to the other fields in the record,
directly in the query:

insert into mytable (SequenceNumber, Record2, Record3)
select max(SequenceNumber)+1 as SequenceNumber from transactiontable, 'Text
for record 2', 'Text for record 3'

However, I do not understand how to do this. Any input is appreciated!

PROBLEM 2
--------------

If possible, I would also like the query to return the value inserted into
the SequenceNumber field of the current record.



Bandit






Re: Newbie: Sequence Number Bob Simms
2/23/2007 7:18:59 AM
SQL has its own sequential generator

CREATE TABLE mytbl( ID int IDENTITY NOT NULL,
Mydata varchar(20) NOT NULL,
Mydata2 varchar(20) NOT NULL)

INSERT INTO mytbl(Mydata, mydata2)
VALUES ('some data', 'some more data')

to find out the value generated

SELECT SCOPE_IDENTITY()

[quoted text, click to view]

AddThis Social Bookmark Button