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


Use MAX in insert query


Use MAX in insert query

Author
Message
masoudk1990
masoudk1990
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 299
Hi, is it possible to use MAX function in an insert query?
I try to write a query similar to this:


INSERT INTO TableName (ID,Value)
VALUES (SELECT coalesce(MAX(ID),0) + 1 FROM TableName, 'something')



I get error, Incorrect syntax near the keyword 'SELECT'.
Thank you for help.

___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
masoudk1990
masoudk1990
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 299
Well, found a solution


Declare @i int;
Select @i = coalesce(MAX(ID),0) + 1 FROM TableName;

INSERT INTO TableName (ID,Value)
VALUES (@i, 'something')



If you think there would be any better way to do this please share it with me.
Thank you every one.

___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47351 Visits: 44392
INSERT INTO TableName (ID,Value)
SELECT coalesce(MAX(ID),0) + 1, 'something' FROM TableName;




Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


masoudk1990
masoudk1990
Valued Member
Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)Valued Member (66 reputation)

Group: General Forum Members
Points: 66 Visits: 299
GilaMonster (5/6/2013)
INSERT INTO TableName (ID,Value)
SELECT coalesce(MAX(ID),0) + 1, 'something' FROM TableName;



Thank you, hence this statement is not allowed:

INSERT INTO TableName (Value1,value2)
values(
select value1 from table1,
select value2 from table2
)



Instead you must join table1 and table2.
Much appreciated

___________________________________
Computer Enterprise Masoud Keshavarz
I don't care about hell.
If I go there I've played enough Diablo to know how to fight my way out.
Adi Cohn-120898
Adi Cohn-120898
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2224 Visits: 6079
You can still do it without joining the tables:



INSERT INTO TableName (Value1,value2)
select
(select value1 from table1),
(select value2 from table2)




Adi

--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47351 Visits: 44392
masoudk1990 (5/6/2013)
GilaMonster (5/6/2013)
INSERT INTO TableName (ID,Value)
SELECT coalesce(MAX(ID),0) + 1, 'something' FROM TableName;



Thank you, hence this statement is not allowed:

INSERT INTO TableName (Value1,value2)
values(
select value1 from table1,
select value2 from table2
)



Instead you must join table1 and table2.
Much appreciated


Join them, maybe, depends what you're doing. You can't mix the INSERT ... VALUES form of the insert statement and the INSERT ... SELECT form. It's either insert with a values clause and a list of values or insert with a select statement.


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


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