UPDATE SET 中的子查询 (sql server 2005)

发布于 2024-08-28 01:24:35 字数 455 浏览 6 评论 0原文

我有一个关于在 Update 语句中使用子查询的问题。我的示例:

UPDATE TRIPS
   SET locations = city + ', ' FROM (select Distinct city 
                                       from poi 
                                      where poi.trip_guid = trips.guid) 

是否可以在子查询中引用主表值(trips.guid)?

当我尝试使用 trips.guid 时,出现错误:

“无法绑定多部分标识符“trips.guid”。”

“select Distinct city from poi”子句返回多个城市。

I have a question about using subqueries in an Update statement. My example:

UPDATE TRIPS
   SET locations = city + ', ' FROM (select Distinct city 
                                       from poi 
                                      where poi.trip_guid = trips.guid) 

Is it possible to refer to main table value (trips.guid) in subqueries?

When i try to use trips.guid I get the error:

"The multi-part identifier "trips.guid" could not be bound."

The clause 'select Distinct city from poi' return more that one city.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

匿名的好友 2024-09-04 01:24:35

你可以尝试类似的东西

UPDATE  trips
SET     locations = t.city + ', ' + poi.city
FROM    trips t INNER JOIN
        (
            select Distinct city, trip_guid from poi
        ) poi ON t.trip_guid = poi.trip_guid

You can try something like

UPDATE  trips
SET     locations = t.city + ', ' + poi.city
FROM    trips t INNER JOIN
        (
            select Distinct city, trip_guid from poi
        ) poi ON t.trip_guid = poi.trip_guid
口干舌燥 2024-09-04 01:24:35

另一个版本。

UPDATE trips
SET locations = trips.city + ', ' + poi.city
FROM trips INNER JOIN poi
ON poi.trip_guid = trips.guid

Another version.

UPDATE trips
SET locations = trips.city + ', ' + poi.city
FROM trips INNER JOIN poi
ON poi.trip_guid = trips.guid
深爱成瘾 2024-09-04 01:24:35

您可以在子选择中使用外部选择中的常量和值:

Update trips
Set locations = ( Select Distinct trips.city + ', ' + poi.city
                  From poi
                  Where poi.trip_guid = trips.guid )

我们不知道您的表是什么样子,所以我只能假设 Distinct 将以这种方式为您工作(仅返回子查询中的一个不同的城市)。

You can use constants and values from the outer select in the sub-select:

Update trips
Set locations = ( Select Distinct trips.city + ', ' + poi.city
                  From poi
                  Where poi.trip_guid = trips.guid )

We don't know how your tables look like, so I can only assume that Distinct will work for you that way (returning only one distinct city in the sub-query).

早茶月光 2024-09-04 01:24:35

我找到了解决方案 - 只需将子查询移至 UDF :)

UPDATE TRIPS
   SET locations = getAllTripCity(guid);

我的 UDF 源代码:

CREATE FUNCTION dbo.getAllTripCity(
    @tripGuid uniqueidentifier
)
RETURNS nvarchar(200)
AS
BEGIN
 DECLARE @cities nvarchar(200);
 set  @cities = ''
 select @cities =  @cities + city + ' ' from (select DISTINCT city poi where poi.trip_guid = @tripGuid)
 return @ @cities;
END

这就是你我需要做的 - 工作正常:)

I found the solution - just move the subquery to the UDF :)

UPDATE TRIPS
   SET locations = getAllTripCity(guid);

My UDF's source code:

CREATE FUNCTION dbo.getAllTripCity(
    @tripGuid uniqueidentifier
)
RETURNS nvarchar(200)
AS
BEGIN
 DECLARE @cities nvarchar(200);
 set  @cities = ''
 select @cities =  @cities + city + ' ' from (select DISTINCT city poi where poi.trip_guid = @tripGuid)
 return @ @cities;
END

That's all what you I need to do - works fine :)

葵雨 2024-09-04 01:24:35

我遇到了与最初的海报相同的问题。我的用例如下:
一张表包含体育赛事的日期和时间。因为我从不同的来源获取信息,所以我更改了数据库的架构,因此我有一个代表体育赛事日期的时间和日期时间(或者可能只是日期)的 int 值。

这是我的查询:

UPDATE Matches 
SET StartTime= MatchTime.ThisMatchStartTime
FROM Matches AS M
INNER JOIN (SELECT CONVERT(int, CONVERT(varchar, DATEPART(Hour, MatchDate)) + RIGHT('00' + CONVERT(varchar, DATEPART(Minute, MatchDate)),2)) AS ThisMatchStartTime, MatchId
  FROM [Matches]
  WHERE SportTypeId=16) AS MatchTime ON M.MatchId=MatchTime.MatchId
WHERE StartTime > 2400
AND SportTypeId = 16;

一些解释:
您必须为子查询 MatchStartTime 指定一个不同的名称,否则您会从 SQL Server 收到警告/错误。
我还必须添加 MatchId,这样我就知道我正在更新正确的 Match。
SportTypeId 用于分隔数据库中的不同运动项目。

感谢@astander 为我指明了正确的方向。如果没有他的帖子,我会更加努力地最终找到这个解决方案。

I had the same issue as the initial poster. My use case was the following:
One table contained Date and Time of a sport event. Because I am getting information from different sources, I changed the schema of the database so I had a int value for time and datetime (or maybe just date) for the date of the sport event.

This is my query:

UPDATE Matches 
SET StartTime= MatchTime.ThisMatchStartTime
FROM Matches AS M
INNER JOIN (SELECT CONVERT(int, CONVERT(varchar, DATEPART(Hour, MatchDate)) + RIGHT('00' + CONVERT(varchar, DATEPART(Minute, MatchDate)),2)) AS ThisMatchStartTime, MatchId
  FROM [Matches]
  WHERE SportTypeId=16) AS MatchTime ON M.MatchId=MatchTime.MatchId
WHERE StartTime > 2400
AND SportTypeId = 16;

Some explanation:
You have to give the subquery MatchStartTime a different name otherwise you get a warning/error from SQL Server.
I also had to add MatchId so I knew I was updating the correct Match.
The SportTypeId is used to separate different sports in the database.

Thanks to @astander for pointing me in the right direction. Without his post I would have struggled a bit more to end up with this solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文