Здесь есть одна возможность, когда вы возвращаете набор записей с требуемыми данными и флаг, указывающий, существует ли пользователь уже или нет.
alter procedure [dbo].[CreateUser]
(
@Email nvarchar(256)
, @Password nvarchar(256)
)
as
begin
declare @UserID uniqueidentifier, @ConfigID uniqueidentifier, @TopMostNode uniqueidentifier, @UserExists bit = 0;
select @UserID = UserID, @UserExists = 1 from Users where Email = @Email;
if @UserExists = 0 begin
begin transaction;
set @UserID = newid();
set @ConfigID = newid();
insert into Users (UserID, Email, [Password], CurrentConfig)
values (@UserID, @Email, @Password, @ConfigID);
insert into Configs (ConfigID, OwnerID, DisplayName, LastPrintID)
values (@ConfigID, @UserID, 'Default Config', 1);
commit transaction
end
-- Return whether the user already exists or not and the user id
select @UserExists, @UserId
return 0;
end
Другой способ вернуть данные в приложение - использовать output
параметры, например
alter procedure [dbo].[CreateUser]
(
@Email nvarchar(256)
, @Password nvarchar(256)
, @UserId uniqueidentifier out
, @UserExists bit out
)
as
begin
declare @ConfigID uniqueidentifier, @TopMostNode uniqueidentifier;
set @UserExists = 0;
select @UserID = UserID, @UserExists = 1 from Users where Email = @Email;
if @UserExists = 0 begin
begin transaction;
set @UserID = newid();
set @ConfigID = newid();
insert into Users (UserID, Email, [Password], CurrentConfig)
values (@UserID, @Email, @Password, @ConfigID);
insert into Configs (ConfigID, OwnerID, DisplayName, LastPrintID)
values (@ConfigID, @UserID, 'Default Config', 1);
commit transaction
end
return 0;
end