SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 12-06-2006, 12:25 AM
Pureke
 
Posts: n/a
Default MS SQL dialect question:

How to select highest value and then create new object with that value +1?


I'm a real newb on sql and can't find any solution to my problem.
I'm having a db with a table Item and in that table are quite a few collumns.
One of them is named IID
What I want to do is the following:
Select the highest IID, then add 1 (say the max IID is 50, I want to use 51) and fill in all the collumns.
I know how to select that highest IID, I know how to fill in all the other values, I just can't figure out how to add 1 to that IID :s

I have this:
SELECT *
FROM Item
WHERE IID = (SELECT MAX(IID) FROM Item)

INSERT INTO Item
(IID, all the rest of the collumns here )
VALUES ( no idea what will come here, again all the rest of the values here)


I'm pretty sure I'll need some extra stuff to make it work but again, I can't figure it out

Thx already
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 12-06-2006, 02:51 PM
Musclebai
 
Posts: n/a
Default SQL 92 answer. Re: How to select highest value and then create new object with that value +1?

INSERT INTO Item
(IID )
VALUES (SELECT (IID +1)
FROM Item
WHERE IID = convert(int,(SELECT MAX(IID ) FROM Item))
)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-23-2007, 04:30 AM
Dimitar
 
Posts: n/a
Default ANY answer. Re: How to select highest value and then create new object with that value +1?

This should work it most dialects:
INSERT INTO item (iid) SELECT max(iid) +1 FROM item;
I find it simpler, but it assumes the IID column is defined NOT NULL.
Here is another attempt, which does not make that assumption:
INSERT INTO item (iid) SELECT max(case when iid IS NULL then -1 else iid end) +1 FROM item;
But what if the table is initially empty?
As the function MAX is applied to an empty set, it will return the null value, which will become the value of the first IID.
Let us again consider the first version (with the NOT NULL assumption):
nsert INTO item (iid) SELECT case when hm IS NULL then 0 else hm end FROM (SELECT max(iid) + 1 FROM item ) AS t
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I select the highest 20 to 30 values? ben SQL - Questions and Answers 4 01-18-2008 11:56 AM


All times are GMT. The time now is 09:54 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8