我想在PLSQL中的CASE THEN语句中编写一个select语句,但它会抛出错误。请告知我是否可以在THEN Statement中编写select语句。
类似于我的要求的示例如下所示
SET SERVEROUTPUT ON
DECLARE
LV_VAR VARCHAR2(4000):=NULL;
BEGIN
LV_VAR:= CASE
WHEN 1=1 THEN
(SELECT 1 FROM DUAL)
ELSE
0
END;
DBMS_OUTPUT.PUT_LINE(LV_VAR);
END;
执行时,会抛出错误,如下所示
ORA-06550: line 6, column 26:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
答案 0 :(得分:3)
您无法直接在PL / SQL代码中使用标量子查询,就像您所示。 (当然,您已经知道了。)您必须选择值INTO
变量,然后使用它。
另请注意:您的代码中没有case
个语句。您有case
表达式。它不会像你写的那样工作。
另一种选择是在SQL case
语句中使用SELECT ... INTO
表达式,正如David Goldman在他的答案中所示。
但是,如果练习的重点是练习PL / SQL中使用的case
表达式,而不是SQL语句中的表达式,则需要SELECT ... INTO
在代码中声明的变量case
,然后在DECLARE
LV_VAR VARCHAR2(4000);
BEGIN
SELECT 1 INTO LV_VAR FROM DUAL;
LV_VAR:= CASE
WHEN 1=1 THEN
LV_VAR
ELSE
0
END;
DBMS_OUTPUT.PUT_LINE(LV_VAR);
END;
表达式中使用该变量。像这样:
SELECT ... INTO
正如您所看到的,我做了一些在过程语言代码中经常执行的操作:我只声明了一个,而不是声明和使用两个变量。我使用case
查询的结果填充它。然后我在 <title>Clein.</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1,
user-scalable=no" />
<link rel="stylesheet" href="assets/css/main.css" />
<link href="assets/css/lightbox.min.css" rel="stylesheet">
<noscript><link rel="stylesheet" href="assets/css/noscript.css" />
</noscript>
<!--[if lte IE 9]><link rel="stylesheet" href="assets/css/ie9.css" /><!
[endif]-->
<link rel="icon" type="image/png" href="favicon.png"/>
<body>
<!-- Wrapper -->
<div id="wrapper">
<!-- Header -->
<header id="header">
<div class="logo">
<img src="Clein_Logo_v1.png" alt="Logo">
</div>
<div class="content">
<div class="inner">
<p>Bij mijn schoonheidsinstituut kan je terecht
voor <br /> tal van behandelingen.</p>
</div>
</div>
<nav>
<ul>
<li><a href="#intro">Over</a></li>
<li><a href="#gallery">Gallerij</a></li>
<li><a href="#work">Behandelingen</a></li>
<li><a href="#about">Book Online</a></li>
<li><a href="#contact">Contact</a></li>
<!--<li><a href="#elements">Elements</a></li>-->
</ul>
</nav>
<ul class="icons">
<li><a href="https://www.facebook.com/Clein-
140687139893899/" target="_blank"
class="icon fa-facebook"><span
class="label">Facebook</span></a></li>
<li><a
href="https://www.instagram.com/_clein._/"
target="_blank" class="icon fa-instagram">
<span class="label">Instagram</span></a>
</li>
</ul>
</header>
<!-- Main -->
<div id="main">
<!-- Intro -->
<article id="intro">
<h2 class="major">Over</h2>
<span class="image main"><img
src="images/bummie.png" alt="" /></span>
<p>Al van kleinsaf was ik geïnteresseerd in het
lakken van nagels, me opmaken,.. Ook mijn
mama moest dit ondervinden. Deze passie
bloeide verder wanneer ik een richting moest
kiezen. Dit werd natuurlijk bio-esthetiek.
Toen ik afgestudeerd was heb ik nog een 7de
specialisatie jaar gevolgd en aan de hand van
een mini-onderneming en stages ondervonden
hoe het is om in het werkveld te staan. Dit
alles heeft ervoor gezorgd dat "Clein."
ontstaan is.</p>
<p>Als productenlijn heb ik gekozen voor het
luxemerk Germaine de Capuccini. Deze staan in
de top 5 van internationale cosmeticamerken.
Met de natuurlijke ingrediënten en
innovatieve verzorgingen wordt uw huid
optimaal verzorgd.</p>
</article>
<!-- Gallery -->
<article id="gallery">
<h2 class="major">Gallerij</h2>
<a href="images/img0.jpg" data-
lightbox="gallery"><img
src="images/pic0.jpg"></a>
<a href="images/img1.jpg" data-
lightbox="gallery"><img src="images/pic1.jpg"></a>
<a href="images/img2.jpg" data-
lightbox="gallery"><img src="images/pic2.jpg"></a>
<a href="images/img3.jpg" data-
lightbox="gallery"><img src="images/pic3.jpg"></a>
<a href="images/img4.jpg" data-
lightbox="gallery"><img src="images/pic4.jpg"></a>
</article>
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/skel.min.js"></script>
<script src="assets/js/util.js"></script>
<script src="assets/js/main.js"></script>
<script src="assets/js/lightbox.min.js"></script>
表达式中再次分配给它:在一种情况下,我将其分配给自己,在另一种情况下,我将其赋值给它。
答案 1 :(得分:2)
在PL / SQL中,您需要执行SELECT ... INTO。所以,要重新编写代码:
SET SERVEROUTPUT ON
DECLARE
LV_VAR VARCHAR2(4000):=NULL;
BEGIN
SELECT CASE
WHEN 1=1 then 1
else 0
end
INTO LV_VAR
FROM DUAL;
DBMS_OUTPUT.PUT_LINE(LV_VAR);
END;
答案 2 :(得分:1)