2013-10-31 88 views
0

以下是我的存儲過程。我得到一個錯誤,但我不明白,究竟是什麼問題在SQL Server存儲過程中將varchar轉換爲int時出錯

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'  
[email protected]+'       

     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
Conversion failed when converting the varchar value ' 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

你宣佈@sql變量INT當它應該是(N)VARCHAR如varchar(max)nvarchar(max),因爲您需要變量來保存您傳遞給EXEC的字符串。

declare @sql int 

應該

declare @sql varchar(max) 
相關問題