我在sql 2005里执行了一句sql语句
select c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path,sum(Car_Num) as Car_Num,sum(Car_Sumprice) as Car_Sumprice
from Car as c inner join GoodsInfo as g on c.Goods_ID = g.Goods_ID 
where [User_ID] = 1 and Car_Jiezhang=1 
group by c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path 
然后我想把这个查出来的当作一张表
要怎么做?
还有
我的存储过程分页要用到这张表cmd.Parameters.AddWithValue("@tblName", "Provisional"); //表名
这样可以吗?
存储过程里需要的是内存的表,如果弄一个临时表可以用吗?

解决方案 »

  1.   

    是把执行的那句sql语句写成视图还是什么?
      

  2.   

    帅锅,那要怎么把我那句sql当做是临时表呢?
      

  3.   

    存储过程如下:set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    goALTER   PROCEDURE   [dbo].[GetPage]   
              @tblName             varchar(255),               --   表名   
              @fldName             varchar(255),               --   字段名   
              @PageSize           int   =   10,                       --   页尺寸   
              @PageIndex         int   =   1,                         --   页码   
              @IsCount             bit   =   0,                         --   返回记录总数,   非   0   值则返回   
              @OrderType         bit   =   0,                         --   设置排序类型,   非   0   值则降序   
              @strWhere           varchar(1000)   =   ''     --   查询条件   (注意:   不要加   where)   
      AS   
        
      declare     @strSQL       varchar(1000)           --   主语句   
      declare   @strTmp       varchar(300)           --   临时变量   
      declare   @strOrder   varchar(400)               --   排序类型   
        
      if   @OrderType   !=   0   
      begin   
              set   @strTmp   =   '<(select   min'   
              set   @strOrder   =   '   order   by   ['   +   @fldName   +']   desc'   
      end   
      else   
      begin   
              set   @strTmp   =   '>(select   max'   
              set   @strOrder   =   '   order   by   ['   +   @fldName   +']   asc'   
      end   
        
      set   @strSQL   =   'select   top   '   +   str(@PageSize)   +   '   *   from   ['    
              +   @tblName   +   '         ]   where   ['   +   @fldName   +   ']'   +   @strTmp   +   '(['  
              +   @fldName   +   '])   from   (select   top   '   +   str((@PageIndex-1)*@PageSize)   +   '   ['   
              +   @fldName   +   ']   from   ['   +   @tblName   +   ']'   +   @strOrder   +   ')   as   tblTmp)'   
              +   @strOrder   
        
      if   @strWhere   !=   ''   
              set   @strSQL   =   'select   top   '   +   str(@PageSize)   +   '   *   from   ['   
                      +   @tblName   +   ']   where   ['   +   @fldName   +   ']'   +   @strTmp   +   '(['   
                      +   @fldName   +   '])   from   (select   top   '   +   str((@PageIndex-1)*@PageSize)   +   '   ['   
                      +   @fldName   +   ']   from   ['   +   @tblName   +   ']   where   ('   +   @strWhere   +   ')   '   
                      +   @strOrder   +   ')   as   tblTmp)   and   ('   +   @strWhere   +   ')   '   +   @strOrder   
        
      if   @PageIndex   =   1   
      begin   
              set   @strTmp   =   ''   
              if   @strWhere   !=   ''   
                      set   @strTmp   =   '   where   ('   +   @strWhere   +   ')'   
        
              set   @strSQL   =   'select   top   '   +   str(@PageSize)   +   '   *   from   ['   
                      +   @tblName   +   ']'   +   @strTmp   +   '   '   +   @strOrder   
      end   
        
      if   @IsCount   !=   0   
              set   @strSQL   =   'select   count(*)   as   Total   from   ['   +   @tblName   +   ']'   
        
      exec   (@strSQL)   
      

  4.   


    两种方法:一:select * into #temptable1 from (select c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path,sum(Car_Num) as Car_Num,sum(Car_Sumprice) as Car_Sumprice
        from Car as c inner join GoodsInfo as g on c.Goods_ID = g.Goods_ID 
            where [User_ID] = 1 and Car_Jiezhang=1 
            group by c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path )e二:
    insert into #temptable1 select c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path,sum(Car_Num) as Car_Num,sum(Car_Sumprice) as Car_Sumprice
        from Car as c inner join GoodsInfo as g on c.Goods_ID = g.Goods_ID 
            where [User_ID] = 1 and Car_Jiezhang=1 
            group by c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path
    第一种不用事先定义表结构,第二种要有实际表
      

  5.   

    你是说要把select c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path,sum(Car_Num) as Car_Num,sum(Car_Sumprice) as Car_Sumprice
        from Car as c inner join GoodsInfo as g on c.Goods_ID = g.Goods_ID 
            where [User_ID] = 1 and Car_Jiezhang=1 
            group by c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path的结果作为分页存数过程的数据源是吧?
    那视图真是好的选择,create view viewname as 
    select c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path,sum(Car_Num) as Car_Num,sum(Car_Sumprice) as Car_Sumprice
        from Car as c inner join GoodsInfo as g on c.Goods_ID = g.Goods_ID 
            where [User_ID] = 1 and Car_Jiezhang=1 
            group by c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path
      

  6.   

    视图是不是可以当做表放在
    cmd.Parameters.AddWithValue("@tblName", "Provisional"); //表名这里呢?
      

  7.   

    select c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path,sum(Car_Num) as Car_Num,sum(Car_Sumprice) as Car_Sumprice
        from Car as c inner join GoodsInfo as g on c.Goods_ID = g.Goods_ID 
            where [User_ID] = 1 and Car_Jiezhang=1 
            group by c.Goods_ID,Goods_Name,Goods_MarketPrice,Goods_MemberPrice,Goods_Path那如果用参数呢?想把[User_ID]=@User_ID
    要怎么弄?
    还有后台要怎么定义@User_ID?