读书人

请帮小弟我看下小弟我写的这个存储过程

发布时间: 2012-03-16 16:34:56 作者: rapoo

请帮我看下我写的这个存储过程哪里有错。。。谢谢
就是同时插入两个表的
create PROCEDURE Stored_Procedure_Name

@ChargeUpDateId uniqueidentifier = NEWID() ,
@year int,
@Month int,
@ChargeUpDate datetime,
@CompanyCode varchar(20),

@PostingDateID uniqueidentifier = NEWID(),
@PostingDate datetime


AS transaction
INSERT INTO ChargeUpDate(ChargeUpDateId,Year,Month,ChargeUpDate,CompanyCode)values(@ChargeUpDateId,@year,@Month,@ChargeUpDate,@CompanyCode)
if @@error <> 0
begin
print 'insert into ChargeUpDate Failure '
goto pro

insert into PostingDate(PostingDateID,ChargeUpDateID,PostingDate)values(newid(),@ChargeUpDateId,@PostingDate)
if @@error <> 0
begin
print 'insert into PostingDate Failure '
goto pro

commit transaction
return 0
pro:
rollback transaction
return 1
Go

谢谢啊

[解决办法]

create PROCEDURE Stored_Procedure_Name

@ChargeUpDateId uniqueidentifier = NEWID() ,
@year int,
@Month int,
@ChargeUpDate datetime,
@CompanyCode varchar(20),

@PostingDateID uniqueidentifier = NEWID(),
@PostingDate datetime


--AS transaction
AS--笔误!!!刚才漏了一个AS
begin transaction

INSERT INTO ChargeUpDate(ChargeUpDateId,Year,Month,ChargeUpDate,CompanyCode)values(@ChargeUpDateId,@year,@Month,@ChargeUpDate,@CompanyCode)
if @@error <> 0
--begin
print 'insert into ChargeUpDate Failure '
--goto pro

insert into PostingDate(PostingDateID,ChargeUpDateID,PostingDate)values(newid(),@ChargeUpDateId,@PostingDate)
if @@error <> 0
--begin
print 'insert into PostingDate Failure '
--goto pro

if @@error=0
begin
commit transaction
return 0
end
else
begin
--pro:
rollback transaction
return 1
end
Go
[解决办法]


就是同时插入两个表的
create PROCEDURE Stored_Procedure_Name



@ChargeUpDateId uniqueidentifier = NEWID() ,
@year int,
@Month int,
@ChargeUpDate datetime,
@CompanyCode varchar(20),

@PostingDateID uniqueidentifier = NEWID(),
@PostingDate datetime


AS
begin
begin transaction
INSERT INTO ChargeUpDate(ChargeUpDateId,Year,Month,ChargeUpDate,CompanyCode)values(@ChargeUpDateId,@year,@Month,@ChargeUpDate,@CompanyCode)
if @@error <> 0
begin
print 'insert into ChargeUpDate Failure '
goto pro
end

insert into PostingDate(PostingDateID,ChargeUpDateID,PostingDate)values(newid(),@ChargeUpDateId,@PostingDate)
if @@error <> 0
begin
print 'insert into PostingDate Failure '
goto pro
end

commit transaction
return 0
pro:
rollback transaction
return 1
Go
end

读书人网 >SQL Server

热点推荐