我是个新手,这个就是写不对,高手指点下。CREATE PROCEDURE User_Insert@UserName nvarchar(50),
@UserPwd nvarchar(50),
@UserTypeID intasset nocount on  begin transactionselect Count(*) from Users where UserName=@UserNameif Count(*) <> 0begin rollback raiserror ('The UserName is been Register.',16,1) return endinsert into Users (UserName,UserPwd,UserTypeID) values (@UserName,@UserPwd,@UserTypeID)select @@identityif @@error <>0begin rollback raiserror ('Error insert into User.',16,1) return endcommit
GO

解决方案 »

  1.   

    CREATE PROCEDURE User_Insert@UserName nvarchar(50),
    @UserPwd nvarchar(50),
    @UserTypeID intas
    set nocount on 
    if not exists(select 1 from Users where UserName=@UserName)
    begin
    insert into Users (UserName,UserPwd,UserTypeID) values (@UserName,@UserPwd,@UserTypeID)
    --select @@identity
    if @@error <>0 
    raiserror ('Error insert into User.',16,1)
    else
        raiserror ('Error insert into User.',16,1)
    GO
      

  2.   


    CREATE PROCEDURE User_Insert@UserName nvarchar(50),
    @UserPwd nvarchar(50),
    @UserTypeID intas
    declare @usercount intset nocount on  begin transactionselect @usercount = Count(*) from Users where UserName=@UserNameif @usercount <> 0begin rollback raiserror ('The UserName is been Register.',16,1) return endinsert into Users (UserName,UserPwd,UserTypeID) values (@UserName,@UserPwd,@UserTypeID)select @@identityif @@error <>0begin rollback raiserror ('Error insert into User.',16,1) return endcommit
    GO
      

  3.   

    随手敲的,可能手误
    CREATE PROCEDURE User_Insert
    (
    @UserName nvarchar(50),
    @UserPwd nvarchar(50),
    @UserTypeID int
    )
    AS
    BEGIN
    SET NOCOUNT ON
    IF EXISTS(SELECT 1 FROM Users WHERE UserName=@UserName)
    raiserror ('The UserName is been Register.',16,1) 
    ELSE
    BEBIN
    BEGIN TRAN
    INSERT Users(UserName,UserPwd,UserTypeID) SELECT @UserName,@UserPwd,@UserTypeID

    IF @error!=0
    BEGIN
    ROLLBACK TRAN
    raiserror ('Error insert into User.',16,1) 
    END
    ELSE
    COMMIT TRAN
    SELECT @@identity
    END
    END
    GO
      

  4.   

    楼上的哥哥if @@error <>0 
            raiserror ('Error insert into User.',16,1) --这个是因为已经被注册的提示吗?
    else
        raiserror ('Error insert into User.',16,1)
    GO
    -------------------------也就是可以写?if @@error <>0 
            raiserror ('The UserName is been Register.',16,1) 
    else
        raiserror ('Error insert into User.',16,1)
    GO
    是么?
      

  5.   

    我5楼的代码已知的手误,请将BEBIN改为BEGIN
      

  6.   


    你两个raiserrror值写成一样了,楼主都指出来了,还说无误? 哈哈.
      

  7.   

    最后一个  raiserror ('Error insert into User.',16,1)
    出了错,提示 ')'附近有错....晕。
      

  8.   


    CREATE PROCEDURE User_Insert@UserName nvarchar(50),
    @UserPwd nvarchar(50),
    @UserTypeID intasset nocount on  begin transactionselect Count(*) from Users where UserName=@UserNameif Count(*) <> 0
    begin
    rollback transaction
    raiserror ('The UserName is been Register.',16,1) return 
    end
    insert into Users (UserName,UserPwd,UserTypeID) values (@UserName,@UserPwd,@UserTypeID)select @@identityif @@error <>0
    begin
     rollback 
     raiserror ('Error insert into User.',16,1) 
     return
    endcommit
    GO
      

  9.   

    5楼的不止 那个begin出了错...还有好几处错误...汗。今晚不行了,看来的等明天上午11点半来电在说了。
      

  10.   

    不是生气呀,是有错就改呀为什么不能一样呀少了一个End吧create table ta(username varchar(10),userpwd varchar(10),usertypeid int)
    goCREATE PROCEDURE User_Insert@UserName nvarchar(50),
    @UserPwd nvarchar(50),
    @UserTypeID intas
    set nocount on 
    if not exists(select 1 from Users where UserName=@UserName)
    begin
        insert into Users (UserName,UserPwd,UserTypeID) values (@UserName,@UserPwd,@UserTypeID)    
        --select @@identity    
        if @@error <>0 
            raiserror ('Error insert into User.',16,1)
    end
    else
        raiserror('Error insert into User.',16,1)
    GO
    drop table ta
    drop proc User_Insert
      

  11.   

    create table Users(username varchar(10),userpwd varchar(10),usertypeid int)
    goCREATE PROCEDURE User_Insert@UserName nvarchar(50),
    @UserPwd nvarchar(50),
    @UserTypeID intas
    set nocount on 
    if not exists(select 1 from Users where UserName=@UserName)
    begin
        insert into Users (UserName,UserPwd,UserTypeID) values (@UserName,@UserPwd,@UserTypeID)    
        --select @@identity    
        if @@error <>0 
            raiserror ('Error insert into User.',16,1)
    end
    else
        raiserror('Error insert into User.',16,1)
    GOexec User_Insert 'test','***', 1
    /*
    (所影响的行数为 1 行)
    */
    exec User_Insert 'test','***', 1
    /*
    服务器: 消息 50000,级别 16,状态 1,过程 User_Insert,行 18
    Error insert into User.
    */select * from users
    /*
    username   userpwd    usertypeid  
    ---------- ---------- ----------- 
    test       ***        1(所影响的行数为 1 行)*/
    drop table Users
    drop proc User_Insert
      

  12.   

    CREATE PROCEDURE pro_AddUser
    (
    @UserName varchar(50),
    @UserPwd varchar(50),
    @UserTypeID int
    )
    as
    set nocount on
    if not exists(select 1 from Users_table where UserName=@UserName)
    begin transcation
        insert into Users_table (UserName,UserPwd,UserTypeID) values (@UserName,@UserPwd,@UserTypeID)        if @@error <>0 
            raiserror ('Error insert into User.',16,1)
        rollback
    else
        raiserror('Error insert into User.',16,1)
        commit
    endGO