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.
| Author |
Topic |
|
baburk
Posting Yak Master
108 Posts |
Posted - 2009-12-06 : 23:16:41
|
| Hi,We had used this to generate primary Key value. But the problem is after deleting the last record it returns the same ID.SELECT @FieldBlockSetupID = @EstateCode + 'R' + CAST((CASEWHEN(ISNULL(MAX(Id), -1) = -1)THEN 1WHEN MAX(Id) >= 1THEN MAX(Id) + 1END) AS VARCHAR)FROM Weighbridge.WBFieldBlockSetupBut this gives 1 when the table is empty. And after inserting a record also it returns 1.SELECT @FieldBlockSetupID = 'E' + 'R' + CAST((CASEWHEN(ISNULL(IDENT_CURRENT('tABLE_1'), -1) = -1)THEN 1WHEN IDENT_CURRENT('tABLE_1') >= 1THEN IDENT_CURRENT('tABLE_1')END) AS VARCHAR)Help me to rectify the problem. |
|
|
vikky
Yak Posting Veteran
54 Posts |
Posted - 2009-12-07 : 00:01:03
|
| Hi,DECLARE @FieldBlockSetupID VARCHAR(20)SELECT @FieldBlockSetupID = 'E' + 'R' + CAST((ISNULL(MAX(ID)+1, 1)) AS VARCHAR) FROM tABLE_1SELECT @FieldBlockSetupIDuse above statement for ur requirement.all these @@IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT statements work after the insert statement only other wilse returns null values.for more information read in BOL.Thanks,vikky. |
 |
|
|
baburk
Posting Yak Master
108 Posts |
Posted - 2009-12-07 : 01:03:29
|
quote: Originally posted by vikky Hi,DECLARE @FieldBlockSetupID VARCHAR(20)SELECT @FieldBlockSetupID = 'E' + 'R' + CAST((ISNULL(MAX(ID)+1, 1)) AS VARCHAR) FROM tABLE_1SELECT @FieldBlockSetupIDuse above statement for ur requirement.all these @@IDENTITY, SCOPE_IDENTITY, and IDENT_CURRENT statements work after the insert statement only other wilse returns null values.for more information read in BOL.Thanks,vikky.
Hi your query will return the same ID after deleting the record which we inserted last. Since maxID(ID) will return the maximum of ID. The problem still persist. |
 |
|
|
vikky
Yak Posting Veteran
54 Posts |
Posted - 2009-12-07 : 01:45:00
|
| hi,DECLARE @FieldBlockSetupID VARCHAR(20)SELECT @FieldBlockSetupID = 'E' + 'R' + CAST((CASEWHEN(ISNULL(IDENT_CURRENT('tABLE_1'), -1) = -1)THEN 1WHEN IDENT_CURRENT('tABLE_1') >= 1THEN IDENT_CURRENT('tABLE_1')+1END) AS VARCHAR)SELECT @FieldBlockSetupIDThanks,vikky. |
 |
|
|
baburk
Posting Yak Master
108 Posts |
Posted - 2009-12-07 : 23:50:39
|
quote: Originally posted by vikky hi,DECLARE @FieldBlockSetupID VARCHAR(20)SELECT @FieldBlockSetupID = 'E' + 'R' + CAST((CASEWHEN(ISNULL(IDENT_CURRENT('tABLE_1'), -1) = -1)THEN 1WHEN IDENT_CURRENT('tABLE_1') >= 1THEN IDENT_CURRENT('tABLE_1')+1END) AS VARCHAR)SELECT @FieldBlockSetupIDThanks,vikky.
Consider I am having a table ([DBO].[WBCustomer]) with IDENTITY column ID.Truncate table [DBO].[WBCustomer]DBCC CHECKIDENT ('[DBO].[WBCustomer]', RESEED, 1)WHEN I giveselect IDENT_CURRENT('[DBO].[WBCustomer]')gives me 1After I inserting a record also it gives me 1Since I having another column PID which is primary key column in the table([DBO].[WBCustomer]). I have to insert like E1, E2Since both the time it returns the same value(i.e. after reseed and inserting a single record) primary key violation error occurred.Thanks. |
 |
|
|
|
|
|
|
|