想读取每个表中的记录总数储存语句:ALTER PROCEDURE [dbo].[Get_Count] 
@ID nvarchar(50), --表的主键
@dtable nvarchar(100)  --表名
ASDECLARE @TempID int
SELECT @TempID = count(@ID) FROM [@dtable]
IF @TempID = 0
RETURN 0
ELSE
RETURN @TempID但每次提示:对象名  '@dtable' 无效。 把[@dtable]  改成表名就可以正常

解决方案 »

  1.   

    去掉 sql语句运行不成功
    必须声明表变量 "@dtable"。
      

  2.   

    可以拼sql语句,再执行
    declare @str  varchar(5000);
    set @str='DECLARE @TempID int;';
    set @str=@str+'SELECT @TempID = count('+@id+') FROM ['+@dtable+'];';
    set @str=@str+'IF @TempID = 0 ';
    set @str= @str+' RETURN 0;ELSE RETURN @TempID;';
    exec @str;
      

  3.   


    ALTER PROCEDURE [dbo].[Get_Count]  
    @ID nvarchar(50), --表的主键
    @dtable nvarchar(100) --表名
    ASDECLARE @sql nvarchar(400)
    set @sql=N'select count( '+ @ID +') from ' + @dtable
    declare @count int;
    exec sp_executesql @sql,'@count int output',@count output
    if @count<0
    begin
    print '无记路'
    end
    else
    begin
    print @count
    end
    ps:在表名 或字段名作变量时候  要拼接语句 执行   要得到执行的结果  需调用sp_executesql
      

  4.   


    ALTER PROCEDURE [dbo].[Get_Count]  
    @ID nvarchar(50), --表的主键
    @dtable nvarchar(100) --表名
    ASexec'(SELECT count('+@ID+') FROM ['+@dtable+']'
      

  5.   

    过程需要类型为 'ntext/nchar/nvarchar' 的参数 '@parameters'。
      

  6.   


    return exec('SELECT count('+@ID+') FROM ['+@dtable+']') 如果这样的话  返回值为0
      

  7.   


    你执行的时候 
    exec Get_Count 你要查询的列的名字,表的名字
    例如 exec Get_Count ID,table1
      

  8.   


    你执行的时候 
    exec Get_Count 你要查询的列的名字,表的名字
    例如 exec Get_Count ID,table1
      

  9.   


    你执行的时候 
    exec Get_Count 你要查询的列的名字,表的名字
    例如 exec Get_Count ID,table1
      

  10.   

    create PROCEDURE [dbo].[Get_Count]  
    @ID nvarchar(50), --表的主键
    @dtable nvarchar(100) --表名
    AS
    exec('SELECT count(['+@ID+']) FROM ['+@dtable+']')select number,type into TB from master..spt_values exec [dbo].[Get_Count] 'type','TB'
    drop proc [dbo].[Get_Count]
    drop table TB
      

  11.   

    我给楼主测试了 直接用把
    修改存储过程ALTER PROCEDURE [dbo].[Get_Count]  
    @ID nvarchar(50), --表的主键
    @dtable nvarchar(100) --表名
    ASDECLARE @sql nvarchar(400)
    set @sql=N'select count( '+ @ID +') from ' + @dtable
    declare @count int;
    exec sp_executesql @sql,N'@count int output',@count output
    if @count<0
    begin
    print '无记路'
    end
    else
    begin
    print @count
    end建立测试表CREATE TABLE tb_Menu(
    id int NULL,
            parentid int NULL,
    menuname varchar (50) 

    测试存储过程exec Get_Count 'id',tb_Menu
    执行结果
    5
      

  12.   

     我刚才那个地方调用sp_executesql的时候 确实有点问题 给你修改好了  你直接用15楼的吧  我测试过了 没问题了
    好了  楼主结贴吧 ~
      

  13.   


    这些语句都是写在 sql 数据库里面程序里面只传递2个对应的参数@ID nvarchar(50), --表的主键
    @dtable nvarchar(100) --表名
     
    public static int Getcount(string FieldName, string TableName)
            {
                int rowsAffected;
                SqlParameter[] parameters = {
                                  new SqlParameter("@ID", SqlDbType.NVarChar,50),
                                  new SqlParameter("@dtable", SqlDbType.NVarChar,100)};
                parameters[0].Value = FieldName;
                parameters[1].Value = TableName;
                return DbHelperSQL.RunProcedure("Get_Count", parameters, out rowsAffected);
                
            }
    怎么调用啊?
      

  14.   


    using System.Data;
    using System.SqlClient;
    public static int Getcount(string FieldName, string TableName)
            {
               
                SqlParameter[] parameters = {
                                  new SqlParameter("@ID", SqlDbType.NVarChar,50),
                                  new SqlParameter("@dtable", SqlDbType.NVarChar,100)};
                parameters[0].Value = FieldName;
                parameters[1].Value = TableName;
                    SqlConnection conn=new SqlConnection("你的数据库连接字符串");            
    SqlCommand cmd=new SqlCommand("Get_Count",conn);
    cmd.CommandType=CommandType.StoreProcedure;
    return cmd.ExecuteScalar();
    conn.Close();
            
            }pS;你用的那个类库我不知道里面啥结构 所以就用我写的这个吧
      

  15.   

    不对 最后可能还需要强制转换下using System.Data;
    using System.SqlClient;
    public static int Getcount(string FieldName, string TableName)
            {
               
                SqlParameter[] parameters = {
                                  new SqlParameter("@ID", SqlDbType.NVarChar,50),
                                  new SqlParameter("@dtable", SqlDbType.NVarChar,100)};
                parameters[0].Value = FieldName;
                parameters[1].Value = TableName;
                    SqlConnection conn=new SqlConnection("你的数据库连接字符串");            
    SqlCommand cmd=new SqlCommand("Get_Count",conn);
    cmd.CommandType=CommandType.StoreProcedure;
    int result=Convert.ToInt32(cmd.ExecuteScalar());
    conn.Close();
        return result;    
            }
      

  16.   

    终于好了ALTER PROCEDURE [dbo].[Get_Count]  
    @ID nvarchar(50), --表的主键
    @dtable nvarchar(100) --表名
    AS Declare @sql nvarchar(100)   begin 
             declare @TempID int 
             set @sql=N'select @n=count('+@ID+')  from '+@dtable
             exec sp_executesql @sql,N'@n int output',@n=@TempID output 
              if @TempID=0
          return 0
             else
          return @TempID
      end