SQL Geval Uitdrukking Sintaksis?
-
08-06-2019 - |
Vra
Wat is die voltooi en korrekte sintaksis vir die SQL Geval uitdrukking?
Oplossing
Die voltooi sintaksis hang af van die databasis-enjin jy werk met:
Vir SQL Server:
CASE case-expression
WHEN when-expression-1 THEN value-1
[ WHEN when-expression-n THEN value-n ... ]
[ ELSE else-value ]
END
of:
CASE
WHEN boolean-when-expression-1 THEN value-1
[ WHEN boolean-when-expression-n THEN value-n ... ]
[ ELSE else-value ]
END
uitdrukkings, ens:
case-expression - something that produces a value
when-expression-x - something that is compared against the case-expression
value-1 - the result of the CASE statement if:
the when-expression == case-expression
OR the boolean-when-expression == TRUE
boolean-when-exp.. - something that produces a TRUE/FALSE answer
Skakel: GEVAL (Transaksies-SQL)
Let ook daarop dat die bestel van die WANNEER stellings is belangrik.Jy kan maklik skryf verskeie WANNEER klousules wat oorvleuel, en die eerste een wat ooreenstem gebruik word.
Nota:Indien nie ANDERS klousule is gespesifiseer, en geen bypassende TOE-toestand is gevind, die waarde van die GEVAL uitdrukking sal wees NULL.
Ander wenke
Met inagneming van wat jy gemerk het verskeie produkte, sou ek sê die volle korrekte sintaksis sou die een wat in die ISO / ANSI SQL-92 standaard wees:
<case expression> ::=
<case abbreviation>
| <case specification>
<case abbreviation> ::=
NULLIF <left paren> <value expression> <comma>
<value expression> <right paren>
| COALESCE <left paren> <value expression>
{ <comma> <value expression> }... <right paren>
<case specification> ::=
<simple case>
| <searched case>
<simple case> ::=
CASE <case operand>
<simple when clause>...
[ <else clause> ]
END
<searched case> ::=
CASE
<searched when clause>...
[ <else clause> ]
END
<simple when clause> ::= WHEN <when operand> THEN <result>
<searched when clause> ::= WHEN <search condition> THEN <result>
<else clause> ::= ELSE <result>
<case operand> ::= <value expression>
<when operand> ::= <value expression>
<result> ::= <result expression> | NULL
<result expression> ::= <value expression>
sintaksisreëls
1) NULLIF (V1, V2) is equivalent to the following <case specification>:
CASE WHEN V1=V2 THEN NULL ELSE V1 END
2) COALESCE (V1, V2) is equivalent to the following <case specification>:
CASE WHEN V1 IS NOT NULL THEN V1 ELSE V2 END
3) COALESCE (V1, V2, . . . ,n ), for n >= 3, is equivalent to the
following <case specification>:
CASE WHEN V1 IS NOT NULL THEN V1 ELSE COALESCE (V2, . . . ,n )
END
4) If a <case specification> specifies a <simple case>, then let CO
be the <case operand>:
a) The data type of each <when operand> WO shall be comparable
with the data type of the <case operand>.
b) The <case specification> is equivalent to a <searched case>
in which each <searched when clause> specifies a <search
condition> of the form "CO=WO".
5) At least one <result> in a <case specification> shall specify a
<result expression>.
6) If an <else clause> is not specified, then ELSE NULL is im-
plicit.
7) The data type of a <case specification> is determined by ap-
plying Subclause 9.3, "Set operation result data types", to the
data types of all <result expression>s in the <case specifica-
tion>.
Access Rules
None.
General Rules
1) Case:
a) If a <result> specifies NULL, then its value is the null
value.
b) If a <result> specifies a <value expression>, then its value
is the value of that <value expression>.
2) Case:
a) If the <search condition> of some <searched when clause> in
a <case specification> is true, then the value of the <case
specification> is the value of the <result> of the first
(leftmost) <searched when clause> whose <search condition> is
true, cast as the data type of the <case specification>.
b) If no <search condition> in a <case specification> is true,
then the value of the <case expression> is the value of the
<result> of the explicit or implicit <else clause>, cast as
the data type of the <case specification>.
Hier is die CASE
verklaring voorbeelde uit die PostgreSQL docs (Postgres volg die SQL standaard hier):
SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END
FROM test;
of
SELECT a,
CASE a WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
ELSE 'other'
END
FROM test;
Dit is duidelik dat die tweede vorm is skoner as jy net een veld monitor teen 'n lys van moontlike waardes. Die eerste vorm kan meer ingewikkeld uitdrukkings.
Sybase het dieselfde geval sintaksis as SQL Server:
Beskrywing
Ondersteun voorwaardelike SQL uitdrukkings; kan op enige plek gebruik word 'n waarde uitdrukking gebruik kan word.
Sintaks
case
when search_condition then expression
[when search_condition then expression]...
[else expression]
end
Case en waardes sintaksis
case expression
when expression then expression
[when expression then expression]...
[else expression]
end
Parameters
geval
begin die geval uitdrukking.
toe
voorafgaan die search toestand of die uitdrukking vergelyk word.
search_condition
word gebruik om toestande vir die resultate wat gekies word. Soek voorwaardes vir geval uitdrukkings is soortgelyk aan die soektog voorwaardes in 'n waar klousule. Soek voorwaardes uiteengesit in Guide die Transact-SQL Gebruikers.
dan
voorafgaan die uitdrukking wat gevolg waarde van geval bepaal.
uitdrukking
'n naam kolom, 'n konstante, 'n funksie, 'n subquery, of 'n kombinasie van kolom name, konstantes, en funksies wat verband hou met rekenkundige of bis-operateurs. Vir meer inligting oor uitdrukkings, sien "Expressions" in.
Voorbeeld
select disaster,
case
when disaster = "earthquake"
then "stand in doorway"
when disaster = "nuclear apocalypse"
then "hide in basement"
when monster = "zombie apocalypse"
then "hide with Chuck Norris"
else
then "ask mom"
end
from endoftheworld
Ek opgegrawe die Oracle bladsy vir dieselfde en dit lyk soos hierdie is dieselfde sintaksis, net beskryf effens anders.
Oracle sintaksis van die 11g Dokumentasie :
CASE { simple_case_expression | searched_case_expression }
[ else_clause ]
END
simple_case_expression
expr { WHEN comparison_expr THEN return_expr }...
searched_case_expression
{ WHEN condition THEN return_expr }...
else_clause
ELSE else_expr
Een punt om daarop te let in die geval Oracle se, indien geen wanneer wedstryde en daar is geen ander deel 'n uitsondering is ingesamel.
Case verklaring sintaksis in SQL Server:
CASE column
WHEN value1 THEN 1
WHEN value3 THEN 2
WHEN value3 THEN 3
WHEN value1 THEN 4
ELSE ''
END
En ons kan gebruik soos hieronder ook:
CASE
WHEN column=value1 THEN 1
WHEN column=value3 THEN 2
WHEN column=value3 THEN 3
WHEN column=value1 THEN 4
ELSE ''
END