请教个问题,我用c#调用存储过程来取值。
存储过程中定义了三个类似declare @sql varchar(8000)的变量 ,
然后三个 set @sql='select 。'
最后用exec(@sql)取值,
因为表比较大,所以varchar(8000)放不下,就分成三个这样的语句来查询一个表(动态获取的字段名,通过colid来分开),
但是这样返回的结果集也成了三个,
c#调用的时候只能调用到第一个,
如何全部调用后绑定到Gridview

解决方案 »

  1.   

    2005可以用 declare @sql varchar(max)类型
      

  2.   

    如果数据库环境是SQL Server 2000,且三个结果集输出的列相同,可以合并在同一个查询里,用union/union all连接,比如:
    declare @sql1 varchar(8000),@sql2 varchar(8000),@sql2 varchar(8000)
    set @sql1='select ...'
    set @sql2='union all select ...'
    set @sql3='union all select ...'
    exec(@sql1+@sql2+@sql3)
    如果数据库环境是SQL Server 2005及以上版本,varchar类型变量内容允许超过8000字节,定义为 varchar(max) 即可。
      

  3.   

    exec(@sql1+@sql2+@sql3)可能用几个连接起来,一起执行,05最好是用VARCHAR(MAX)
      

  4.   

    我用的是SQL2000,你们给我的方法能执行,但是还是三个结果集。向gridview中绑定时还是只能绑定第一个@sql1的结果。其余的绑定不了。
      

  5.   

    三个sql不能union all到一起吗 
      

  6.   

    CREATE proc ysjcx
    @begintime DateTime, 
    @endtime DateTime, 
    @sjb  varchar(100) 
    as 
    begin 
        declare @g varchar(8000) 
        declare @sql varchar(8000) 
        declare @sql1 varchar(8000)
        declare @sql2 varchar(8000)    if (datediff(hh,@begintime,@endtime) <=24 ) 
            set @g = 'convert(varchar(16),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) > 24 ) and (datediff(hh,@begintime,@endtime) <= 720 )) 
            set @g = 'convert(varchar(13),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) >720 ) and (datediff(hh,@begintime,@endtime) <= 8760 )) 
            set @g = ' convert(varchar(10),sj,120)' 
        if (datediff(hh,@begintime,@endtime) > 8760 ) 
            set @g = ' convert(varchar(7),sj,120)'     select @sql=isnull(@sql+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid<=70    set @sql='select  '+@sql+','+@g+' AS [sj] from '+@sjb+' where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' order by '+@g+'' 
         select @sql1=isnull(@sql1+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>70 and  colid<=140    set @sql1='select  '+@sql1+' from '+@sjb+' where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' order by '+@g+'' 
     select @sql2=isnull(@sql2+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>140     set @sql2='select  '+@sql2+' from '+@sjb+' where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' order by '+@g+'' 
       
    exec(@sql+@sql1+@sql2) 
    end
    GO
    union all出错,你们自己调一下吧,试一试。我的表有200多个字段。
      

  7.   

    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
            SqlDataAdapter ada = new SqlDataAdapter();
            SqlCommand sqlcom = new SqlCommand("ysjcx", sqlcon);
            SqlParameter pbegintime = new SqlParameter("@begintime", SqlDbType.DateTime);
            pbegintime.Value = Convert.ToDateTime(sbegintime);
            sqlcom.Parameters.Add(pbegintime);        SqlParameter pendtime = new SqlParameter("@endtime", SqlDbType.DateTime);
            pendtime.Value = Convert.ToDateTime(sendtime);
            sqlcom.Parameters.Add(pendtime);        SqlParameter psjb = new SqlParameter("@sjb", SqlDbType.VarChar, 100);
            psjb.Value = ssjb;
            sqlcom.Parameters.Add(psjb);        sqlcom.CommandType = CommandType.StoredProcedure;        ada.SelectCommand = sqlcom;
            DataSet ds = new DataSet();
            ada.Fill(ds);
            sousuo1.DataSource = ds;
            sousuo1.DataBind();取数据的语句。
      

  8.   


    CREATE proc ysjcx
    @begintime DateTime, 
    @endtime DateTime, 
    @sjb  varchar(100) 
    as 
    begin 
        declare @g varchar(8000) 
        declare @sql varchar(8000) 
        declare @sql1 varchar(8000)
        declare @sql2 varchar(8000)    if (datediff(hh,@begintime,@endtime) <=24 ) 
            set @g = 'convert(varchar(16),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) > 24 ) and (datediff(hh,@begintime,@endtime) <= 720 )) 
            set @g = 'convert(varchar(13),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) >720 ) and (datediff(hh,@begintime,@endtime) <= 8760 )) 
            set @g = ' convert(varchar(10),sj,120)' 
        if (datediff(hh,@begintime,@endtime) > 8760 ) 
            set @g = ' convert(varchar(7),sj,120)'     select @sql=isnull(@sql+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid<=70    set @sql='select  '+@sql+','+@g+' AS [sj] from '+@sjb+' where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' order by '+@g+'' 
         select @sql1=isnull(@sql1+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>70 and  colid<=140    set @sql1='select  '+@sql1+' from '+@sjb+' where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' order by '+@g+'' 
     select @sql2=isnull(@sql2+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>140     set @sql2='select  '+@sql2+' from '+@sjb+' where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' order by '+@g+'' 
       
    exec(@sql+' union all '+@sql1+' union all '+@sql2)   ---三个查询的select 列都一样吧 就这样写 其实很简单 
    end
    GO
      

  9.   

    [code]
    CREATE proc ysjcx
    @begintime DateTime, 
    @endtime DateTime, 
    @sjb  varchar(100) 
    as 
    begin 
        declare @g varchar(8000) 
        declare @sql varchar(8000) 
        declare @sql1 varchar(8000)
        declare @sql2 varchar(8000)    if (datediff(hh,@begintime,@endtime) <=24 ) 
            set @g = 'convert(varchar(16),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) > 24 ) and (datediff(hh,@begintime,@endtime) <= 720 )) 
            set @g = 'convert(varchar(13),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) >720 ) and (datediff(hh,@begintime,@endtime) <= 8760 )) 
            set @g = ' convert(varchar(10),sj,120)' 
        if (datediff(hh,@begintime,@endtime) > 8760 ) 
            set @g = ' convert(varchar(7),sj,120)'     select @sql=isnull(@sql+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid<=70    set @sql='select  '+@sql+','+@g+' AS [sj] from '+@sjb+' where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' ' 
         select @sql1=isnull(@sql1+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>70 and  colid<=140    set @sql1='select  '+@sql1+' from '+@sjb+' where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' ' 
     select @sql2=isnull(@sql2+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>140     set @sql2='select  '+@sql2+' from '+@sjb+' where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' ' 
       
    exec(@sql+' union all '+@sql1+' union all '+@sql2+' order by '+@g+'')   ---三个查询的select 列都一样吧 就这样写 其实很简单 
    end
    GO[/code]
      

  10.   

    你这个是怎么查的啊 按列查的呀 第一个sql取一些列 第二个再去取一些列 >..?
      

  11.   

    你这是三个查询语句,你想怎么样,如果列数不同,肯定不能UNION ALL
    你可以分开三次执行呀,要不然放EXEC()里用;分开
      

  12.   

    服务器: 消息 156,级别 15,状态 1,行 1
    [Microsoft][ODBC SQL Server Driver][SQL Server]在关键字 'union' 附近有语法错误。
      

  13.   

    你的sql1,sql2,sql3的列数不同啊?
      

  14.   

    试过了,exec(@sql; @sql1;@sql2) ?这个是行不通的,我就想从一个表中取出所有的字段,然后根据时间分类,然后求平均值。只是用一句话,超了8000,没办法只能分开取了
      

  15.   

    一个表里的199个字段,分三次取出后求平均值,然后我想得到一个数据集,因为三个select是三个数据集,绑定的时候没办法弄,
      

  16.   

    union 查询,要求每个子查询的字段数相等,各字段的定义类型也要一至!你先看一下:要查询的表有多少个字段!
      

  17.   

       CREATE proc ysjcx
    @begintime DateTime, 
    @endtime DateTime, 
    @sjb  varchar(100) 
    as 
    begin 
        declare @g varchar(8000) 
        declare @sql varchar(8000) 
        declare @sql1 varchar(8000)
        declare @sql2 varchar(8000)

    declare @strSelect varchar(8000)
        if (datediff(hh,@begintime,@endtime) <=24 ) 
            set @g = 'convert(varchar(16),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) > 24 ) and (datediff(hh,@begintime,@endtime) <= 720 )) 
            set @g = 'convert(varchar(13),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) >720 ) and (datediff(hh,@begintime,@endtime) <= 8760 )) 
            set @g = ' convert(varchar(10),sj,120)' 
        if (datediff(hh,@begintime,@endtime) > 8760 ) 
            set @g = ' convert(varchar(7),sj,120)'     select @sql=isnull(@sql+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid<=70 select @sql1=isnull(@sql1+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>70 and  colid<=140 select @sql2=isnull(@sql2+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>140  exec('select  '+@sql+','+ @sql1+','+sql2+' from '+@sjb+ ' 
    where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' order by '+@g+' ') 
       
    end
    GO
    试试
      

  18.   

       CREATE proc ysjcx
    @begintime DateTime, 
    @endtime DateTime, 
    @sjb  varchar(100) 
    as 
    begin 
        declare @g varchar(8000) 
        declare @sql varchar(8000) 
        declare @sql1 varchar(8000)
        declare @sql2 varchar(8000)

    declare @strSelect varchar(8000)
        if (datediff(hh,@begintime,@endtime) <=24 ) 
            set @g = 'convert(varchar(16),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) > 24 ) and (datediff(hh,@begintime,@endtime) <= 720 )) 
            set @g = 'convert(varchar(13),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) >720 ) and (datediff(hh,@begintime,@endtime) <= 8760 )) 
            set @g = ' convert(varchar(10),sj,120)' 
        if (datediff(hh,@begintime,@endtime) > 8760 ) 
            set @g = ' convert(varchar(7),sj,120)'     select @sql=isnull(@sql+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid<=70 select @sql1=isnull(@sql1+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>70 and  colid<=140 select @sql2=isnull(@sql2+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>140  exec('select  '+@sql+','+ @sql1+','+@sql2+' from '+@sjb+ ' 
    where sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + '''  group by '+@g+' order by '+@g+' ') 
       
    end
    GO
    试试
      

  19.   

    楼主敢肯定:colid>140 的字段总共一定只有70个吗?union 查询,要求每个子查询的字段数相等,各字段的定义类型也要一至! 你先看一下:要查询的表有多少个字段!
      

  20.   

    不行的,以前好像就试过,说是convert附近有语法错误,但是哪来的语法错误啊。分开就没有啊
      

  21.   

    你把 exec 里边convert
    如果没有在数据定义或变量声明语句中指定 n,则默认长度为 1
      

  22.   

    主要是@begintime和@endtime两个变量放到exec里就会出错。你说的这个是什么意思啊?没明白
      

  23.   

    你把 时间那段去掉试一下 如果没问题 就说明convert有错     exec('select  '+@sql+','+ @sql1+','+@sql2+' from '+@sjb+ ' where group by '+@g+' order by '+@g+' ') 
      

  24.   

       --试一下 好了说一声CREATE proc ysjcx
    @begintime DateTime, 
    @endtime DateTime, 
    @sjb  varchar(100) 
    as 
    begin 
        declare @g varchar(8000) 
        declare @sql varchar(8000) 
        declare @sql1 varchar(8000)
        declare @sql2 varchar(8000)
        
        
        declare @strTime as nvarchar(100)
        if (datediff(hh,@begintime,@endtime) <=24 ) 
            set @g = 'convert(varchar(16),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) > 24 ) and (datediff(hh,@begintime,@endtime) <= 720 )) 
            set @g = 'convert(varchar(13),sj,120)' 
        if ((datediff(hh,@begintime,@endtime) >720 ) and (datediff(hh,@begintime,@endtime) <= 8760 )) 
            set @g = ' convert(varchar(10),sj,120)' 
        if (datediff(hh,@begintime,@endtime) > 8760 ) 
            set @g = ' convert(varchar(7),sj,120)'     select @sql=isnull(@sql+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid<=70    select @sql1=isnull(@sql1+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>70 and  colid<=140    select @sql2=isnull(@sql2+',','')+'cast(avg('+name+') as decimal(12,2)) as ['+name+']' 
        from syscolumns 
        where id=object_id(@sjb) and name<>'riqi' and name<>'uid' and name<>'sj' and colid>140  select @strTime='sj between ''' + convert(nvarchar,@begintime) + '''  and ''' + convert(nvarchar,@endtime) + ''''
     
        exec('select  '+@sql+','+ @sql1+','+@sql2+' from '+@sjb+ ' where '+ @strTime +' group by '+@g+' order by '+@g+' ') 
       
    end
    GO
      

  25.   

    挣实验的了。把convert去掉后好了,但是这样有隐患啊!不知道为什么convert会出错
    exec('select   '+@sql+', '+@sql1+','+@sql2+','+@g+' AS [sj] from '+@sjb+' where sj between ''' +@begintime+ '''  and ''' + @endtime + '''  group by '+@g+' order by '+@g+'' )
     
    就能行
      

  26.   

    哦,是这样啊!还不知道了,SQL还是太菜了。