我有一个以xml作为输入的存储过程。
执行SP的C#代码是
using (SqlConnection conn = new SqlConnection(connString))
{
try
{
conn.Open();
SqlCommand command = new SqlCommand("sp_Configure_Users", conn);
command.CommandType = CommandType.StoredProcedure;
var xmlData = new SqlParameter(parameterName: "@XMLDATA", value: GenerateXML(_userDetails));
command.Parameters.Add(xmlData);
count = command.ExecuteNonQuery();
if (count > 0)
{
return count;
}
else
{
return 0;
}
}
catch (SqlException )
{
}
}
方法GenerateXML返回一个xml数据形式的字符串。在我的存储过程中,我删除了SET NOCOUNT ON,以便在插入,删除或更新时返回实际的行数。如果我运行我的查询示例xml数据: -
<UserCollection>
<InsertList><Users User_Id="438" First_Name="Praveen" Middle_Name="" Last_Name="Kumar" Designation_Id="1" Email="" Contact_Number="96533" Updated_By="pkumar" />
</InsertList>
<UpdateList></UpdateList>
<DeleteList></DeleteList>
</UserCollection>
返回的值是3而不是1,因为在我的集合中我只传递了一行需要插入的行。因为没有更新或删除的行。如果必须返回SP,SP如何返回3 1 ??
存储的Proc是
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_Configure_Users]
@XMLDATA xml
AS
DECLARE @Users_Staging Table(
[User_Id] [int] NOT NULL,
[First_Name] [varchar](200) NOT NULL,
[Middle_Name] [varchar](200) NULL,
[Last_Name] [varchar](200) NOT NULL,
[Designation_Id] [int] NOT NULL,
[Email] [varchar](250) NOT NULL,
[Contact_Number] [varchar](50) NOT NULL,
[Updated_By] [varchar](255) NOT NULL
)
BEGIN
Set nocount off
Go
BEGIN TRY
BEGIN TRANSACTION
DECLARE @CurrDate datetime;
SELECT @CurrDate = GETDATE();
-- Insert statements for procedure
INSERT INTO Users
([User_Id]
,[First_Name]
,[Middle_Name]
,[Last_Name]
,[Designation_Id]
,[Email]
,[Contact_Number]
,[Updated_By]
,[Updated_Date]
)
Select
XMLDATA.item.value('@User_Id[1]', 'int') AS User_Id,
XMLDATA.item.value('@First_Name[1]', 'varchar(200)') AS First_Name,
XMLDATA.item.value('@Middle_Name[1]', 'varchar(200)') AS Middle_Name,
XMLDATA.item.value('@Last_Name[1]', 'varchar(200)') AS Last_Name,
XMLDATA.item.value('@Designation_Id[1]', 'int') AS Designation_Id,
XMLDATA.item.value('@Email[1]', 'varchar(250)') AS Email,
XMLDATA.item.value('@Contact_Number[1]', 'varchar(50)') AS Contact_Number,
XMLDATA.item.value('@Updated_By[1]', 'varchar(255)') AS Updated_By,
@CurrDate
FROM @XMLDATA.nodes('//UserCollection/InsertList/Users') AS XMLDATA(item)
-- Update statements for procedure
INSERT INTO @Users_Staging
([User_Id]
,[First_Name]
,[Middle_Name]
,[Last_Name]
,[Designation_Id]
,[Email]
,[Contact_Number]
,[Updated_By])
Select
XMLDATA.item.value('@User_Id[1]', 'int') AS User_Id,
XMLDATA.item.value('@First_Name[1]', 'varchar(200)') AS First_Name,
XMLDATA.item.value('@Middle_Name[1]', 'varchar(200)') AS Middle_Name,
XMLDATA.item.value('@Last_Name[1]', 'varchar(200)') AS Last_Name,
XMLDATA.item.value('@Designation_Id[1]', 'int') AS Designation_Id,
XMLDATA.item.value('@Email[1]', 'varchar(250)') AS Email,
XMLDATA.item.value('@Contact_Number[1]', 'varchar(50)') AS Contact_Number,
XMLDATA.item.value('@Updated_By[1]', 'varchar(255)') AS Updated_By
FROM @XMLDATA.nodes('//UserCollection/InsertList/Users') AS XMLDATA(item)
UPDATE Users
SET First_Name=stgusr.First_Name,
Middle_Name=stgusr.Middle_Name,
Last_Name=stgusr.Last_Name,
Email=stgusr.Email,
Contact_Number=stgusr.Contact_Number,
Updated_By=stgusr.Updated_By,
Updated_Date=@CurrDate
FROM Users usr INNER JOIN
@Users_Staging stgusr
ON usr.User_Id=stgusr.User_Id
-- Delete statements for procedure
DELETE Users
WHERE User_Id
IN (Select
XMLDATA.item.value('@User_Id[1]', 'int') AS User_Id
FROM @XMLDATA.nodes('//UserCollection/DeleteList/Users') AS XMLDATA(item))
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
--EXEC sp_rethrow_error;
ROLLBACK TRANSACTION;
END CATCH;
END
答案 0 :(得分:1)
如果存储过程更新/插入的表上有触发器,它们将影响受影响的行数:
对于UPDATE,INSERT和DELETE语句,返回值为 受命令影响的行数。当a上存在触发器时 正在插入或更新的表,返回值包括数字 受插入或更新操作影响的行数和数字 受触发器或触发器影响的行数。对于所有其他类型的 语句,返回值为-1。如果发生回滚,则返回 值也是-1 Ref
建议您发布所涉及的表的存储过程和模式/触发器。