这是我正在使用的示例存储过程
CREATE PROCEDURE [dbo].[CreateCustomer]
@firstName nvarchar(50) = null,
@lastName nvarchar(50) = null,
@emailAddress nvarchar(50),
@contactNumber varchar(15) =null,
@street1 nvarchar(50) = null,
@street2 nvarchar(50) = null,
@city nvarchar(50) = null,
@State nvarchar(50)= null,
@Country nvarchar(50)=null,
@postalCode varchar(5) = null,
@middleName nvarchar(50),
@password nvarchar(50),
@membershipExpieryDate datetime,
@status int OUT
AS
BEGIN TRY
BEGIN TRANSACTION
SAVE TRANSACTION initialP
DECLARE @lastAccessed datetime = GETDATE()
DEClARE @uniqueCustomerID UNIQUEIDENTIFIER= NEWID()
BEGIN TRANSACTION
INSERT INTO Customers(CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber,
Street1, Street2, City, State, Country, PostalCode, MiddleName, Password, MembershipExpieryDate)
VALUES(@uniqueCustomerID,@firstName, @lastName, @emailAddress, @lastAccessed, @contactNumber, @street1, @street2,
@city, @State, @Country, @postalCode, @middleName, @password, @membershipExpieryDate)
INSERT INTO Roles(CustomersCustomerID,Role) VALUES(@uniqueCustomerID,'V')
COMMIT
SELECT CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber,
Street1, Street2, City, State, Country, PostalCode, MiddleName, MembershipExpieryDate FROM Customers WHERE EmailAddress = @emailAddress
SET @status =1
COMMIT
END TRY
BEGIN CATCH
IF(@@TRANCOUNT > 0)
BEGIN
ROLLBACK TRANSACTION initialP
END
SET @status =2
END CATCH
这是我用来获取查询数据的代码:
ObjectParameter status = new ObjectParameter("status", typeof(int));
var dataSet = Entity.CreateCustomer(newCustomer.FirstName, newCustomer.LastName,
newCustomer.EmailAddress, newCustomer.ContactNumber, newCustomer.Street1, newCustomer.Street2,
newCustomer.City, newCustomer.State, newCustomer.Country, newCustomer.PostalCode, newCustomer.MiddleName,
newCustomer.Password, newCustomer.MembershipExpieryDate, status);
但是当执行此存储过程时,它会给出一个错误,指出Trans count不匹配。
据我所知trans count
在begin transaction
出现时被commit
所取代。当CREATE PROCEDURE dbo.TestForCommit
(
@test11 int,
@test111 int,
@status int OUTPUT
)
AS
begin try
declare @test1 uniqueidentifier = NEWID()
begin transaction
insert into TestT1(Test1, Test11, Test111) values(@test1, @test11, @test111)
commit
select Test1, Test11, Test111 from TestT1 where Test1 = @test1
SET @status = 1
end try
begin catch
Rollback Transaction
set @status =2
end catch
/* SET NOCOUNT ON */
RETURN
出现时,由{1}进行描述。
我已经在函数导入中映射了该函数,因为它可以返回复杂类型。
我无法理解这个问题。对我来说似乎很好。我想知道天气我这样做是否正确与一些细节。感谢。
编辑: 这是我通过编写示例代码发现的。我首先想到的是开始跨行动和提交。但事实并非如此。 它允许选择甚至在提交语句之前插入的值(如果我错了,请纠正我。)。 我创建了一个包含3列的测试表(test1唯一标识符,test2 int,test3 int) 并且我创建了一个存储过程调用Test: 其内容如下:
ObjectParameter status = new ObjectParameter("status", typeof(int));
SchoolEntities test = new SchoolEntities();
var dataSet = test.TestForCommit(1, 1, status);
//A:
//(XXXXXXXXXXXXX)Console.WriteLine((int)status.Value);
foreach (var item in dataSet)
{
TestForCommit_Result t = item;
Console.WriteLine("{0} , {1} , {2}", t.Test1, t.Test11, t.Test111);
}
//B:
Console.WriteLine((int)status.Value);
Console.ReadLine();
我在实体模型中创建了一个功能导入,我就像这样使用它:
{{1}}
此// A:部分导致问题。这给出了一个空引用异常。但是// B:部分工作并给出状态值。我仍然在寻找它为什么这样工作。谢谢! 的
答案 0 :(得分:1)
Begin Try
Set NoCount On
Set Xact_Abort On
Begin Tran
--Your Query
Commit Tran
End Try
Begin Catch
Rollback Tran
End Catch
CREATE PROCEDURE [dbo].[CreateCustomer]
@firstName nvarchar(50) = null,
@lastName nvarchar(50) = null,
@emailAddress nvarchar(50),
@contactNumber varchar(15) =null,
@street1 nvarchar(50) = null,
@street2 nvarchar(50) = null,
@city nvarchar(50) = null,
@State nvarchar(50)= null,
@Country nvarchar(50)=null,
@postalCode varchar(5) = null,
@middleName nvarchar(50),
@password nvarchar(50),
@membershipExpieryDate datetime,
@status int OUT
AS
BEGIN TRY
Set NoCount ON
Set XAct_Abort ON
DECLARE @lastAccessed datetime = GETDATE()
DEClARE @uniqueCustomerID UNIQUEIDENTIFIER= NEWID()
BEGIN TRANSACTION
INSERT INTO Customers(CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber,
Street1, Street2, City, State, Country, PostalCode, MiddleName, Password, MembershipExpieryDate)
VALUES(@uniqueCustomerID,@firstName, @lastName, @emailAddress, @lastAccessed, @contactNumber, @street1, @street2,
@city, @State, @Country, @postalCode, @middleName, @password, @membershipExpieryDate)
INSERT INTO Roles(CustomersCustomerID,Role) VALUES(@uniqueCustomerID,'V')
COMMIT
SELECT CustomerID, FirstName, LastName, EmailAddress, LastAccessed, ContactNumber,
Street1, Street2, City, State, Country, PostalCode, MiddleName, MembershipExpieryDate FROM Customers WHERE EmailAddress = @emailAddress
SET @status =1
COMMIT Tran
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
SET @status =2
END CATCH