插入后@@ rowcount没有给出正确的值

时间:2014-10-31 04:45:28

标签: sql sql-server-2012 rowcount

我使用此查询在表tblWorkOrders中插入一行。 但是@@ ROWCOUNT在表中插入一行时会给出0。 可能是什么原因?如何更改查询以提供正确的@@ ROWCOUNT?

INSERT INTO tblWorkOrders
        (           WO_Type , WO_Yard , WO_Operation , WO_Source , WO_SourceID , WO_SourceXLocation , WO_SourceYLocation , 
                    WO_Destination , WO_DestinationID , WO_DestinationXLoc, WO_DestinationYLoc , WO_Crane , WO_Priority , 
                    WO_ScheduledAt , WO_CoilNo , WO_ExecutionStart , WO_ExecutionEnd , WO_ExecutedBy , WO_Remarks,WO_RequestRefID )

        ( SELECT    OM_WOType,OM_Yard,OM_OperationsID,'Saddle' ,STARTINGPOINT.NAME,STARTINGPOINT.XAxis,STARTINGPOINT.YAxis,
                    'Saddle',ENDINGPOINT.NAME,ENDINGPOINT.XAxis,ENDINGPOINT.YAxis,OM_AssignedCrane,NULL,
                    NULL,@COILNO,NULL,NULL,NULL,NULL,NULL
                    FROM 

                    tblOperationsMaster OPM

                    INNER JOIN 
                    (SELECT SP_OperatingPointID AS [NAME],SP_XLocation AS [XAxis],SP_YLocation AS [YAxis] FROM tblStandardPoints
                    UNION ALL
                    SELECT SM_SaddleID AS [NAME],SM_XCoordinate AS [XAxis],SM_YCoordinate AS [YAxis] FROM tblSaddleMaster) STARTINGPOINT
                    ON STARTINGPOINT.NAME = @SOURCEIDFROMCALLINGFN

                    INNER JOIN 
                    (SELECT SP_OperatingPointID AS [NAME],SP_XLocation AS [XAxis],SP_YLocation AS [YAxis] FROM tblStandardPoints
                    UNION ALL
                    SELECT SM_SaddleID AS [NAME],SM_XCoordinate AS [XAxis],SM_YCoordinate AS [YAxis] FROM tblSaddleMaster) ENDINGPOINT 
                    ON ENDINGPOINT.NAME = @DESTINATIONIDFROMCALLINGFN

                    WHERE OPM.OM_OperationsID = @OPERATIONID)

        SET @ROWSINSERTED = @@ROWCOUNT;

3 个答案:

答案 0 :(得分:2)

嗨,你会得到rowcount

SELECT @ROWSINSERTED =@@ROWCOUNT

希望有所帮助

答案 1 :(得分:1)

试试这个,

我提供了示例检查。

插入后使用此功能。

SELECT @ROWSINSERTED=@@ROWCOUNT
SELECT @ROWSINSERTED AS Rows

答案 2 :(得分:1)

INSERT INTO tblWorkOrders
        (           WO_Type , WO_Yard , WO_Operation , WO_Source , WO_SourceID , WO_SourceXLocation , WO_SourceYLocation , 
                    WO_Destination , WO_DestinationID , WO_DestinationXLoc, WO_DestinationYLoc , WO_Crane , WO_Priority , 
                    WO_ScheduledAt , WO_CoilNo , WO_ExecutionStart , WO_ExecutionEnd , WO_ExecutedBy , WO_Remarks,WO_RequestRefID )

        ( SELECT    OM_WOType,OM_Yard,OM_OperationsID,'Saddle' ,STARTINGPOINT.NAME,STARTINGPOINT.XAxis,STARTINGPOINT.YAxis,
                    'Saddle',ENDINGPOINT.NAME,ENDINGPOINT.XAxis,ENDINGPOINT.YAxis,OM_AssignedCrane,NULL,
                    NULL,@COILNO,NULL,NULL,NULL,NULL,NULL
                    FROM 

                    tblOperationsMaster OPM

                    INNER JOIN 
                    (SELECT SP_OperatingPointID AS [NAME],SP_XLocation AS [XAxis],SP_YLocation AS [YAxis] FROM tblStandardPoints
                    UNION ALL
                    SELECT SM_SaddleID AS [NAME],SM_XCoordinate AS [XAxis],SM_YCoordinate AS [YAxis] FROM tblSaddleMaster) STARTINGPOINT
                    ON STARTINGPOINT.NAME = @SOURCEIDFROMCALLINGFN

                    INNER JOIN 
                    (SELECT SP_OperatingPointID AS [NAME],SP_XLocation AS [XAxis],SP_YLocation AS [YAxis] FROM tblStandardPoints
                    UNION ALL
                    SELECT SM_SaddleID AS [NAME],SM_XCoordinate AS [XAxis],SM_YCoordinate AS [YAxis] FROM tblSaddleMaster) ENDINGPOINT 
                    ON ENDINGPOINT.NAME = @DESTINATIONIDFROMCALLINGFN

                    WHERE OPM.OM_OperationsID = @OPERATIONID)
DECLARE @Rows int
SELECT @Rows=@@ROWCOUNT
SELECT @Rows AS Rows,@@ROWCOUNT AS [ROWCOUNT]

尝试这个我觉得它会起作用