用于计算动态表项的MS SQL存储过程

时间:2012-07-21 12:49:47

标签: sql sql-server sql-server-2008 stored-procedures stored-functions

我在下面的存储过程中写了这个并且得到了错误的声明。

ALTER PROCEDURE dbo.[Counter]
        @TableName VARCHAR(100)
    AS 
        BEGIN
            DECLARE @Counter INT
            DECLARE @SQLQ VARCHAR(200)
            SET NOCOUNT ON;

        --SET @TableName = 'Member';
        SET @SQLQ = 'SELECT COUNT(*) FROM dbo.[' + @TableName + ']';
        --Preparing the above sql syntax into a new statement(get_counter).

            --Getting an error here I had googled the prepare statement but don't know why facing this error.
        PREPARE get_counter FROM @SQLQ;
    @Counter = EXEC get_counter; -- here @resutl gets the value of the count.@TableName
        DEALLOCATE PREPARE get_counter; -- removing the statement from the memory.


    END

然后我又写了一篇:

ALTER PROCEDURE dbo.[Counter]
    @TableName VARCHAR(100)
AS 
    BEGIN
        DECLARE @Counter INT
        DECLARE @SQLQ VARCHAR(200)
        SET NOCOUNT ON;

        --SET @TableName = 'Member';
        SET @SQLQ = 'SELECT COUNT(*) FROM dbo.[' + @TableName + ']';
        --Preparing the above sql syntax into a new statement(get_counter).


    Execute @SQLQ; -- here @resutl gets the value of the count.@TableName

        --DEALLOCATE PREPARE get_counter; -- removing the statement from the memory.
        Return @Counter;

    END

它运行正常,但我无法在计数器中得到结果,任何人都请帮助我(我知道我没有为计数器分配任何值,但如果我这样做,我会收到错误)。

在你回答马丁之后我现在用你的代码替换了我的代码:

ALTER PROCEDURE dbo.[Counter] @SchemaName SYSNAME = 'dbo' , @TableName  SYSNAME

AS
  BEGIN
      SET NOCOUNT ON;

      DECLARE @SQLQ NVARCHAR(1000)
      DECLARE @Counter INT;

      SET @SQLQ = 'SELECT @Counter = COUNT(*) FROM ' + 
       Quotename(@SchemaName) + '.' + Quotename(@TableName);

      EXEC sp_executesql
        @SQLQ ,
        N'@Counter INT OUTPUT',
        @Counter = @Counter OUTPUT

      Return   SELECT @Counter
  END 

现在我找回了它。

  ALTER PROCEDURE dbo.[CreateBusinessCode]
    @MemberID                               bigint,
    @len                                        int,
    @RewardAccountID                bigint,
    @ConnectionStatusID         tinyint,
    @Assign                                 smalldatetime

AS
    BEGIN
    SET NOCOUNT ON;
    DECLARE @counter INT

    EXEC @counter = dbo.Counter 'dbo','member';

    Select @counter;


    END

3 个答案:

答案 0 :(得分:3)

您应该使用SYSNAME作为对象标识符和Quotename,而不是自己连接方括号。

ALTER PROCEDURE dbo.[Counter] @TableName  SYSNAME,
                              @SchemaName SYSNAME = 'dbo'
AS
  BEGIN
      SET NOCOUNT ON;

      DECLARE @SQLQ NVARCHAR(1000)
      DECLARE @Counter INT;

      SET @SQLQ = 'SELECT @Counter = COUNT(*) FROM ' + 
       Quotename(@SchemaName) + '.' + Quotename(@TableName);

      EXEC sp_executesql
        @SQLQ ,
        N'@Counter INT OUTPUT',
        @Counter = @Counter OUTPUT

         SELECT @Counter
  END 

答案 1 :(得分:0)

在SQL Server中,如果要将结果输入变量,则有两种选择。

首先是使用游标。

第二个是动态SQL:

declare @sql varchar(max) = whatever;
declare @cnt int;

declare @cntTable table as (cnt int);

insert into @cntTable
    exec(@sql);

select @cnt = t.cnt
from @cntTable

这很麻烦,但其中一个确实有效。

答案 2 :(得分:0)

尝试这个。它不会查询实际的表,但会为你提供行数。如果你有大表并且需要近似计数,这是更好的方法。

ALTER PROCEDURE dbo.[Counter] 
    @TableName VARCHAR(100) 
AS  
begin

declare @objectid int,@counter int
select @objectid = object_id from sys.all_objects where name = @tablename and schema_id=SCHEMA_ID('dbo')
select @counter = sum(rows) from sys.partitions where object_id= @objectid
and index_id in (0,1)
select @counter
end
go