I have a stored proc containing an SQL statement which is something like:
CREATE PROCEDURE SaveUser
@UserName nvarchar(10),
@FirstName nvarchar(150),
@LastName nvarchar(150)
AS
BEGIN
INSERT INTO Users (UserName, FirstName, LastName)
VALUES (@UserName, @FirstName, @LastName)
SELECT SCOPE_IDENTITY()
END
Some users cannot log into the system and do not have a username; however, the UserName field has a unique index on it so I would like to be able to insert the users ID #, which is an auto increment field, as the UserName.
Does anyone know of an MS SQL Server method or variable which would allow me to do something like?
INSERT INTO Users (UserName, FirstName, LastName)
VALUES (ISNULL(@UserName, SCOPE_IDENTITY()),@FirstName,@LastName)
Edit My intention right now is to use something like the following
DECLARE @NextID int
SET @NextID = IDENT_CURRENT(Users) + IDENT_INCR(Users)
INSERT INTO Users (UserName, FirstName, LastName)
VALUES (ISNULL(@UserName, @NextID), @FirstName, @LastName)
I would just like to know if there is a built in method which could guarantee that this would be consistent.