我写的存储过程大致如下:
@recordcount 为输出参数
begin
SET @sql="select SELECT @recordcount=count(*) FROM table"
EXEC sp_executesql @sql,N'@recordcount int OUTPUT',@recordcount OUTPUT  --语句一
set @sql="select * from table"
exec EXEC(@sql)
end
(1)我用sqldatareader.executeReader()之后,输出参数@recordcount并没用被相应的赋值;
但是第二句的exec EXEC(@sql)有正确输出;(2)我删除了了第二条语句---set @sql=select * from table exec EXEC(@sql)
再次用 sqldatareader.executeReader()的时候能够正确返回@recordCount的值;请问怎样才能获得输出参数的值和后面的一组数据;

解决方案 »

  1.   

    有两种方法,第一种是begin 
    set @sql="select * from table" 
    exec EXEC(@sql)
    return(@@rowcount)
    end @@rowcount 就是记录总数第二种@recordcount int OUTPUT  为输出参数 
    begin 
    .....
      

  2.   

    我这里是执行了两条@sql语句(不一样的);
    @rowcount是作为第一条语句返回的总记录数返回:;
    然后我又执行了第二条sql(这一条返回一组记录);
      

  3.   

    抱歉,没太注意,我把上面的两条语句写成一样了;
    我是两次执行两条不同语句,第一条是给@recordCount赋值;
      

  4.   

    你要关闭sqldatareader对像后才能取输出参数的值,因为存储过程返回一个datareader对像实际返回的是一个指针,在这个时候输出参数还没有返回.
    using (SqlConnection conn = new SqlConnection("uid=sa;password=密码;database=数据库名;server=127.0.0.1"))
    {
        try
        {
            int coun = 0;
            conn.Open();
            SqlCommand comm = new SqlCommand("getTable",conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.Add(new SqlParameter("@recordcount",
                                                SqlDbType.Int,
                                                0,
                                                ParameterDirection.Output,
                                                false,
                                                0,
                                                0,
                                                "",
                                                DataRowVersion.Default,
                                                null));
            comm.Parameters["@recordcount"].Value = 0;
            SqlDataReader dr = comm.ExecuteReader();
            string temp = "";
            while (dr.Read())
            {
                //读数据
            }
            dr.Close();   //要在这之后取存储过程的输出参数
            coun = (int)comm.Parameters["@recordcount"].Value;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
      

  5.   

    建议存储过程改动一下,以下代码利用@@rowcount可减少一次查询.CREATE PROC getTable
        @RecordCount int OUTPUT
    as
       /*定义局部变量*/
         declare @TmpSelect          NVarchar(600)
       /*关闭计数*/
      set nocount on  select @TmpSelect = 'select * from table'
      execute  sp_executesql @TmpSelect
      select @RecordCount=@@rowcountGO
      

  6.   

    我是两次执行两条不同语句,涉及的是两个不同的表(再次抱歉),
    第一条是给select @recordCount=Count(*)from table2 赋值;
    第二:select * from table1 ;
    还有我有关闭sqldatareader,然后才读输出参数。
    我目前还发现用ExecuteScalar可以单独获取@RecordCount 的值;
    so 我现在是读了两次(凑合着先):先用sqldatareader 后用ExecuteScalar,哎,无奈
      

  7.   

    存储过程如下,已上机测试过了
    [code=SQL]
    CREATE PROC getTable
           @RecordCount int OUTPUT
    as
       /*定义局部变量*/
         declare @intRootRecordCount int
         declare @TmpSelect          NVarchar(600)
       /*关闭计数*/
       set nocount on
       
       select @TmpSelect = 'set nocount on;select @SPintRootRecordCount = count(*) from area'
       execute sp_executesql 
                 @TmpSelect,
                 N'@SPintRootRecordCount int OUTPUT',
                 @SPintRootRecordCount=@RecordCount OUTPUT  select @TmpSelect = 'select * from users'
      execute  sp_executesql @TmpSelect
    GO[/SQL]
      

  8.   

    抱歉,格式不对(该死的Firefox),重新贴一次.CREATE PROC getTable
           @RecordCount int OUTPUT
    as
       /*定义局部变量*/
         declare @intRootRecordCount int
         declare @TmpSelect          NVarchar(600)
       /*关闭计数*/
       set nocount on
       
       select @TmpSelect = 'set nocount on;select @SPintRootRecordCount = count(*) from area'
       execute sp_executesql 
                 @TmpSelect,
                 N'@SPintRootRecordCount int OUTPUT',
                 @SPintRootRecordCount=@RecordCount OUTPUT  select @TmpSelect = 'select * from users'
      execute  sp_executesql @TmpSelect
    GO