return value - Stored procedure for adding an application user MS SQL 2012 -
i trying write stored procedure 1 of queries adding users table. having difficulty capturing return values during success , failure.
the username column in user table defined have unique constraint.
this outline of stored procedure. stored procedure name: createuser
parameters
@username varchar(64)
@firstname varchar(64)
@lastname varchar(64)
@md5password varchar(64)
@access_control int
returns identity, -1 if user exists or (-@@error) (integer)
below stored procedure have written can't seem capture return value part.
alter procedure [dbo].[createuser] -- add parameters stored procedure here @username varchar(64) = null, @firstname varchar(64) = null, @lastname varchar(64) = null, @md5password varchar(255) = null, @access_control int = -10 declare @return_code int begin -- set nocount on added prevent result sets -- interfering select statements. set nocount on; -- insert statements procedure here insert [dbo].[user] ([username], [firstname], [lastname], [password], [access_control]) values (@username, @firstname, @lastname, @md5password, @access_control) if(@@error <> 0 ) begin print n'user name exists. username must unique' + @@error set @return_code = -1 end else begin set @return_code = scope_identity() end return @return_code end
i want set return value identity in case of success. -1 if user exists or error value if internal error occurs. how can capture behavior in stored procedure. in advance.
Comments
Post a Comment