SQL-子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =

时间:2019-09-09 03:40:02

标签: sql

我正在使用存储过程运行查询,但是出现错误。我想更新每个TicketNumber的每一列。我正在更新TimeRun列,以计算当前运行时间。 DateAndTime是创建日期的开始日期,是使用变量@end(当前时间)计算得出的。

ALTER PROCEDURE spTicketTimeUpdate
AS
BEGIN
    DECLARE @start DATETIME, @end DATETIME = GETDATE(),
            @runningTime VARCHAR(50), @x INT

    SET @start = (SELECT DateAndTime FROM TicketTb)
    SET @x = DATEDIFF(s, @start, @end);

    SET @runningTime = CONVERT(VARCHAR(10), (@x / 86400)) + ' Day(s) ' +
                       CONVERT(VARCHAR(10), ((@x % 86400) / 3600)) + ' Hr(s) ' +
                       CONVERT(VARCHAR(10), (((@x % 86400) % 3600) / 60)) + ' Min(s) ' + 
                       CONVERT(VARCHAR(10), (((@x % 86400) % 3600) % 60)) + ' Sec(s)';

    UPDATE TicketTb 
    SET TimeRun = @runningTime 
    WHERE TicketStatus = 'Open'

    UPDATE TicketTb 
    SET RunTimeClose = @runningTime

END

enter image description here

4 个答案:

答案 0 :(得分:2)

我认为这就是您所需要的

ALTER PROCEDURE spTicketTimeUpdate

as
begin

declare @end datetime
set @end = getdate()

update TicketTb set TimeRun = t1.rt 
from
    (select CONVERT(VARCHAR(10), ( DATEDIFF(s, DateAndTime, @end) / 86400 )) + ' Day(s) '
        + CONVERT(VARCHAR(10), ( ( DATEDIFF(s, DateAndTime, @end) % 86400 ) / 3600 )) + ' Hr(s) '
        + CONVERT(VARCHAR(10), ( ( ( DATEDIFF(s, DateAndTime, @end) % 86400 ) % 3600 ) / 60 ))
        + ' Min(s) ' + CONVERT(VARCHAR(10), ( ( ( DATEDIFF(s, DateAndTime, @end) % 86400 ) % 3600 ) % 60 ))
        + ' Sec(s)' as rt, TicketNumber from TicketTb
            where TicketStatus = 'Open') as t1
where t1.TicketNumber = TicketTb.TicketNumber


update TicketTb set RunTimeClose = t1.rt 
from
    (select CONVERT(VARCHAR(10), ( DATEDIFF(s, DateAndTime, @end) / 86400 )) + ' Day(s) '
        + CONVERT(VARCHAR(10), ( ( DATEDIFF(s, DateAndTime, @end) % 86400 ) / 3600 )) + ' Hr(s) '
        + CONVERT(VARCHAR(10), ( ( ( DATEDIFF(s, DateAndTime, @end) % 86400 ) % 3600 ) / 60 ))
        + ' Min(s) ' + CONVERT(VARCHAR(10), ( ( ( DATEDIFF(s, DateAndTime, @end) % 86400 ) % 3600 ) % 60 ))
        + ' Sec(s)' as rt, TicketNumber from TicketTb
            ) as t1
where t1.TicketNumber = TicketTb.TicketNumber

结束

答案 1 :(得分:1)

扩展Metals excellent answer,可以使用CASE Statement在一个查询中完成

var viewer = NOP_VIEWER;
viewer.setGhosting(false);

答案 2 :(得分:1)

您可以尝试一下。为了简化此操作,我使用{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "sqlserverName": { "type": "string", "minLength": 1, "defaultValue": "[concat('sqlserver', uniqueString(resourceGroup().id))]" }, "sqlserverAdminLogin": { "type": "string", "minLength": 1, "defaultValue":"jimtest" }, "sqlserverAdminLoginPassword": { "type": "securestring", "metadata": { "description": "The administrator password of the SQL Server." }, "defaultValue":"Password0123!" }, "dbName": { "type": "string", "minLength": 1, "defaultValue": "[concat('db', uniqueString(resourceGroup().id))]" }, "dbCollation": { "type": "string", "minLength": 1, "defaultValue": "SQL_Latin1_General_CP1_CI_AS" }, "dbEdition": { "type": "string", "defaultValue": "Basic", "allowedValues": [ "Basic", "Standard", "Premium" ] }, "dbRequestedServiceObjectiveName": { "type": "string", "defaultValue": "Basic", "allowedValues": [ "Basic", "S0", "S1", "S2", "P1", "P2", "P3" ], "metadata": { "description": "Describes the performance level for Edition" } } }, "variables": { "sqlserverName": "[parameters('sqlserverName')]", "databaseName": "[parameters('dbName')]" }, "resources": [ { "name": "[variables('sqlserverName')]", "type": "Microsoft.Sql/servers", "location": "[resourceGroup().location]", "apiVersion": "2014-04-01-preview", "dependsOn": [ ], "tags": { "displayName": "sqlserver" }, "properties": { "administratorLogin": "[parameters('sqlserverAdminLogin')]", "administratorLoginPassword": "[parameters('sqlserverAdminLoginPassword')]" }, "resources": [ { "name": "AllowAllWindowsAzureIps", "type": "firewallrules", "location": "[resourceGroup().location]", "apiVersion": "2014-04-01-preview", "dependsOn": [ "[resourceId('Microsoft.Sql/servers', variables('sqlserverName'))]" ], "properties": { "startIpAddress": "0.0.0.0", "endIpAddress": "0.0.0.0" } }, { "name": "[parameters('dbName')]", "type": "databases", "location": "[resourceGroup().location]", "apiVersion": "2014-04-01-preview", "dependsOn": [ "[resourceId('Microsoft.Sql/servers', variables('sqlserverName'))]" ], "tags": { "displayName": "db" }, "properties": { "collation": "[parameters('dbCollation')]", "edition": "[parameters('dbEdition')]", "maxSizeBytes": "1073741824", "requestedServiceObjectiveName": "[parameters('dbRequestedServiceObjectiveName')]" } } ] }], "outputs": { "ServerObject": { "type": "Object", "value": "[reference(variables('sqlServerName'))]" }, "Passwording":{ "type":"string", "value":"[parameters('sqlserverAdminLoginPassword')]" } } } 将查询分为不同的部分。首先,您需要在cte中获得所需的时差,然后在@x部分中以字符串形式计算时差。最后,通过使用case语句,您可以按以下方式在单个更新查询中更新两个结果。

ct

答案 3 :(得分:0)

您有这行:

set  @start = (Select DateAndTime from TicketTb)

如果表TicketTb具有多行,则引擎不知道要选择哪一行。

您应自行决定如何选择一行:

set  @start = (Select TOP(1) DateAndTime from TicketTb order by DateAndTime DESC)