Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

using a sub sql with expression Expand / Collapse
Author
Message
Posted Monday, October 18, 2004 10:30 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 27, 2005 7:00 AM
Points: 5, Visits: 1

Hello,

I have the following SQL;

 

insert into t_postCard(ID, Source, toName, toEmail, fromName, fromEmail, message, readRecept, flash, dateSent, pass) values(4003, 'scccx', 'bxzcv', 'msdf', 'ssdf', 'sdfam', 'csdfsad', 0, 1, '01/01/2004', '2664')

 

I would like to modify it so that the value input for the ID is the result of a select max ID value + 1. Is this possible? Also if it is, then is it a acceptable way to increase the ID value by one for a table, ID is the primary key,

 

Many thanks

Post #142219
Posted Monday, October 18, 2004 10:49 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, September 16, 2010 9:01 AM
Points: 148, Visits: 132
You could do this with some type of variable, but I would recommend using IDENTITY for an incremental ID column.


Post #142222
Posted Monday, October 18, 2004 10:51 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, April 03, 2014 2:45 AM
Points: 54, Visits: 80

It is possible as I demonstrate below but it is not the recommended way to get a unique incrementing serial number.  What you want is an IDENTITY column.

An Identity column is automatically allocated a number by SQL Server when a row is added.  The numbers generally increade by 1 each row but you can occationally get a gap in the numbering if a transaction is rolled back. 


Create your table with
CREATE TABLE MyTab
( ID integer identity not null primary key,
 Source varchar(??) etc..
)

The column doesn't have to be a primary key but I presume you want it to be in this case.

You can also mark the column as an identity column via enterprise namager table designed (I can't remember where).

When you insert the data into the table, you should miss out the identify column from the statement. e.g.

insert into t_postCard(Source, toName, toEmail, fromName, fromEmail, message, readRecept, flash, dateSent, pass) values('scccx', 'bxzcv', 'msdf', 'ssdf', 'sdfam', 'csdfsad', 0, 1, '01/01/2004', '2664')

The full details about Identity columns can be seen in SQL Books online.

The SQL for not using the Identity column is below but it will not perform so well.

insert into t_postCard(ID, Source, toName, toEmail, fromName, fromEmail, message, readRecept, flash, dateSent, pass)
Select  (select ISNULL(MAX(ID)) + 1 from t_postCard),
 'scccx', 'bxzcv', 'msdf', 'ssdf', 'sdfam', 'csdfsad', 0, 1, '01/01/2004', '2664'

 


 

Post #142223
Posted Monday, October 18, 2004 12:59 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, January 15, 2014 1:40 PM
Points: 93, Visits: 120

--This works if ID is not identity

declare @ID int

set @ID = MAX(ID) + 1 FROM t_postcard

insert into t_postCard(ID, Source, toName, toEmail, fromName, fromEmail, message, readRecept, flash, dateSent, pass) values(@ID, 'scccx', 'bxzcv', 'msdf', 'ssdf', 'sdfam', 'csdfsad', 0, 1, '01/01/2004', '2664')

Post #142240
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse