sql if函数中的语句

时间:2018-07-12 08:31:23

标签: sql if-statement switch-statement

我在Visual Basic中有一个功能,向该功能发送a和b变量。它执行很多选择语句,如下所示:

SQL代码

var maps = {
"type": "FeatureCollection",
"name": "maps",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "IDLo": "1", "SoHieu": "1-1", "KyHieu": "C", "TenLo": "C1-1", "TenCty": "CMC Data Center" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 106.791800519464871, 10.854928689692501 ], [ 106.792069337729856, 10.855930098557222 ], [ 106.792653322236532, 10.855766231881775 ], [ 106.79231961680415, 10.854783029941672 ], [ 106.791800519464871, 10.854928689692501 ] ] ] } },
{ "type": "Feature", "properties": { "IDLo": "2", "SoHieu": "1-2", "KyHieu": "C", "TenLo": "C1-2", "TenCty": "ASCENDAS" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 106.79264868743887, 10.855779887550094 ], [ 106.792064702932166, 10.855943754285464 ], [ 106.791786615071828, 10.854942345054598 ], [ 106.79101723865827, 10.855151730898562 ], [ 106.790461062937595, 10.855306494254153 ], [ 106.789969774384346, 10.855424842648457 ], [ 106.789478485831097, 10.855688850436046 ], [ 106.78819928167357, 10.857819111634392 ], [ 106.790915273109462, 10.859412245764197 ], [ 106.791907119811313, 10.857746282442497 ], [ 106.792111050908886, 10.857518691103408 ], [ 106.792379869173871, 10.857291099590915 ], [ 106.792583800271444, 10.856999782201919 ], [ 106.792732113796944, 10.856544598212894 ], [ 106.792741383392297, 10.856116724630859 ], [ 106.79264868743887, 10.855779887550094 ] ] ] } },

我想再加上一个if语句并集。 我要运行Select xxx where a=@a union Select yyy where u=@a 的一部分,if @b=1的另一部分

if @b=2

我想学习如何将联合与if语句一起使用以及正确的语法。

union

if @b=1

select zzz

if @b=2

select ttt

对吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

我认为您可以将支票放在WHERE子句中。如果它们失败了,那么将选择行,因此不会将任何行添加到总体结果中。

...
UNION
SELECT ...
       WHERE @b = 1
UNION
SELECT ...
       WHERE @b = 2;