如何在存储过程中设置多个输出参数

How to set multiple output parameters in stored procedures

有没有办法设置多个输出参数?

例如,在此处显示的存储过程中,我希望从同一个存储过程中获取 NUM_OF_ROWSTicketNumberz

因为我是新手不知道怎么办...

DECLARE @TicketNumberz VARCHAR(15) OUT
DECLARE @NUM_OF_ROWS INT OUT
DECLARE @INIT INT=1 OUT

SET @TicketNumberz = (SELECT TICKETNUMBER
                      FROM TicketHistory s1 
                      WHERE TICKETTIME IN (SELECT MAX(S2.TICKETTIME) 
                                           FROM TicketHistory] S2 
                                           WHERE s1.TICKETNUMBER = S2.TICKETNUMBER)  
                        AND CURRENTSTATUS_ANALYST != 'Closed' 
                        AND CURRENTSTATUS_ANALYST = 'Resolved'
                        AND TICKETTIME < GETDATE() - 5)

-- after getting all the list of ticket numbers, update query follows to update the ticket status to 'Closed'   

WHILE (@INIT <= @NUM_OF_ROWS)
BEGIN
    INSERT INTO TicketHistory (CURRENTSTATUS_ANALYST, TICKETNUMBER, 
                               PREVIOUSSTATUS_ANALYST, TICKETTIME, FIELD, CREATEDBY)
    VALUES ('Closed', @TicketNumberz, 
            'Resolved', CURRENT_TIMESTAMP, 'Status', 'Auto.User')   
END       

这个查询基本上是做什么的,它会获取所有超过 5 天且未自动 'Closed' 的 ''Resolved' 票证。所以通过这个存储过程手动完成。

但是,由于以下错误,我卡住了:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

数据库是这样的:

我可能会在此重做子查询,但并不重要。似乎 table 结构在这里有点挑战,但我认为您正在寻找类似的东西。这将替换您发布的所有代码。根据您发布的内容,不需要任何类型的循环或变量。

--编辑--

随着 OP 评论的澄清,所有需要发生的事情就是在此 select

上方添加插入内容
insert into TicketHistory (CURRENTSTATUS_ANALYST,TICKETNUMBER,PREVIOUSSTATUS_ANALYST,TICKETTIME,FIELD,CREATEDBY)
SELECT 'Closed'
    , th.TICKETNUMBER
    , 'Resolved'
    , CURRENT_TIMESTAMP
    , 'Status'
    , 'Auto.User'
FROM TicketHistory th 
WHERE TICKETTIME IN 
(
    SELECT MAX(S2.TICKETTIME) 
    FROM TicketHistory S2 
    WHERE s1.TICKETNUMBER = S2.TICKETNUMBER
)  
    --AND CURRENTSTATUS_ANALYST != 'Closed' --there is no value that equals 'Resolved' where it could also equal 'Closed'
    AND th.CURRENTSTATUS_ANALYST = 'Resolved'
    AND th.TICKETTIME < dateadd(day, -5, GETDATE()) --use dateadd instead of shorthand

最后,我用下面的代码得到了想要的结果:

    --Creating a table variable
    declare @TempTable table(ticketnum varchar(50))

    --Inserting values in table variable using procedure
    insert @TempTable
    select TICKETNUMBER 
        FROM TicketHistory th 
    WHERE TICKETTIME IN 
    (
        SELECT MAX(S2.TICKETTIME) 
        FROM TicketHistory S2 
        WHERE th.TICKETNUMBER = S2.TICKETNUMBER
    )  
        AND th.CURRENTSTATUS_ANALYST = 'Resolved'
        AND th.TICKETTIME < dateadd(day, -5, GETDATE())

    --Selecting values from table variable
    SELECT * from @TempTable
            
    DECLARE @ticket_number varchar(100)
    DECLARE cur CURSOR FOR SELECT ticketnum FROM @TempTable
    OPEN cur

    FETCH NEXT FROM cur INTO @ticket_number

    WHILE @@FETCH_STATUS = 0 
        BEGIN
            insert into TicketHistory (CURRENTSTATUS_ANALYST,TICKETNUMBER,PREVIOUSSTATUS_ANALYST,TICKETTIME,FIELD,CREATEDBY)
              values('Closed', @ticket_number, 'Resolved', CURRENT_TIMESTAMP, 'Status', 'User.Auto')
            FETCH NEXT FROM cur INTO @ticket_number
        END

    CLOSE cur    
    DEALLOCATE cur
        
    END