简体   繁体   English

SQL Server 2008-更新临时表

[英]SQL Server 2008 - Update a temporary table

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. 用户可能没有与之关联的票证,在这种情况下,我知道我只需要返回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. 同时,id不知道如何仅获取更新语句中的最后一条记录。

Thank you! 谢谢!

Once you've loaded the data into #tickets (as your example does), this query will give you the most recent for each username: 将数据加载到#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

I don't think you need the Tickets temporary table in this case. 在这种情况下,我认为您不需要Tickets临时表。 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM