Results 1 to 2 of 2
-
03-02-2011, 06:28 PM #1
- Join Date
- Apr 2008
- Location
- Boston, USA
- Posts
- 13,175
- Thanks
- 744
- Thanked 699 Times in 550 Posts
- Blog Entries
- 4
- Feedback Score
- 4 (100%)
Insert Max int Type Primary Column Value + 1 - Microsoft Sql
How to Insert Max int Type Primary Column Value + 1 - Microsoft Sql
If you have a primary column of type int, you need to insert unique values for it, best approach is set the column IS IDENTITY type true with identity seed 1. That way you wont have to worry about it.
However in some cases you might not be able to do that. Recently i had to work for a large health provider in MA area. They gave me an access to the db without proper privileges, which doesn't allow me set the column IS IDENTITY true. Since i am in rush to finish i have to come up with a way i could insert unique incremental values for the int primary key column on a table.
insert into MyTable(tId)
Select IsNull((Select Max(IsNull(tId,0))+1 from MyTable),1)
Note that I have to wrap the inner subquery in a IsNull for the case when there is no rows in table.Free Classified Ads & BUSINESS/PROFESSIONAL SOCIAL NETWORK
-
04-05-2011, 01:26 AM #2
Junior
- Join Date
- Apr 2011
- Posts
- 332
- Thanks
- 0
- Thanked 2 Times in 2 Posts
- Feedback Score
- 0
Thanks a lot for sharing your knowledge.It is a useful tool.
Similar Threads
-
Set Identity Insert On Off - Microsoft SQL
By manik in forum DatabasesReplies: 3Last Post: 04-05-2011, 02:08 AM -
Get Column Names of Table - Microsoft Sql
By manik in forum DatabasesReplies: 0Last Post: 03-02-2011, 05:08 PM -
Different topics for my primary domain
By jaabirdon78 in forum Search Engine OptimizationReplies: 1Last Post: 02-26-2011, 12:03 AM -
Cannot find either column dbo or the user-defined function or aggregate or the name is ambiguous - Microsoft SQL
By manik in forum DatabasesReplies: 0Last Post: 01-26-2011, 01:24 PM -
Type X conflicts with the imported Type X Compile Errror - Asp .net
By manik in forum ASPReplies: 0Last Post: 12-21-2010, 05:42 PM


Reply With Quote

