使用CTE更新语句SQL

时间:2017-01-13 17:54:19

标签: sql sql-server tsql

我有一个常用的表表达式,我正在尝试将其用作更新语句。 CTE的唯一原因是我可以使用where子句来过滤CredCount。我想使用where子句来仅更新匹配的记录,在这种情况下CredCount为2.但是,我在查询的更新部分遇到问题。

+---------------+--------------+-----------+-----------------+
| Members_id    | Credentials  | CredCount |members_amountdue|
+---------------+--------------+-----------+-----------------+
| 1             | CMA, CPR     |      2    |    0            |
| 2             | CMA, CPR     |      2    |    0            |
| 3             | CMA, CPR     |      2    |    0            |
+---------------+--------------+-----------+-----------------+

以下是对该

的查询
WITH CTE AS (
SELECT members_id,   members_amountdue,  [Credentials], LEN([Credentials]) - LEN(REPLACE([Credentials], ',', '')) + 1 AS CredCount
FROM (
        SELECT DISTINCT  mem1.members_id, mem1.members_amountdue, 
            STUFF(
                (SELECT DISTINCT ', ' + credentials_code
                FROM members AS mem JOIN
                members_credentials  AS mc ON mc.members_id = mem.members_id JOIN
                credentials AS c ON c.credentials_id = mc.credentials_id
                WHERE mem.[members_id] = mem1.[members_id]              
                FOR XML PATH ('')), 1, 1, '')  AS [Credentials]
        FROM members AS mem1 JOIN
        members_status as ms on ms.members_status_id = mem1.members_status_id   
    ) AS derived),
CTE2 AS ( SELECT members_id
            FROM members)
SELECT  CTE.members_id, CTE.Credentials, CTE.CredCount, (CTE.members_amountdue + 25) as NewPriceTotal
FROM CTE JOIN
CTE2 ON CTE.members_id = CTE2.members_id
WHERE CTE.CredCount = 2

使用更新语句,我正在查看此处Update records in table from CTE提供的示例,因此我将更新语句添加到查询的底部

WITH CTE AS (
SELECT members_id,   members_amountdue,  [Credentials], LEN([Credentials]) - LEN(REPLACE([Credentials], ',', '')) + 1 AS CredCount
FROM (
        SELECT DISTINCT  mem1.members_id, mem1.members_amountdue, 
            STUFF(
                (SELECT DISTINCT ', ' + credentials_code
                FROM members AS mem JOIN
                members_credentials  AS mc ON mc.members_id = mem.members_id JOIN
                credentials AS c ON c.credentials_id = mc.credentials_id
                WHERE mem.[members_id] = mem1.[members_id]              
                FOR XML PATH ('')), 1, 1, '')  AS [Credentials]
        FROM members AS mem1 JOIN
        members_status as ms on ms.members_status_id = mem1.members_status_id   
    ) AS derived),
CTE2 AS ( SELECT members_id
            FROM members)
SELECT  CTE.members_id, CTE.Credentials, CTE.CredCount, (CTE.members_amountdue + 25) as NewPriceTotal
FROM CTE JOIN
CTE2 ON CTE.members_id = CTE2.members_id
WHERE CTE.CredCount = 2
UPDATE members
set members_amountdue = NewPriceTotal

当我添加更新语句时,我收到“NewPriceTotal”的无效列名称错误。我知道我需要列匹配才能使更新生效但我不确定为什么它无效。

+---------------+--------------+-----------+----------------+
| Members_id    | Credentials  | CredCount |  NewPriceTotal |
+---------------+--------------+-----------+----------------+
| 1             | CMA, CPR     |      2    |    25          |
| 2             | CMA, CPR     |      2    |    25          |
| 3             | CMA, CPR     |      2    |    25          |
+---------------+--------------+-----------+----------------+

我想将NewPriceTotal设置在members表的members_amountdue中,但我不确定我在哪里做错了。非常感谢任何帮助,评论或反馈。

1 个答案:

答案 0 :(得分:1)

试试这个:

....
CTE2 AS ( 
   SELECT members_id
   FROM members
), CTE3 AS (
   SELECT  CTE.members_id, CTE.Credentials, CTE.CredCount,  
           CTE.members_amountdue,  
           (CTE.members_amountdue + 25) as NewPriceTotal
   FROM CTE JOIN CTE2 ON CTE.members_id = CTE2.members_id
   WHERE CTE.CredCount = 2)
UPDATE CTE3
SET members_amountdue = NewPriceTotal