2

我在下面的存儲過程中寫了這個,並得到不正確的語句。用於計數動態表項的MS SQL存儲過程

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 [email protected] 
     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 [email protected] 

     --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

您應該使用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 
+0

我猜我遲到了:) – 2012-07-21 13:28:57

+0

優秀的@Martin Smith。你能解釋一下EXEC的代碼嗎? – user1542653 2012-07-21 13:50:57

+0

我想我從這http://msdn.microsoft.com/en-us/library/ms175170%28v=sql.105%29.aspx – user1542653 2012-07-21 15:13:20

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 

這是很麻煩的,但一個或其他不工作。

+0

如果嘗試Select語句而不是insert into語句會發生什麼?你能解釋一下嗎? – user1542653 2012-07-21 13:49:17

+0

它不起作用。您可以將exec語句的輸出插入到表中。你不能從中選擇。 (對於select,SQL Server在編譯查詢時需要知道表的結構,它沒有exec語句的這些信息。) – 2012-07-21 14:21:48

0

試試這個one.It犯規查詢的實際表,但會爲您提供計數的rows.This是更好的辦法,如果你有大的表,你需要大概計數。

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 
+0

得到的解釋謝謝。這也有幫助。 – user1542653 2012-07-21 14:19:57