create function [dbo].[GetSumpageByIp](@customerID bigint ,@ip varchar(200),@stime datetime,@endTime datetime)
returns float 
as 
begin
declare @temp1 varchar(8000) 
declare @temp float
set @temp1='
select @temp= Convert(numeric(10, 2),Count(CountURl))  from Count_RandD'+Convert(varchar(50),@CustomerID)+'
where CustomerID='''+convert(varchar(50),@CustomerID)+''' and 
countTime between   '''+convert(varchar(100),@stime)+''' and  '''+convert(varchar(100),@endtime)+''' and
countIp='''+@ip+''''
exec (@temp1)
return @temp
end--------
这个怎么改成存储过程,并说一下存储过程怎么用,谢谢!

解决方案 »

  1.   

    create procedure protest
    @customerID bigint ,
    @ip varchar(200),
    @stime datetime,
    @endTime datetime
    returns float 
    as 
    declare @temp1 varchar(8000) 
    declare @temp float
    set @temp1='
    select @temp= Convert(numeric(10, 2),Count(CountURl))  from Count_RandD'+Convert(varchar(50),@CustomerID)+'
    where CustomerID='''+convert(varchar(50),@CustomerID)+''' and 
    countTime between   '''+convert(varchar(100),@stime)+''' and  '''+convert(varchar(100),@endtime)+''' and
    countIp='''+@ip+''''
    exec (@temp1)
      

  2.   

    create procedure protest
    @customerID bigint ,
    @ip varchar(200),
    @stime datetime,
    @endTime datetime
    as 
    declare @temp1 varchar(8000) 
    declare @temp float
    set @temp1='
    select @temp= Convert(numeric(10, 2),Count(CountURl))  from Count_RandD'+Convert(varchar(50),@CustomerID)+'
    where CustomerID='''+convert(varchar(50),@CustomerID)+''' and 
    countTime between   '''+convert(varchar(100),@stime)+''' and  '''+convert(varchar(100),@endtime)+''' and
    countIp='''+@ip+''''
    exec (@temp1)
    go
    --调用
    EXEc protest 1,'1xs','2007-04-26','2007-04-26'
      

  3.   

    create procedure [dbo].[GetSumpageByIp](
    @customerID bigint      ,
    @ip         varchar(200),
    @stime      datetime    ,
    @endTime    datetime    ,
    @return     float output)
    as 
    begin
        declare @sql nvarchar(8000) 
        set @sql=N'
        select @return=Convert(numeric(10, 2),Count(CountURl)) from Count_RandD'+Convert(varchar(50),@CustomerID)+'
        where CustomerID='''+convert(varchar(50),@CustomerID)+''' and 
        countTime between   '''+convert(varchar(100),@stime)+''' and  '''+convert(varchar(100),@endtime)+''' and
        countIp='''+@ip+''''
        exec @sql,N'@return float output',@return output
        return
    end
    godeclare @customerID bigint,@ip varchar(200),@stime datetime,@endTime datetime,@return float
    set ...exec [dbo].[GetSumpageByIp] @customerID,@ip,@stime,@endTime,@return outputselect @return
    go
      

  4.   

    lz的那个函数能用么?不是说在函数中不能使用exec (@temp1)这种动态SQL语句么?
      

  5.   

    alter proc [dbo].[GetSumpageByIp](@customerID bigint ,@ip varchar(200),@stime datetime,@endTime datetime,@temp float output)
    as 
    begin
    declare @temp1 nvarchar(4000) 
    set @temp1='
    select @temp= Convert(numeric(10, 2),Count(CountURl))  from Count_RandD'+Convert(varchar(50),@CustomerID)+'
    where CustomerID='''+convert(varchar(50),@CustomerID)+''' and 
    countTime between   '''+convert(varchar(100),@stime)+''' and  '''+convert(varchar(100),@endtime)+''' and
    countIp='''+@ip+''''
    --print @temp1
    exec sp_executesql @temp1,N'@temp float output',@temp output
    end
    Go
    declare @temp float 
    exec GetSumpageByIp 1,'2','2007-04-23','2007-04-23',@temp output
    print @temp
      

  6.   

    要在select 中 执行存储过程怎么执行!
      

  7.   

    CCHome1985() ( ) 信誉:100    Blog   加为好友  2007-04-26 15:56:29  得分: 0  
     
     
       要在select 中 执行存储过程怎么执行!
      
    -------------------------------------------------
    不行.不能像函数那样调用.
      

  8.   

    想办法把你存储过程的功能放在sql语句里实现,看你的意思逻辑好像也不很复杂.
    另外,可能要用游标逐行处理才行.