SQL Server 2008 - 更新临时表

发布于 2024-09-01 13:51:05 字数 1064 浏览 5 评论 0原文

我有一个存储过程,在其中尝试检索以逗号分隔的用户名字符串中列出的每个用户完成的最后一张票证。用户可能没有与他们关联的票证,在这种情况下我知道我只需要返回 null。我正在使用的两个表定义如下:

User
----
UserName,
FirstName,
LastName

Ticket
------
ID,
CompletionDateTime,
AssignedTo,
AssignmentDate,
StatusID

TicketStatus
------------
ID,
Comments

我创建了一个存储过程,在其中尝试返回逗号分隔的用户名列表的最后完成的票证。每条记录都需要包含与其关联的评论。目前,我正在尝试以下操作:

CREATE TABLE #Tickets
(
  [UserName] nvarchar(256),
  [FirstName] nvarchar(256),
  [LastName] nvarchar(256),
  [TicketID] int,
  [DateCompleted] datetime,
  [Comments] text
)

-- This variable is actually passed into the procedure
DECLARE @userList NVARCHAR(max)
SET @userList='user1,user2,user2'

-- Obtain the user information for each user
INSERT INTO #Tickets
(
  [UserName],
  [FirstName],
  [LastName]
)
SELECT
  u.[UserName],
  u.[FirstName],
  u.[LastName]
FROM
  User u 
    INNER JOIN dbo.ConvertCsvToTable(@userList) l ON u.UserName=l.item

此时,我已传入每个用户的用户名、名字和姓氏。但是,我不知道如何实际为每个用户完成最后一张票证。

我该怎么做?我相信我应该更新我创建的临时表。同时,id 不知道如何获取更新语句中的最后一条记录。

谢谢你!

I have stored procedure in which I am trying to retrieve the last ticket completed by each user listed in a comma-delimited string of usernames. The user may not have a ticket associated with them, in this case I know that i just need to return null. The two tables that I am working with are defined as follows:

User
----
UserName,
FirstName,
LastName

Ticket
------
ID,
CompletionDateTime,
AssignedTo,
AssignmentDate,
StatusID

TicketStatus
------------
ID,
Comments

I have created a stored procedure in which I am trying to return the last completed ticket for a comma-delimited list of usernames. Each record needs to include the comments associated with it. Currently, I'm trying the following:

CREATE TABLE #Tickets
(
  [UserName] nvarchar(256),
  [FirstName] nvarchar(256),
  [LastName] nvarchar(256),
  [TicketID] int,
  [DateCompleted] datetime,
  [Comments] text
)

-- This variable is actually passed into the procedure
DECLARE @userList NVARCHAR(max)
SET @userList='user1,user2,user2'

-- Obtain the user information for each user
INSERT INTO #Tickets
(
  [UserName],
  [FirstName],
  [LastName]
)
SELECT
  u.[UserName],
  u.[FirstName],
  u.[LastName]
FROM
  User u 
    INNER JOIN dbo.ConvertCsvToTable(@userList) l ON u.UserName=l.item

At this point, I have the username, first and last name for each user passed in. However, I do not know how to actually get the last ticket completed for each of these users.

How do I do this? I believe I should be updating the temp table I have created. At the same time, id do not know how to get just the last record in an update statement.

Thank you!

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

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

发布评论

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

评论(2

蓝颜夕 2024-09-08 13:51:05

我认为在这种情况下您不需要 Tickets 临时表。您只想知道“对于逗号分隔列表中的每个用户,他们最近完成的票证是什么(如果有)”。

因此,您需要找出每个用户的最新票证,并且您希望确保外部联接该结果,以便如果用户没有票证,您仍然可以取回他们。

这是未经测试的,但希望它能为您提供基本的想法。

    SELECT u.UserName
         , u.FirstName
         , u.LastName
         , t.ID
         , t.CompletionDateTime
         , t.AssignedTo
         , t.AssignmentDate
         , t.StatusID
      FROM dbo.User u INNER JOIN dbo.ConvertCsvToTable(@userList) ON u.UserName=l.item
   LEFT OUTER JOIN
          (SELECT t.AssignedTo
                , MAX(t.CompletionDateTime) CompletionDateTime)
             FROM Ticket t
         GROUP BY t.AssignedTo) t
    ON t.AssignedTo = u.UserName;

I don't think you need the Tickets temporary table in this case. You just want to know, "For each user in the comma separated list, what was their most recently completed ticket (if any)".

So you need to find out the most recent ticket for each user, and you want to be sure to outer join that result so if user has no ticket you still get them back.

This is untested, but hopefully it gives you the basic idea.

    SELECT u.UserName
         , u.FirstName
         , u.LastName
         , t.ID
         , t.CompletionDateTime
         , t.AssignedTo
         , t.AssignmentDate
         , t.StatusID
      FROM dbo.User u INNER JOIN dbo.ConvertCsvToTable(@userList) ON u.UserName=l.item
   LEFT OUTER JOIN
          (SELECT t.AssignedTo
                , MAX(t.CompletionDateTime) CompletionDateTime)
             FROM Ticket t
         GROUP BY t.AssignedTo) t
    ON t.AssignedTo = u.UserName;
像你 2024-09-08 13:51:05

将数据加载到 #tickets 后(如您的示例所示),此查询将为您提供每个用户名的最新数据:

select * from #tickets x
inner join
(select username, max(DateCompleted) as theDate 
    from #tickets group by username) y
ON x.username = y.username, x.DateCompleted = y.theDate

Once you've loaded the data into #tickets (as your example does), this query will give you the most recent for each username:

select * from #tickets x
inner join
(select username, max(DateCompleted) as theDate 
    from #tickets group by username) y
ON x.username = y.username, x.DateCompleted = y.theDate
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文