从存储过程T-SQL返回rowcount

时间:2015-03-31 07:01:48

标签: sql tsql stored-procedures rowcount

您好我有一个agentjob检查要调用的存储过程。并将XMLData从表发送到存储过程。这个例子

ELSE IF(@Entity = 'Payment') 
BEGIN
EXEC dbo.Payment @output output, @XmlDocument
END

XmlDocument工作正常,但我想检查存储过程中的rowcount。但正如你可以看到它以这种方式工作,它说"程序有太多的参数指定"。 我需要帮助才能从存储过程中恢复此变量。提前致谢!

create procedure dbo.Payment (@output int output, @XmlDocument XML)
as
begin
Create Table #TempTable(IntComp VARCHAR(10), PaymentCode VARCHAR(10), PaymentDays int, [Text] VARCHAR(30), FreeMonth tinyint, PaymentCode2 VARCHAR(10))
Declare @Identity as INT
SET @Identity = (SELECT max ((etPayment.ID) + 1) from dbo.etPayment)
IF @Identity IS NULL
    SET @Identity = 1

BEGIN TRY
WITH XMLNAMESPACES('http://bla.bla.Payment' AS ns0)
INSERT INTO #TempTable (IntComp, PaymentCode, PaymentDays, [Text], FreeMonth, PaymentCode2)     
            SELECT 
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/IntComp[1]', 'VARCHAR(10)') IntComp,
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/PaymentCode[1]', 'VARCHAR(10)') PaymentCode,
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/PaymentDays[1]', 'int') PaymentDays,
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/Text[1]', 'VARCHAR(30)') [Text],
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/FreeMonth[1]', 'tinyint') FreeMonth,
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/PaymentCode2[1]', 'VARCHAR(10)') PaymentCode2


MERGE dbo.etPayment AS Target
USING 
(

SELECT 
IntComp,
PaymentCode,
PaymentDays,
[Text],
FreeMonth,
PaymentCode2
FROM #TempTable) AS Source
ON (Target.IntComp = Source.IntComp and Target.PaymentCode = Source.PaymentCode)
WHEN MATCHED 
THEN UPDATE SET 
Target.PaymentDays = Source.PaymentDays,
Target.[Text] = Source.[Text],
Target.FreeMonth = Source.FreeMonth,
Target.PaymentCode2 = Source.PaymentCode2

WHEN NOT MATCHED BY TARGET 
THEN INSERT(
ID,
IntComp,
PaymentCode,
PaymentDays,
[Text],
FreeMonth,
PaymentCode2
)

VALUES(
@Identity,
Source.IntComp,
Source.Paymentcode,
Source.PaymentDays,
Source.Text,
Source.FreeMonth,
Source.PaymentCode2
);
END TRY
BEGIN CATCH
SET @output = @@ROWCOUNT
SELECT @output
END CATCH
DROP TABLE #TempTable;
SET @output = @@ROWCOUNT 
SELECT @output


end

1 个答案:

答案 0 :(得分:1)

  

我需要帮助从存储过程中获取此变量。

您需要在SET @output = @@ROWCOUNT语句之后移动MERGE并删除设置变量@output的其他语句。这样的事情。

http://sqlfiddle.com/#!6/99e0b/5

create procedure dbo.Payment (@output int output, @XmlDocument XML)
as
begin
Create Table #TempTable(IntComp VARCHAR(10), PaymentCode VARCHAR(10), PaymentDays int, [Text] VARCHAR(30), FreeMonth tinyint, PaymentCode2 VARCHAR(10))
Declare @Identity as INT
SET @Identity = (SELECT max ((etPayment.ID) + 1) from dbo.etPayment)
IF @Identity IS NULL
    SET @Identity = 1

BEGIN TRY
WITH XMLNAMESPACES('http://bla.bla.Payment' AS ns0)
INSERT INTO #TempTable (IntComp, PaymentCode, PaymentDays, [Text], FreeMonth, PaymentCode2)     
            SELECT 
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/IntComp[1]', 'VARCHAR(10)') IntComp,
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/PaymentCode[1]', 'VARCHAR(10)') PaymentCode,
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/PaymentDays[1]', 'int') PaymentDays,
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/Text[1]', 'VARCHAR(30)') [Text],
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/FreeMonth[1]', 'tinyint') FreeMonth,
                    @xmldocument.value('/ns0:Payment[1]/Payment[1]/PaymentCode2[1]', 'VARCHAR(10)') PaymentCode2;


MERGE dbo.etPayment AS Target
USING 
(

SELECT 
IntComp,
PaymentCode,
PaymentDays,
[Text],
FreeMonth,
PaymentCode2
FROM #TempTable) AS Source
ON (Target.IntComp = Source.IntComp and Target.PaymentCode = Source.PaymentCode)
WHEN MATCHED 
THEN UPDATE SET 
Target.PaymentDays = Source.PaymentDays,
Target.[Text] = Source.[Text],
Target.FreeMonth = Source.FreeMonth,
Target.PaymentCode2 = Source.PaymentCode2

WHEN NOT MATCHED BY TARGET 
THEN INSERT(
ID,
IntComp,
PaymentCode,
PaymentDays,
[Text],
FreeMonth,
PaymentCode2
)

VALUES(
@Identity,
Source.IntComp,
Source.Paymentcode,
Source.PaymentDays,
Source.Text,
Source.FreeMonth,
Source.PaymentCode2
);
SET @output = @@ROWCOUNT;
END TRY
BEGIN CATCH
SET @output = 0;
SELECT @output
END CATCH
DROP TABLE #TempTable;
end

您可以像这样调用此过程并获取@output

DECLARE @output int,@XmlDocument XML= '<?xml version="1.0" encoding="UTF-8"?>
<ns0:Payment  xmlns:ns0="http://bla.bla.Payment">
    <Payment>
        <IntComp>asasc</IntComp>
        <PaymentCode>P</PaymentCode>
        <PaymentDays>20</PaymentDays>
        <FreeMonth>1</FreeMonth>
        <PaymentCode2>P</PaymentCode2>
        <Text>TEXT</Text>
        </Payment>
    </ns0:Payment>'

EXEC dbo.Payment @output output,@XmlDocument

SELECT @output as output,* FROM etPayment;