我在报表(主从报表)要用到存储过程,比如主表select mainid , from maintbl 
从表是存储过程 比如 create sp_test @mainid integer  as select mainid ,subid from subtbl where mainid=@mainid 当然实际上从表的存储过程是比较复杂的,我想知道如何把主表的mainid作为参数传递到从表的存储过程中,谢谢了

解决方案 »

  1.   

    在从表中USE MAINFRM
    把查询出来的mainid 存入的从表的一个变量中,
    再传进存储过程的参数@BB...
      

  2.   

    如果用SQL,可以设置子表的masterfield和indexfield进行关联,但是储存过程,好像没有办法
      

  3.   

    直接用存贮过程!
    CREATE  PROCEDURE   usp_test   AS 
    declare @mainid char(20)
    declare @count  int
     
    create table #temp1(id int,num int)
    insert into #temp1
    select mainid  from maintbl

    declare read_userid cursor for   select mainid ,subid from subtbl 
    select @count=count(id) from subtbl
     if @count>0
       begin
         open read_userid  
     ----------------------------------------------------------------------------------------------------------------------------while begin 
         while @count>0
           begin
           fetch next from read_userid into @mainid  
           --读取数量金额表
          if exists( select * from subtbl  where id=@mainid )
           begin 
           --希望地操作
             end
           set @count=@count-1
         end
    ---------------------------------------------------------------------------------------------------------------------------while end
         commit transaction
    end
    deallocate read_userid
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------定义游标endGO
      

  4.   

    直接用存贮过程!修正版本
    CREATE  PROCEDURE   usp_test   AS 
    declare @mainid char(20)
    declare @count  int
     
    create table #temp1(id int)
    insert into #temp1
    select mainid  from maintbl

    declare read_userid cursor for   select mainid ,subid from subtbl 
    select @count=count(id) from subtbl
     if @count>0
       begin
         open read_userid  
     
         while @count>0
           begin
           fetch next from read_userid into @mainid  
         
          if exists( select * from subtbl  where id=@mainid )
           begin 
           --希望地操作
             end
           set @count=@count-1
         end
         commit transaction
    end
    deallocate read_userid
    GO
      

  5.   

    谢谢sxdoujg和78hgdong提高的思路,可能是我表达不够明白,我的意识是要从子表得到结果集,主从关系的,当然我们如果直接用代码处理是可以的,但是我目前做报表,只能想办法把主表的mainid转递到从表来,create table #temp1(id int,num int)
    insert into #temp1
    select mainid  from maintbl 实际上只有一个mainid转递过来,但是没有办法传递
      

  6.   

    create table #temp1(id int,num int)
    insert into #temp1
    select mainid,0  from maintbl 
    你这样如果还不能取得你想要的mainID,那你要看看你的main表中到底有几个适合的mainID了
      

  7.   

    各个高手们,根据大家提供的方法,好像还是不能够解决,可能我的存储过程比较复杂,共写了1400行的psql,我如果用sp简单直接读表,是可以的,但是换成我写的那堆代码,就不可以了????