// @ColumnName is obviously the COLUMN name u use
---Get next ID number DECLARE @ID int IF (SELECT count(*) FROM @TableName ) > 0 BEGIN SELECT @ID = max(ColumnName ) from @TableName SET @ID = @ID + 1 END ELSE BEGIN SET @ID = 1 END
11391 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
---Get next ID number DECLARE @ID int IF (SELECT count(*) FROM @TableName ) > 0 BEGIN SELECT @ID = max(ColumnName ) from @TableName SET @ID = @ID + 1 END ELSE BEGIN SET @ID = 1 END
You need to create an account or log in to post comments to this site.
This allows you to insert into a table without specifying the column at all and SQL Server will handle getting the next available number.
For example, if you had an employee table with 2 columns, ID and NAME, you could define ID as an identity column, then you could do:
INSERT INTO Employee (NAME)
VALUES('Some Name')
Now the ID field will be populated automatically for you on the INSERT
Very nice feature of SQL Server.
Hope that makes sense