0

以下是我的存储过程。我收到一个错误,但我不明白到底是什么问题

CREATE Proc [dbo].[sp_Product_details_for_Productcategory_as_per_productcategoryID]                  
   @ProductCategoryId int,                
   @PageIndex INT                 
   ,@PageSize INT                 
   ,@PageCount INT OUTPUT         
   ,@ProductSearch nvarchar(Max)                      
as                      
BEGIN             
  declare @Criteria varchar(4000)       

  set @Criteria = (select replace(@ProductSearch,'(','('''))            
set @Criteria = (select REPLACE(@Criteria,',',''','''))            
set @Criteria = (select REPLACE(@Criteria,')',''')'))          

declare @sql int        

SET NOCOUNT ON;                
        select @sql = ' SELECT ROW_NUMBER() OVER                
            (                
                  ORDER BY PM.ProductMainPkId ASC                
            )AS RowNumber ,          
            PM.ProductMainPkId ProductMainPkId               
      ,PM.Title,                      
    PS.ProductSubCategory ProductSubCategory,                      
   PM.ProductSubCategoryFkId ,                     
  PP.MarketValue,                      
  PP.DiscountPrice,                      
  PID.Path1Thumb                  

    INTO #Results                
       from ProductMain_Details PM                      
  Left join ProductPrice_Details PP on   PM.ProductMainPkId = PP.ProductMain_FkId                      
  Left join ProductImage_Details PID on PM.ProductMainPkId = PID.ProductMain_FkId                      
  Left Join ProductSubCategory_Master PS on  PM.ProductSubCategoryFkId = PS.ProductSubCategoryPkId                      
where                     
--PS.ProductSubCategorypkId = 216                   
PS.ProductSubCategorypkId =  '+ CAST((@ProductCategoryId) as varchar(5) ) +'  and                  
PM.Active = 1                      
 and PM.Deleted = 0'      
 +@Criteria+'                         

      DECLARE @RecordCount INT                
      SELECT @RecordCount = COUNT(*) FROM #Results                

      SET @PageCount = CEILING(CAST(@RecordCount AS DECIMAL(10, 2)) / CAST(@PageSize AS DECIMAL(10, 2)))                
      PRINT       @PageCount                

      SELECT * FROM #Results                
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1                

      DROP TABLE #Results'        
  EXEC    (@sql)      

-- SET NOCOUNT ON;                
--      SELECT ROW_NUMBER() OVER                
--            (                
--                  ORDER BY PM.ProductMainPkId ASC                
--            )AS RowNumber ,          
--            PM.ProductMainPkId ProductMainPkId               
--      ,PM.Title,                      
--    PS.ProductSubCategory ProductSubCategory,                      
--   PM.ProductSubCategoryFkId ,                     
--  PP.MarketValue,                      
--  PP.DiscountPrice,                      
--  PID.Path1Thumb                  

--    INTO #Results                
--       from ProductMain_Details PM                      
--  Left join ProductPrice_Details PP on   PM.ProductMainPkId = PP.ProductMain_FkId                      
--  Left join ProductImage_Details PID on PM.ProductMainPkId = PID.ProductMain_FkId                      
--  Left Join ProductSubCategory_Master PS on  PM.ProductSubCategoryFkId = PS.ProductSubCategoryPkId                      
--where                     
----PS.ProductSubCategorypkId = 216                   
--PS.ProductSubCategorypkId = @ProductCategoryId and                  
--PM.Active = 1                      
-- and PM.Deleted = 0                         

--      DECLARE @RecordCount INT                
--      SELECT @RecordCount = COUNT(*) FROM #Results                

--      SET @PageCount = CEILING(CAST(@RecordCount AS DECIMAL(10, 2)) / CAST(@PageSize AS DECIMAL(10, 2)))                
--      PRINT       @PageCount                

--      SELECT * FROM #Results                
--      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1                

--      DROP TABLE #Results                
END             

错误:

消息 245,级别 16,状态 1,过程 sp_Product_details_for_Productcategory_as_per_productcategoryID,第 21 行
转换 varchar 值时转换失败 'SELECT ROW_NUMBER() OVER
(
ORDER BY PM.ProductMainPkId ASC
)AS RowNumber ,
PM.ProductMainPkId ProductMainPkId
,PM.Title,
PS.ProductSubCategory ProductSubCategory、
PM.ProductSubCategoryFkId、
PP.MarketValue、
PP.DiscountPrice、
PID.Path1Thumb

    INTO #Results              
       from ProductMain_Details PM                    
  Left join ProductPrice_Details PP on   PM.ProductMainPkId = PP.ProductMain_FkId                    
  Left join ProductImage_Details PID on PM.ProductMainPkId = PID.ProductMain_FkId                    
  Left Join ProductSubCategory_Master PS on  PM.ProductSubCategoryFkId = PS.ProductSubCategoryPkId                    
where                   
--PS.ProductSubCategorypkId = 216                 
PS.ProductSubCategorypkId =  1  and                
PM.Active = 1                    
 and PM.Deleted = 0                       

      DECLARE @RecordCount INT              
      SELECT @RecordCount = COUNT(*) FROM #Results              

      SET @PageCount = CEILING(CAST(@RecordCount AS DECIMAL(10, 2)) / CAST(@PageSize AS DECIMAL(10, 2)))              
      PRINT       @PageCount              

      SELECT * FROM #Results              
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1              

      DROP TABLE #Results' to data type int.
4

1 回答 1

4

您将@sql变量声明为何INT时应该是合适大小的 (n)varchar,例如varchar(max)or nvarchar(max),因为您需要该变量来保存您传递给 的字符串EXEC

declare @sql int   

应该

declare @sql varchar(max)   
于 2013-10-31T14:26:13.147 回答