现在执行一个存储过程:
假定是:exec spname @p1,@p2
现在的情况是@p2的值是  (select field1 from table)
请问这种情况下除了用游标,还能有什么更好的办法吗?

解决方案 »

  1.   

    如果能改成标量函数select dbo.fn_name(@p1,field1) from table
      

  2.   

    如果field1返回的不是很多,@p1@p2类型知道,都是输入参数,可以拼sql语句
    如两参数都是字符型declare @sql varchar(8000)
    declare @p1 varchar(200)
    set @p1='abcd'set @sql=''
    select @sql=@sql+'exec spname '''+@p1+''','''+field1+''' '
    exec(@sql)
      

  3.   

    现在的field1字段只有几百条数据,但以后自增的
      

  4.   

    to Top  
     Haiwer(海阔天空)  标量函数怎么个用法?
      

  5.   

    标量函数怎么个用法?
    --------------------------
    Give out your sp first.If possible, you could rewrite it into a function
      

  6.   

    declare @Player varchar(40)
    declare GetPlayer cursor for 
    select Player from vGetPlayOffSeason where Season=@Season and IsPlayOff=@IsPlayOff group by Player order by len(Player)
    open GetPlayer
    fetch next from GetPlayer into @Player
    if exists(select * from sysobjects where xtype='u' and id=object_id('ttbb'))
    drop table ttbb
    create table ttbb(球员 varchar(50),场数 int,投篮 int,罚球 int,得分总数 int,平均得分 dec(4,2))
    while @@fetch_status=0
    begin
            insert ttbb
    select  
    rtrim(Player)+'('+(select rtrim(SimpleCHNName) from TeamInfo where left(SimpleName,3)=Team)+')',
    (select count(*) from vGetPlayOffSeason where Season=@Season  and IsPlayOff=@IsPlayOff and Player=@Player),
    sum(Shot),sum(FreeShowShot),sum(PTS),avg(PTS*1.0)
    from vGetPlayOffSeason
    where Season=@Season  and IsPlayOff=@IsPlayOff and Player=@Player
    group by Player,Team
    fetch next from GetPlayer into @Player
    end
    select top 50 * from ttbb order by 平均得分 desc
    drop table ttbb
    close GetPlayer
    deallocate GetPlayer
      

  7.   

    CREATE procedure pArrayPTS
    @Season varchar(12),
    @IsPlayOff bit
    asset nocount off
    set @season= (select case  
    when len(@Season)=2 then '200'+left(@Season,1)+'-200'+right(@Season,1)
    when len(@Season)=3 and charindex('-',@Season)>0 then '200'+left(@Season,1)+'-200'+right(@Season,1)
    when len(@Season)=3 and charindex('-',@Season)=0 then '200'+left(@Season,1)+'-20'+right(@Season,2)
    when len(@Season)=4 then '20'+left(@Season,2)+'-'+'20'+right(@Season,2)
    when len(@Season)=5 then '20'+left(@Season,2)+'-'+'20'+right(@Season,2)
    when len(@Season)=9 then @Season
    else @Season end)
    declare @Player varchar(40)
    declare GetPlayer cursor for 
    select Player from vGetPlayOffSeason where Season=@Season and IsPlayOff=@IsPlayOff group by Player order by len(Player)
    open GetPlayer
    fetch next from GetPlayer into @Player
    if exists(select * from sysobjects where xtype='u' and id=object_id('ttbb'))
    drop table ttbb
    create table ttbb(球员 varchar(50),场数 int,投篮 int,罚球 int,得分总数 int,平均得分 dec(4,2))
    while @@fetch_status=0
    begin
            insert ttbb
    select  
    rtrim(Player)+'('+(select rtrim(SimpleCHNName) from TeamInfo where left(SimpleName,3)=Team)+')',
    (select count(*) from vGetPlayOffSeason where Season=@Season  and IsPlayOff=@IsPlayOff and Player=@Player),
    sum(Shot),sum(FreeShowShot),sum(PTS),avg(PTS*1.0)
    from vGetPlayOffSeason
    where Season=@Season  and IsPlayOff=@IsPlayOff and Player=@Player
    group by Player,Team
    fetch next from GetPlayer into @Player
    end
    select top 50 * from ttbb order by 平均得分 desc
    drop table ttbb
    close GetPlayer
    deallocate GetPlayer
    set nocount on
    GO
      

  8.   

    改不了标量函数标量函数返回在 RETURNS 子句中定义的类型的单个数据值。可以使用所有标量数据类型,包括 bigint 和 sql_variant。不支持 timestamp 数据类型、用户定义数据类型和非标量类型(如 table 或 cursor)。在 BEGIN...END 块中定义的函数主体包含返回该值的 Transact-SQL 语句系列。返回类型可以是除 text、ntext、image、cursor 和 timestamp 之外的任何数据类型。
      

  9.   

    目的是为求出NBA赛场,某赛季中,常规赛,季后赛,平均得分排行前50名