这是我在SQL Server 2016中创建的过程。
CREATE PROCEDURE [dbo].[sp_getProductInfo]
@CompanyId VARCHAR(50) = '',
@ProductIds VARCHAR(max) = '',
@IsChange VARCHAR(100) = '0',
@PaymentOptionId VARCHAR(100) = '0',
@MetalTypeId VARCHAR(100) = '0',
@MetalGradeId VARCHAR(100) = '0',
@FullCutClarityId_P1 VARCHAR(100) = '0',
@SingleCutClarityId_P1 VARCHAR(100) = '0',
@IsconvertedToFullCut_P1 VARCHAR(100) = '0',
@FullCutClarityId_P2 VARCHAR(100) = '0',
@SingleCutClarityId_P2 VARCHAR(100) = '0',
@IsconvertedToFullCut_P2 VARCHAR(100) = '0'
AS
DECLARE @SQL VARCHAR(8000) = ''
BEGIN
SET NOCOUNT ON
SET @SQL = 'SELECT dbo.Product.Id, dbo.Product.ItemSku, dbo.Product.ProductName, dbo.Product.ParentSku, dbo.Product.Quantity,
(SELECT TotalProductPrice FROM dbo.fn_returnChangedB2BCost(dbo.Product.Id,'+@CompanyId+','+@IsChange+','+@PaymentOptionId+','+@MetalTypeId+','+@MetalGradeId+','+@FullCutClarityId_P1+','+@SingleCutClarityId_P1+','+@IsconvertedToFullCut_P1+','+@FullCutClarityId_P2+','+@SingleCutClarityId_P2+','+@IsconvertedToFullCut_P2+')) AS StandardPrice, dbo.Product.SalePrice
FROM dbo.Product
WHERE dbo.Product.CompanyId = '+@CompanyId+' AND dbo.Product.Id in(' + @ProductIds +')'
EXEC (@SQL)
END
函数是
CREATE FUNCTION [dbo].[fn_returnChangedB2BCost]
(@ProductId BIGINT = 0,
@CompanyId INT = 0,
@IsChangeByCritariya BIT = 0,
@PaymentOptionId INT = 0,
@MetalTypeId BIGINT = 0,
@MetalGradeId BIGINT = 0,
@FullCutClarityId_P1 BIGINT = 0,
@SingleCutClarityId_P1 BIGINT = 0,
@IsConvertedToFullCut_P1 BIT = 0,
@FullCutClarityId_P2 BIGINT = 0,
@SingleCutClarityId_P2 BIGINT = 0,
@IsConvertedToFullCut_P2 BIT = 0)
RETURNS @ReturnTable TABLE (TotalProductPrice DECIMAL(18, 4))
AS
BEGIN
INSERT INTO @ReturnTable (TotalProductPrice)
SELECT 120
RETURN
END
现在我的问题是,当我调用该过程时,它返回的错误是..
将数据类型nvarchar转换为数字时出错。
但是,当我传递..
这样的直接产品ID时,与其在功能中放入“ dbo.Product.Id”,(SELECT TotalProductPrice FROM dbo.fn_returnChangedB2BCost(3596,'+@CompanyId+','+@IsChange+','+@PaymentOptionId+','+@MetalTypeId+','+@MetalGradeId+','+@FullCutClarityId_P1+','+@SingleCutClarityId_P1+','+@IsconvertedToFullCut_P1+','+@FullCutClarityId_P2+','+@SingleCutClarityId_P2+','+@IsconvertedToFullCut_P2+')) as StandardPrice
然后正常工作。
我找不到解决方案。请帮忙。
谢谢
答案 0 :(得分:1)
我怀疑您的问题在这里
set @SQL='SELECT dbo.Product.Id, dbo.Product.ItemSku, dbo.Product.ProductName, dbo.Product.ParentSku, dbo.Product.Quantity,
(select TotalProductPrice from dbo.fn_returnChangedB2BCost(dbo.Product.Id,'+@CompanyId+','+@IsChange+','+@PaymentOptionId+','+@MetalTypeId+','+@MetalGradeId+','+@FullCutClarityId_P1+','+@SingleCutClarityId_P1+','+@IsconvertedToFullCut_P1+','+@FullCutClarityId_P2+','+@SingleCutClarityId_P2+','+@IsconvertedToFullCut_P2+')) as StandardPrice, dbo.Product.SalePrice
FROM dbo.Product
where dbo.Product.CompanyId='+@CompanyId+' and dbo.Product.Id in(' + @ProductIds +')'
exec (@SQL)
您应该在可能的情况下将数据作为参数传递。 @ProductIds
有点棘手,所以一个好的开始是:
DECLARE @SQL NVARCHAR(MAX) = '';
set @SQL = N'
SELECT p.Id, p.ItemSku, p.ProductName, p.ParentSku, p.Quantity,
(select TotalProductPrice
FROM dbo.fn_returnChangedB2BCost(p.Id, @CompanyId, @IsChange, @PaymentOptionId, @MetalTypeId, @MetalGradeId, @FullCutClarityId_P1, @SingleCutClarityId_P1, @IsconvertedToFullCut_P1, @FullCutClarityId_P2, @SingleCutClarityId_P2, @IsconvertedToFullCut_P2)
) as StandardPrice,
p.SalePrice
FROM dbo.Product
WHERE dbo.Product.CompanyId =@CompanyId and p.Id in(' + @ProductIds +')';
exec sp_executesql @sql,
N'@ProductId bigint, @CompanyId, @IsChangeByCritariya, @PaymentOptionId, @MetalTypeId, @MetalGradeId, @FullCutClarityId_P1, @SingleCutClarityId_P1, @IsConvertedToFullCut_P1, @FullCutClarityId_P2, @SingleCutClarityId_P2, @IsConvertedToFullCut_P2',
@ProductId=@ProductId,
@CompanyId=@CompanyId ,
@IsChangeByCritariya=@IsChangeByCritariya,
@PaymentOptionId=@PaymentOptionId,
@MetalTypeId=@MetalTypeId,
@MetalGradeId=@MetalGradeId,
@FullCutClarityId_P1=@FullCutClarityId_P1,
@SingleCutClarityId_P1=@SingleCutClarityId_P1,
@IsConvertedToFullCut_P1=@IsConvertedToFullCut_P1,
@FullCutClarityId_P2=@FullCutClarityId_P2,
@SingleCutClarityId_P2=@SingleCutClarityId_P2,
@IsConvertedToFullCut_P2=@IsConvertedToFullCut_P2;