Domanda

Avevo pubblicato una domanda simile in precedenza - un requisito leggermente diverso qui.

Ho una casella di testo che restituisce l'utente selezionato 'Number'Value. (Eg .: 100.200.300)

Ciò che deve essere fatto è fondamentalmente controllare una tabella MyTable Se esistono record/record per il particolare Number valore selezionato dall'utente. Se restituisce null, devo restituire i record per il valore numero predefinito di 999.

MyTable:
id Number MyVal
1  100    55
2  200    66
3  400    22
4  400    12
5  999    23
6  999    24

Ecco cosa ho finora: (assumendo textBoxInput (numero) = 300)

SELECT Myval 
from MyTable 
where id in (
    SELECT ISNULL(
        SELECT id 
            from MyTable 
            where Number=300, 
        select id 
            from MyTable 
            where Number = 999
    )
)

Quindi qui, poiché il numero = 300 non esiste nella tabella, restituisce i record per il numero = 999.

Ma quando eseguo questa query, ricevo un errore "la sottostante ha restituito più di 1 valore ..."

Qualche suggerimento/idee?

È stato utile?

Soluzione

Questo dovrebbe funzionare:

SELECT Myval 
from MyTable 
where Number = @Number
OR (NOT EXISTS(SELECT * FROM MyTable WHERE Number = @Number) AND Number = 999)

Altri suggerimenti

SELECT id, Myval 
  FROM MyTable 
 WHERE id = @id
UNION
SELECT id, 999 AS Myval
  FROM MyTable 
 WHERE id = 999
       AND @id IS NULL;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top