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

Posted

技术标签:

【中文标题】在 SQL Server 存储过程中将 varchar 转换为 int 时出错【英文标题】:Error converting varchar to int in SQL Server stored procedure 【发布时间】:2013-10-31 14:22:05 【问题描述】:

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

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 ( 由 PM.ProductMainPkId ASC 订购 )AS 行号 , 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】:

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

declare @sql int   

应该是

declare @sql varchar(max)   

【讨论】:

以上是关于在 SQL Server 存储过程中将 varchar 转换为 int 时出错的主要内容,如果未能解决你的问题,请参考以下文章

在 Sql Server 中将 xml 文档作为存储过程的参数传递是不是安全?

如何在 sql server 2008r2 中将行名更改为列名

sql存储过程输出

在 SQL Server 存储过程中查找表名的列名作为保留关键字

如何在 sql server 2008 中将日期与 GETDATE() 进行比较

如何从 SQL Server 2014 中的 Select 查询中将数据分配给用户定义的表类型