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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Insert Identity depending on the value of a Column

Author  Topic 

coagulance
Yak Posting Veteran

78 Posts

Posted - 2008-04-23 : 17:18:55
Would like to have Identity Inserted wrt a date column
Eg:
ProdDate ID Details
============================
2008.04.01 1 afafafaf
2008.04.01 2 GAFSGHFGF
2008.04.02 1 GAGJAGSDH
2008.04.02 2 QYTYTT
2008.04.03 3 QYTWRRT

At present it is not an Auto increment , I check for the Maximum ID and add One to the next Inserted record. Is there any simpler method?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-23 : 17:26:46
SELECT ProdDate, ROW_NUMBER() OVER(PARTITION BY ProdDate ORDER BY Details ASC) AS 'ID', Details
FROM YourTable

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

coagulance
Yak Posting Veteran

78 Posts

Posted - 2008-04-23 : 17:36:11
Hi Thanks,
I would like to have this in my Insert Statement Like I just Give the ProdDate and Details , ID should be an Auto Increment wrt ProdDate and Should Start from 1 when the ProdDate Changes
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-04-23 : 23:26:25
Why insert it into the table? Just get the row number as your ID when you select it out as Tara suggests.
You might find it helpful to add an actual ID if the date isn't fine grained enough. If so, just add an identity and include it in the ORDER BY is her query.
Go to Top of Page

coagulance
Yak Posting Veteran

78 Posts

Posted - 2008-04-23 : 23:32:29
Would like to have ID as a Primary key along with ProdDay. Hence this requirement.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-23 : 23:40:20
quote:
Originally posted by coagulance

Would like to have ID as a Primary key along with ProdDay. Hence this requirement.


then its better to add an identity column to your table. Inserting records will automatically increment the value of this column.
Go to Top of Page

coagulance
Yak Posting Veteran

78 Posts

Posted - 2008-04-24 : 00:03:24
As explained earlier, I would like to reset this Identity when the ProdDay Starts so that my ID for the Day will not be a vague number.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-24 : 00:11:30
Then you have to manage it. SQL Server will not do this for you.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-24 : 00:32:28
use DBCC CHECKIDENT() if you want to reset the identity column value.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-24 : 02:22:48
DBCC CHECKIDENT() only works when you have an identity column. coagulance can't use an identity column.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-24 : 02:32:24
quote:
Originally posted by tkizer

DBCC CHECKIDENT() only works when you have an identity column. coagulance can't use an identity column.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/


I'm confused wasnt OPs query to add an ID column to his table whose value needed to be reset each day?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-24 : 11:10:27
What's the point of using an identity column if you are going to reset it all of the time?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-04-24 : 11:23:04
quote:
Originally posted by tkizer

What's the point of using an identity column if you are going to reset it all of the time?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/


Yup thats true . Sorry was thinking that OP was trying for a temporary table which is used for some intermediate operations and has to clear after day. Didnt happen to me then that same is achieved by using ROW_NUMBER(). my bad..
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-04-24 : 12:29:10
Well your solution solves the problem at first glance, but then how are you going to manage DBCC CHECKIDENT once per day? If you run it from a job, then there is a chance that rows will get inserted with identity values of the previous day. If you run it from the application, then you've got to provide elevated permissions to the account plus you have to prevent everyone from running any writes until it's been reset. It would be a big management issue.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -