[英]Update statement SQL with CTE
I have a common table expression that I am using trying to use as an update statement.我有一个公共表表达式,我试图将其用作更新语句。 The only reason for the CTE is so I can use a where clause to filter by CredCount.
CTE 的唯一原因是我可以使用 where 子句按 CredCount 进行过滤。 I would like to use that where clause to update only records that match in this case CredCount of 2. However, I am having trouble with the update part of the query.
我想使用该 where 子句仅更新在这种情况下 CredCount 为 2 匹配的记录。但是,我在查询的更新部分遇到了问题。
Members_id![]() |
Credentials![]() |
CredCount![]() |
members_amountdue ![]() |
---|---|---|---|
1 ![]() |
CMA, CPR![]() |
2 ![]() |
0 ![]() |
2 ![]() |
CMA, CPR![]() |
2 ![]() |
0 ![]() |
3 ![]() |
CMA, CPR![]() |
2 ![]() |
0 ![]() |
Here is the query for that这是查询
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
With the update statement I was looking at an example provided here at Update records in table from CTE so I added the update statement to the bottom of the query使用更新语句,我查看了在更新 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
When I add the update statement I get an error for invalid column name for 'NewPriceTotal'.当我添加更新语句时,我收到“NewPriceTotal”无效列名的错误。 I know I need the column to match in order for the update to work but I'm not sure why it's invalid.
我知道我需要该列匹配才能使更新工作,但我不确定为什么它无效。
Members_id![]() |
Credentials![]() |
CredCount![]() |
NewPriceTotal![]() |
---|---|---|---|
1 ![]() |
CMA, CPR![]() |
2 ![]() |
25 ![]() |
2 ![]() |
CMA, CPR![]() |
2 ![]() |
25 ![]() |
3 ![]() |
CMA, CPR![]() |
2 ![]() |
25 ![]() |
I would like for the NewPriceTotal to be set at the members_amountdue from the members table but I'm not sure where I made the wrong turn at.我希望将 NewPriceTotal 设置在 members 表中的 members_amountdue 处,但我不确定我在哪里转错了。 Any help, comments or feedback is greatly appreciated.
非常感谢任何帮助、评论或反馈。
Try this:尝试这个:
....
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.