我是个新手,这个就是写不对,高手指点下。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
@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
@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
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
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
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
是么?
你两个raiserrror值写成一样了,楼主都指出来了,还说无误? 哈哈.
出了错,提示 ')'附近有错....晕。
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
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
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
(
@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