简体   繁体   English

检查已提交的事务,如果是,则返回一个值

[英]Check transaction committed and if so return a value

I want to insert a new user (Email address) into a table of users.我想在用户表中插入一个新用户(电子邮件地址)。 If successful (no duplicate), insert basic configurations for new user to other tables, and return some data to client.如果成功(没有重复),将新用户的基本配置插入其他表,并返回一些数据给客户端。 If not successful, receive relevant notification in dataset in C# project.如果不成功,在 C# 项目中的数据集中接收相关通知。

Here is sample code so you can easily comment on it这是示例代码,因此您可以轻松对其进行评论

ALTER PROCEDURE [dbo].[CreateUser] 
  @Email nvarchar(256),   
  @Password nvarchar(256)   
AS
BEGIN
  declare @UserID uniqueidentifier;
  declare @ConfigID uniqueidentifier;
  declare @TopMostNode uniqueidentifier;

  BEGIN TRANSACTION;
    select @UserID = NEWID();
    select @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

Here is one possibility where you return a recordset with the data you require and a flag to indicate whether the user already existed or not.这是一种可能,您返回一个记录集,其中包含您需要的数据和一个指示用户是否已经存在的标志。

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

Another way to return data to the app is using output parameters eg将数据返回到应用程序的另一种方法是使用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

That code is almost complete.该代码几乎完成。 Just need to add a SELECT to return the new keys.只需要添加一个 SELECT 来返回新键。 In case of an error, you will get an exception.如果出现错误,您将收到异常。 EG:例如:

ALTER PROCEDURE [dbo].[CreateUser] 
  @Email nvarchar(256),   
  @Password nvarchar(256)   
AS
BEGIN
  declare @UserID uniqueidentifier;
  declare @ConfigID uniqueidentifier;
  declare @TopMostNode uniqueidentifier;

  BEGIN TRANSACTION;
    select @UserID = NEWID();
    select @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 
  select @UserID UserID, @ConfigID ConfigID;
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM