20 lines
2.5 KiB
HTML
20 lines
2.5 KiB
HTML
<p>sql server支持变量所以一般使用方法如下:</p>
|
||
<div class="cnblogs_code">
|
||
<pre><span style="color: #0000ff;">DECLARE</span> <span style="color: #008000;">@Val</span> <span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">50</span><span style="color: #000000;">)
|
||
</span><span style="color: #0000ff;">select</span> <span style="color: #008000;">@Val</span> <span style="color: #808080;">=</span> param_value <span style="color: #0000ff;">where</span> t_param <span style="color: #0000ff;">where</span> param_name <span style="color: #808080;">=</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">log_type</span><span style="color: #ff0000;">'</span>
|
||
<span style="color: #0000ff;">if</span>(<span style="color: #008000;">@Val</span> <span style="color: #0000ff;">is</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">)
|
||
</span><span style="color: #0000ff;">begin</span>
|
||
<span style="color: #0000ff;">select</span> <span style="color: #008000;">@Val</span> <span style="color: #808080;">=</span> param_default_value <span style="color: #0000ff;">where</span> t_param_info <span style="color: #0000ff;">where</span> param_name <span style="color: #808080;">=</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">log_type</span><span style="color: #ff0000;">'</span>
|
||
<span style="color: #0000ff;">end</span>
|
||
<span style="color: #0000ff;">select</span> <span style="color: #008000;">@Val</span></pre>
|
||
</div>
|
||
<p> </p>
|
||
<p>mysql中执行语句时不支持定义变量及条件判断,所以想了个办法,也能达到相同目的</p>
|
||
<p> </p>
|
||
<div class="cnblogs_code">
|
||
<pre><span style="color: #0000ff;">SELECT</span><span style="color: #000000;"> IFNULL(
|
||
(</span><span style="color: #0000ff;">select</span> param_value <span style="color: #0000ff;">from</span> t_param <span style="color: #0000ff;">where</span> param_name <span style="color: #808080;">=</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">log_type</span><span style="color: #ff0000;">'</span><span style="color: #000000;">),
|
||
(</span><span style="color: #0000ff;">select</span> param_default_value <span style="color: #0000ff;">from</span> t_param_info <span style="color: #0000ff;">where</span> param_name <span style="color: #808080;">=</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">log_type</span><span style="color: #ff0000;">'</span><span style="color: #000000;">)
|
||
) </span><span style="color: #0000ff;">as</span> param_value;</pre>
|
||
</div>
|
||
<p> </p> |