0
我有奇怪的問題有以下SP:存儲過程鑄造錯誤
CREATE PROCEDURE [dbo].[Manufacturer_GetProductsCountByManufacturedId]
(
@ManufacturerIds NVARCHAR(MAX) = '',
@ExcludeType INT
)
AS
BEGIN
DECLARE @ManufacturerId NVARCHAR(MAX)
DECLARE @GoodManufacturerIds NVARCHAR(MAX) = ''
DECLARE @result BIGINT
DECLARE @pos INT
DECLARE @len INT
SET @GoodManufacturerIds = ''
SET @pos = 0
SET @len = 0
WHILE CHARINDEX(',', @ManufacturerIds, @pos + 1)>0
BEGIN
-- Split
SET @len = CHARINDEX(',', @ManufacturerIds, @pos+1) - @pos
SET @ManufacturerId = SUBSTRING(@ManufacturerIds, @pos, @len)
-- Check
SELECT TOP 1 @result = p.ProductId
FROM [MYDB].[dbo].[Product] p
INNER JOIN [MYDB].[ProductTyping].[TypedProductFieldValue] tpfv ON tpfv.ProductId = p.ProductId
WHERE ManufacturerId = @ManufacturerId
AND tpfv.ProductTypeId <> @ExcludeType
IF @result > 0
SET @GoodManufacturerIds = @GoodManufacturerIds + @ManufacturerId + ','
SET @pos = CHARINDEX(',', @ManufacturerIds, @[email protected])+1
END
-- Last comma kaputt
SET @GoodManufacturerIds = LEFT(@GoodManufacturerIds, LEN(@GoodManufacturerIds) - 1)
RETURN @GoodManufacturerIds
END
基本上我有分裂與逗號分隔值的字符串併爲每個這些值,我執行查詢,並把導致另一個逗號分隔的字符串。 (「5220,3008,1561,2678,」和)我得到一個錯誤,「無法強制轉換nvarchar值'5220,3008,1561,2678'鍵入int「。 但如果我建於這些值在存儲過程爲:
DECLARE @ManufacturerId NVARCHAR(MAX)
DECLARE @GoodManufacturerIds NVARCHAR(MAX) = ''
DECLARE @result BIGINT
DECLARE @pos INT
DECLARE @len INT
-- test
DECLARE @ManufacturerIds NVARCHAR(MAX) = ''
DECLARE @ExcludeType INT
SET @ManufacturerIds = '5220,3008,1561,2678,13715,5047,'
SET @ExcludeType = 5
SET @GoodManufacturerIds = ''
SET @pos = 0
SET @len = 0
WHILE CHARINDEX(',', @ManufacturerIds, @pos + 1)>0
BEGIN
-- Split
SET @len = CHARINDEX(',', @ManufacturerIds, @pos+1) - @pos
SET @ManufacturerId = SUBSTRING(@ManufacturerIds, @pos, @len)
-- Check
SELECT TOP 1 @result = p.ProductId
FROM [MYDB].[dbo].[Product] p
INNER JOIN [MYDB].[ProductTyping].[TypedProductFieldValue] tpfv ON tpfv.ProductId = p.ProductId
WHERE ManufacturerId = @ManufacturerId
AND tpfv.ProductTypeId <> @ExcludeType
IF @result > 0
SET @GoodManufacturerIds = @GoodManufacturerIds + @ManufacturerId + ','
SET @pos = CHARINDEX(',', @ManufacturerIds, @[email protected])+1
END
-- Last comma kaputt
SET @GoodManufacturerIds = LEFT(@GoodManufacturerIds, LEN(@GoodManufacturerIds) - 1)
RETURN @GoodManufacturerIds
一切正常,我得到了我的新的字符串。 我不知道錯誤可能在第一個過程中,任何提示?
爲什麼不使用數據類型*設計*來保存多個值,如XML或表格,而不是將所有內容填充到字符串中? – 2015-02-12 08:29:56
您好t-clausen.dk,我已經使用正確的語法高亮更新了帖子,現在您可以看到「<>」 – Ras 2015-02-12 08:41:31