Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with select and insert

Author  Topic 

haden213
Starting Member

3 Posts

Posted - 2009-09-26 : 05:10:23
I have a table with some values as show below

ENTRYID | CLIENTID | ENTRYNUMBER | MESSAGE
-------------------------------------------
1 | 222 | 1 |'ffdfdf'
2 | 222 | 2 |'dsds'
3 | 222 | 3 | 'dfdfd
4 | 333 | 1 | 'sdsd'
5 | 444 | 2 | 'sdsd'

When i insert a new row I need to generate a new ID which will be the 'MAX(ENTRYID) + 1' and the entry number would 'MAX(ENTRYNUMBER)+1 WHERE CLIENTID = <<given client id>>' This table doesn't have any primary key or autoincrement values.

I would greatly appreciate it if someone would help we in getting this done with one single query...

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-09-26 : 05:47:11
That's absolut dangerous if more than one insert at a time happens...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-29 : 00:29:21
if its sql 2005 or greater use ROW_NUMBER function for this. somethinglike:-

SELECT ROW_NUMBER() OVER (PARTITION BY CLIENTID ORDER BY ENTRYID) AS ENTRYNUMBER,...

and make ENTRYID as an identity column
Go to Top of Page
   

- Advertisement -