简体   繁体   中英

SQL CASE sub-query

I have two tables, the following query to retrieve the os memory when max instance memory is unlimited and the error message:

--osmemory                          --instancememory
servername  osmemory                servername  instancename  instancememory
----------  --------                ----------  ------------  --------------
srva        4096                    srva        srva\insta    2048
srvb        6144                    srvb        srvb\instb    2147483647

select i.ServerName
     , i.instancename
     , case i.instance memory LIKE '2147483647' 
         then (select o.osmemory from o.osmemory
               join i.instance on o.servername = i.servername)
...

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

I've been trying (and i'm still trying) various scripts, but with no luck.

Can somebody help? Thanks

The error is obvious. You are using a subquery in a context where one column and up to one row is allowed. This is called a "scalar subquery". One method to fix this uses an aggregation function:

select i.ServerName, i.instancename,
       (case i.instance memory LIKE '2147483647' 
            then (select max(o.osmemory) from o.osmemory join i.instance on o.servername = i.servername)
       . . . 

However, that might not be your intent.

EDTI:

If I had to speculate, your problem is the join in the subquery. You probably just want a correlated subquery. This is a guess, but:

select i.ServerName, i.instancename,
       (case i.instance memory LIKE '2147483647' 
            then (select o.osmemory from o.osmemory where o.servername = i.servername)
       . . . 

还是使用Max(o.osmemory)或Max(o.osmemory)不管结果是否始终为1。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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