I have a stored procedure that creates a user profile, my form has the following fields:
FirstName,
LastName.
EmailAddress,
ZipCode,
Password
The zip code is a FK in my UserProfile of type smallint the value may or may not already exist in a table called Location:
LocationId bigInt (identity index)
ZipCode smallint
Is there a way from within my stored procedure to query that table, if the item exist use the PK of that entry in my stored procedure ?
This is my stored procedure so far
CREATE PROCEDURE [dbo].[SP_InsertInitialProfile]
@FirstName NVARCHAR(1000),
@LastName NVARCHAR(1000),
@EmailAddress NVARCHAR(1000),
@ZipCode SMALLINT,
@Password NVARCHAR(1000)
AS
IF (SELECT COUNT(1) FROM UserProfile WHERE EmailAddress =@EmailAddress) = 1
BEGIN
RETURN -1
END ELSE
BEGIN
INSERT INTO UserProfile(FirstName,LastName,EmailAddress,Password)
VALUES(@FirstName,@LastName,@EmailAddress,@Password)
RETURN Cast(@@IDENTITY as INT)
END
You can do SP like this: