我使用PDOException: SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in lock_may_be_available() (line 167 of /home/..../public_html/page/includes/lock.inc).
命令将一些值插入表中,然后我将其插入值INSERT
。现在我想使用OUTPUT
命令通过其id选择插入的行我使用下面的代码,但它似乎不起作用。
SELECT
答案 0 :(得分:2)
您还可以使用output parameter代替select
将行返回给您的应用。
如果Id
生成了sequence
,请使用next value for
:
create procedure [dbo].[usp_UserRegistration] (
@Name varchar(100),
@Contact varchar(20),
@dob varchar(20),
@MailAddress varchar(500),
@Id int output
) as
begin;
set nocount, xact_abort on;
begin try;
begin tran
/* your critiera for a new record here */
select @Id = Id
from dbo.Customer with (updlock, serializable)
where Name = @Name
and dob = @dob;
if @@rowcount = 0
begin;
set @Id = next value for dbo.IdSequence /* with your Sequence name here */
insert into dbo.Customer (Id, Name, Contact, dob, MailAddress)
values (@Id, @Name, @Contact, @dob, @MailAddress );
end;
commit tran;
end try
begin catch;
if @@trancount > 0
begin;
rollback transaction;
throw;
end;
end catch;
go
如果您的Id
是identity
列,请使用scope_identity()
。
There is a big difference between @@identity
, scope_identity()
, and ident_current()
参考文献:
序列: