处理多个记录的存储过程

时间:2017-08-18 22:38:11

标签: sql-server tsql stored-procedures

我正在尝试转换一个存储过程,该存储过程接受一个参数,并将特定记录的表列更新为存储过程,该存储过程执行相同的更新,但是批量处理。

因为在存储过程代码中有一个变量,所以当我删除参数并运行它时,我会收到一条错误:

  

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

我想请求帮助,并学习如何正确地将存储过程从基于参数的转换为无参数代码。

更新代码

这是我失败的代码:

BEGIN

SET NOCOUNT ON;

DECLARE @Desc  NVARCHAR(MAX);
SET @DESC = 'Beginning of description ';
SET @Desc = @DESC+(
SELECT
  CASE
    WHEN LEFT([pd].[Title], 1) = '"'
    THEN CONCAT('"', REPLACE([pd].[Title], '"', ''), '"')
  END
FROM [dbo].[Data_Feed] [df]
INNER JOIN [dbo].[ProductData] [pd]
    ON [df].[sku] = [pd].[ProductID]
WHERE pd.ProductType IN (19, 20, 23, 26, 27));

SET @DESC = (
SELECT
  CASE
    WHEN [df].[show] IS NOT NULL
    THEN CONCAT(@DESC, ' from ', [df].[show])
    ELSE CASE
           WHEN [df].[primary_performer] IS NOT NULL
           THEN CONCAT([df].[primary_performer], ' ', @DESC)
           ELSE CASE
                  WHEN [df].[performer] IS NOT NULL
                  THEN CASE
                         WHEN LEN([df].[performer])-LEN(REPLACE([df].[performer], ';', '')) > 1
                         THEN CONCAT(@DESC, ' by ', REPLACE(STUFF([df].[performer], LEN([df].[performer])-CHARINDEX(';',REVERSE([df].[performer]))+1, 1, ' and '), ';', ', '))                                                                                              
                         ELSE CONCAT(@DESC, ' by ', REPLACE([df].[performer],';', ' and '))                                                                                      
                       END
                  ELSE CASE                                                                              
                         WHEN [df].[composer] IS NOT NULL
                         THEN CASE
                                WHEN LEN([df].[composer])-LEN(REPLACE([df].[composer], ';', '')) > 1
                                THEN CONCAT(@DESC, ' by ', REPLACE(STUFF([df].[composer], LEN([df].[composer])-CHARINDEX(';', REVERSE([df].[composer]))+1, 1, ' and '),';', ', '))                                                                                                      
                                ELSE CONCAT(@DESC, ' by ', REPLACE([df].[composer], ';', ' and '))
                              END
                       END
                END
         END
  END
FROM [Data_Feed] [df]);

SET @DESC = @DESC+(
SELECT
  CASE
    WHEN [df].[scoring] IS NULL
    THEN CONCAT(RTRIM(@DESC), '.')
    ELSE CASE
           WHEN CHARINDEX(';', [df].[scoring]) > 0
           THEN CONCAT(' in ', LEFT([df].[scoring], CHARINDEX(';', [df].[scoring], 1)-1), ' format.')
           ELSE CONCAT('  in ', [df].[scoring], ' format.')
         END
  END
FROM [Data_Feed] [df]);

UPDATE [Data_Feed]
SET
  [description] = @Desc         

END;

1 个答案:

答案 0 :(得分:0)

看起来这就是你想要的......

ALTER PROCEDURE dbo.Update_Description

AS
BEGIN
    SET NOCOUNT ON;

    UPDATE df SET
        df.[description] = CONCAT('Beginning of description ', CASE -- Why??? Both the "then" & "else" are the exact same...???
                                WHEN LEFT(pd.Title, 1) = '"'
                                THEN CONCAT('"', REPLACE(pd.Title, '"', ''), '"')
                                ELSE CONCAT('"', REPLACE(pd.Title, '"', ''), '"')
                            END)
    FROM 
        dbo.Feed df
        INNER JOIN dbo.Product pd
            ON df.sku = pd.ProductID
    WHERE 
        pd.ProductType IN (19, 20, 23, 26, 27);
END;
GO

另外,看一下你的CASE表达,“then”和& “别的”是完全相同的。

HTH,Jason