SQL حالة التعبير اللغوي?
-
08-06-2019 - |
سؤال
ما هو كاملة و الصحيح بناء جملة SQL حالة التعبير ؟
المحلول
على كاملة بناء الجملة يعتمد على قاعدة بيانات المحرك كنت تعمل مع:
ل SQL Server:
CASE case-expression
WHEN when-expression-1 THEN value-1
[ WHEN when-expression-n THEN value-n ... ]
[ ELSE else-value ]
END
أو:
CASE
WHEN boolean-when-expression-1 THEN value-1
[ WHEN boolean-when-expression-n THEN value-n ... ]
[ ELSE else-value ]
END
التعبيرات ، وما إلى ذلك:
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
الرابط: حالة (Transact-SQL)
نلاحظ أيضا أن يأمر من حين البيانات الهامة.يمكنك بسهولة كتابة متعددة عند البنود التي تتداخل ، أول واحد يطابق يستخدم.
ملاحظة:إذا كان أي شرط آخر هو المحدد و لا مطابقة عندما الشرط وجدت قيمة الحالة سيتم التعبير NULL.
نصائح أخرى
باعتبار أنك معلم منتجات متعددة ، أنا أقول كامل بناء الجملة الصحيح سوف تكون واحدة وجدت في ISO/ANSI القياسي SQL-92:
<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>
قواعد النحو
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>.
هنا CASE
بيان أمثلة من كيو مستندات (بوستجرس يلي SQL القياسية هنا):
SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END
FROM test;
أو
SELECT a,
CASE a WHEN 1 THEN 'one'
WHEN 2 THEN 'two'
ELSE 'other'
END
FROM test;
ومن الواضح أن النموذج الثاني هو منظف عندما كنت مجرد التحقق من حقل واحد ضد قائمة من القيم الممكنة.أول نموذج يتيح المزيد من التعبيرات المعقدة.
Sybase له نفس حالة الجملة كما SQL Server:
الوصف
يدعم المشروط عبارات SQL;يمكن استخدامها في أي مكان قيمة التعبير يمكن استخدامها.
بناء الجملة
case
when search_condition then expression
[when search_condition then expression]...
[else expression]
end
حالة والقيم الجملة
case expression
when expression then expression
[when expression then expression]...
[else expression]
end
المعلمات
الحالة
تبدأ حالة التعبير.
عندما
تسبق شرط البحث أو التعبير أن تكون مقارنة.
search_condition
يتم استخدام مجموعة شروط النتائج التي يتم اختيارها.البحث ظروف القضية تعبيرات مشابهة شروط البحث في جملة where.البحث شروط مفصلة في Transact-SQL دليل المستخدم.
ثم
تسبق التعبير الذي يحدد قيمة نتيجة القضية.
التعبير
هو اسم العمود ثابت ، وظيفة ، فرعي ، أو أي مزيج من أسماء الأعمدة, الثوابت, وظائف متصلة بواسطة الحساب أو المعامل المشغلين.لمزيد من المعلومات حول التعبيرات ، انظر "التعبير" في.
على سبيل المثال
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
حفرت أوراكل صفحة عن نفسه و يبدو أن هذا هو نفس بناء الجملة فقط وصفها مختلفة قليلا.
الرابط: أوراكل/PLSQL:بيان حالة
أوراكل جملة من الوثائق 11g:
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
نقطة واحدة إلى ملاحظة في أوراكل الحالة ، إذا كان أي عندما المباريات وليس هناك شيء آخر جزء يتم رفع استثناء.
بيان حالة بناء الجملة في SQL SERVER:
CASE column
WHEN value1 THEN 1
WHEN value3 THEN 2
WHEN value3 THEN 3
WHEN value1 THEN 4
ELSE ''
END
و يمكننا استخدام مثل أدناه أيضا:
CASE
WHEN column=value1 THEN 1
WHEN column=value3 THEN 2
WHEN column=value3 THEN 3
WHEN column=value1 THEN 4
ELSE ''
END