Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


using a sub sql with expression


using a sub sql with expression

Author
Message
orkie
orkie
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
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


Osoba
Osoba
SSC-Enthusiastic
SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)

Group: General Forum Members
Points: 148 Visits: 132
You could do this with some type of variable, but I would recommend using IDENTITY for an incremental ID column.



Douglas De Ivey
Douglas De Ivey
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 84

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'

 


 


artilugio
artilugio
SSC-Enthusiastic
SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)SSC-Enthusiastic (102 reputation)

Group: General Forum Members
Points: 102 Visits: 153

--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')


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search