显示从现有表插入表中的行数

时间:2015-02-23 19:14:10

标签: sql-server select stored-procedures insert count

请使用带存储过程的Microsoft SQL Server 2012显示插入存档表的行数。我的代码显示如下。我运行代码成功,但调用存储过程,我收到错误消息说

  

Msg 8114,Level 16,State 5   过程p_moveTransHisttoArchive3a,第0行将数据类型varchar转换为datetime时出错。

这是我的代码:

IF OBJECT_ID(N'Production.p_moveTransHisttoArchive3a', N'P') IS NOT NULL
    DROP PROCEDURE Production.p_moveTransHisttoArchive3a;
GO

CREATE PROCEDURE Production.p_moveTransHisttoArchive3a
    @FiveDaysAgo    DATETIME
AS
    SET NOCOUNT ON;

BEGIN
        DECLARE @counter INT;
        SET @counter = @@ROWCOUNT
        INSERT INTO Production.TransactionHistoryArchive
            SELECT*
            FROM Production.TransactionHistory
            WHERE  TransactionDate <= @FiveDaysAgo
            RETURN @counter --No of rows moved
END;
GO

1 个答案:

答案 0 :(得分:1)

这与你想要的东西很接近。为什么您的入站参数名为FiveDaysAgo?如果您希望始终获得超过五天的行,这些行属于proc,而不是参数。

CREATE PROCEDURE Production.p_moveTransHisttoArchive3a
    @FiveDaysAgo    DATETIME 
    , @RowCount int OUTPUT
AS
    set nocount on;

    INSERT INTO Production.TransactionHistoryArchive
    (
        Col1
        , Col2
    )
    SELECT Col1
        , Col2
    FROM Production.TransactionHistory
    WHERE  TransactionDate <= @FiveDaysAgo

    select @RowCount = @@ROWCOUNT