/*
-------------------------------------------
名  称:Product_GetInfoByTemplate
功  能:根据参数表返回对应的产品
参  数:TableName 用户输入的一个表名,KeyWords,传入的参数数组,Expression查询条件
相关表:Product
编写人:****
时  间:20050810
-------------------------------------------
*/CREATE   PROCEDURE Product_GetInfoByTemplate
(
@TableName varchar(100)=null, --用户输入的一个表名
            @keywords        nvarchar(500)=null,            --传入的参数数组
            @Expression     nvarchar(500)=null             --条件表达式
)
ASbegin
 exec ('select  Product.ProductName  as 名称, 
 Product.ProductModel as 型号,  Product.ProductBrand as 品牌,
      Product.MoneyAmount as 价格, 
      Product.MoneyComment as 价格说明, Product.FromArea  as 产地 , 
     Product.ForIndustry as 适用行业 , 
     
     '+@keywords+'  from Product INNER JOIN '+@TableName+' ON Product.ProductID=Product.ProductID WHERE '+@Expression+'')
end
GO

解决方案 »

  1.   

    CREATE   PROCEDURE Product_GetInfoByTemplate
    (
    @TableName varchar(100)=null, --用户输入的一个表名
                @keywords        nvarchar(500)=null,            --传入的参数数组
                @Expression     nvarchar(500)=null             --条件表达式
    @PageSize   int,                     --显示记录数
    @PageIndex int, --页索引
    @docount bit
    )
    AS
    set nocount on
    if(@docount=1)
    exec ('select  Product.ProductName  as 名称, 
     Product.ProductModel as 型号,  Product.ProductBrand as 品牌,
          Product.MoneyAmount as 价格, 
          Product.MoneyComment as 价格说明, Product.FromArea  as 产地 , 
         Product.ForIndustry as 适用行业 , 
         
         '+@keywords+'  from Product INNER JOIN '+@TableName+' ON Product.ProductID=Product.ProductID WHERE '+@Expression+'')
    else
    begin
    declare @PageLowerBound int
    declare @PageUpperBound int
    set @PageLowerBound=(@PageIndex-1)*@PageSize
    set @PageUpperBound=@PageLowerBound+@PageSize
    create table #PageIndex(id int identity(1,1) not null,nid int)
    set rowcount @PageUpperBound
    exec('insert into #PageIndex(ind) select  Product.ProductID , 
         
         '+@keywords+'  from Product INNER JOIN '+@TableName+' ON Product.ProductID=Product.ProductID WHERE '+@Expression+' ')
    select 0.* 
    from Product 0,#PageIndex p INNER JOIN '+@TableName+' ON Product.ProductID=Product.ProductID WHERE '+@Expression+'
    end
    set nocount off
    GO
    改成这样,还是报错