在SQL Server存储过程中将varchar转换为int时出错

时间:2013-10-31 14:22:05

标签: asp.net sql sql-server

以下是我的存储过程。我收到错误但我不明白究竟是什么问题

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             

错误:

  

Msg 245,Level 16,State 1,Procedure sp_Product_details_for_Productcategory_as_per_productcategoryID,Line 21
  转换varchar值'SELECT ROW_NUMBER()OVER
时转换失败                   (
                        订购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.

1 个答案:

答案 0 :(得分:4)

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

declare @sql int   

应该是

declare @sql varchar(max)   
相关问题