日期:2014-05-18  浏览次数:20368 次

关于SQL存储过程必须声明表变量的问题
SQL code
ALTER PROCEDURE [dbo].[corpListRealUser]
    -- Add the parameters for the stored procedure here
    @myCountrys varchar(500),
    @myCategorys varchar(500),
    @myTopic decimal,
    @myPriceL int,
    @myPriceH int,
    @myOrderType int,
    @myOrderFlag int,
    @mySerKeyword varchar(500),
    @CI_StartRecordIndex int,
    @CI_EndRecordIndex int
AS

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    -- Insert statements for procedure here
    declare @str varchar(1000),@strOrder varchar(200)
    set @str=''    
    declare @split varchar(1)
    set @split=','
    if @myTopic>0
        begin
        set @str=@str+'and topic_ID='+rtrim(@myTopic)
        end
    set @str=@str+'and CI_Charge BETWEEN '+rtrim(@myPriceL)+' AND '+rtrim(@myPriceH)
    if @myCountrys<>'0'
--把国家参数拆分存为@t_country:start
        begin
        declare @t_country table(m varchar(200))    
        while charindex(rtrim(@split),@myCountrys)<>0
            begin
            set @myCountrys=ltrim(rtrim(@myCountrys))
            if(substring(@myCountrys,1,1)<>@split)
              begin
              insert into @t_country (m) values(substring(@myCountrys,1,charindex(@split,@myCountrys)-1))
              set @myCountrys =stuff(@myCountrys,1,charindex(@split,@myCountrys),'')
              end
            else
              begin
              set @myCountrys=stuff(@myCountrys,1,1,'')
              end
            end
            if rtrim(@myCountrys) <>''
            insert into @t_country (m) values (@myCountrys) 
--把国家参数拆分存为@t_country:end        
        set @str=@str+'and exists(select * from @t_country where TE_CorpInfoReal.Country_ID=m)'
        end
    
    if @myCategorys<>'0'
--把类别参数拆分存为@t_country:start
        begin
        declare @t_Category table(n varchar(200))    
        while charindex(rtrim(@split),@myCategorys)<>0
            begin
            set @myCategorys=ltrim(rtrim(@myCategorys))
            if(substring(@myCategorys,1,1)<>@split)
              begin
              insert into @t_Category (n) values(substring(@myCategorys,1,charindex(@split,@myCategorys)-1))
              set @myCategorys =stuff(@myCategorys,1,charindex(@split,@myCategorys),'')
              end
            else
              begin
              set @myCategorys=stuff(@myCategorys,1,1,'')
              end
            end
            if rtrim(@myCategorys) <>''
            insert into @t_Category (n) values (@myCategorys) 
--把类别参数拆分存为@t_country:end        
        set @str=@str+'and exists(
        select * from @t_Category
        where charindex('+@split+' + m + '+@split+', '+@split+' + TE_CorpInfoReal.C_ChildID) > 0)'
        end    

    if @mySerKeyword<>''
        begin
        --set @str=@str+'and (CI_Keywords like ''%'+ @mySerKeyword +'%'' or CI_Profile like ''%'+ @mySerKeyword +'%'' or CI_CorpName like ''%'+ @mySerKeyword +'%'')'
        set @str=@str+'and (CI_Keywords like ''%'+ @mySerKeyword +'%'')'
        end

    set @strOrder=''
    declare @orderPX varchar(10)
    if @myOrderFlag=1
        set @orderPX=' asc'
    else
        set @orderPX=' desc'
    if @myOrderType=1
        begin            
            set @strOrder=@strOrder+'order by CI_ModifyTime'+@orderPX    
        end
    else if @myOrderType=2
        begin            
            set @strOrder=@strOrder+'order by CI_Charge'+@orderPX    
        end
    else if @myOrderType=3
        begin            
            set @strOrder=@strOrder+'order by CI_ClickTimes'+@orderPX    
        end
    else if @myOrderType=4
        begin            
            set @strOrder=@strOrder+'order by CI_BuyTimes'+@orderPX    
        end
    else
        begin            
            set @strOrder=@strOrder+'order