一次CASE
语句可以select
ColumnA = case when CheckColumn='condition' then 'result1' end
,ColumnB = case when CheckColumn='condition' then 'result2' end
两列或更多列的值吗?我的意思是代替:
select case when CheckColumn='condition' then ColumnA='result1', ColumnB='result2' end
类似的东西:
update CTE
set ColumnA='result1', ColumnB='result2'
where CheckColumn='condition'
更新
与我们使用UPDATE语句一样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Header aligned to top -->
<!-- Footer aligned to bottom -->
<RelativeLayout
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fffff"
android:gravity="center" >
<TextView
android:id="@+id/copyrightext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:singleLine="true"
android:textColor="#ffffff"
android:textSize="12dp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="70dp"
android:layout_height="14dp"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/copyrightext"
android:src="@drawable/softagelogo_icon" />
</RelativeLayout>
<!-- Content below header and above footer -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/footer"
android:gravity="center" >
<com.example.softageinfraapp.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@drawable/tabbackground"
app:pstsShouldExpand="true"
app:pstsTextAllCaps="true" >
</com.example.softageinfraapp.PagerSlidingTabStrip>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@android:color/black" />
</RelativeLayout>
</RelativeLayout>
答案 0 :(得分:1)
CASE
表达式无法实现。
对于每列,您需要新的CASE
答案 1 :(得分:1)
这是不可能的,但您可以使用table value constructor作为解决方法,将columna和columnb的每个值存储在检查列中:
SELECT t.CheckColumn,
v.ColumnA,
v.ColumnB
FROM dbo.YourTable AS t
LEFT JOIN
(VALUES
('Condition1', 'Result1', 'Result2'),
('Condition2', 'Result3', 'Result4'),
('Condition3', 'Result5', 'Result6')
) AS v (CheckColumn, ColumnA, ColumnB)
ON v.CheckColumn = t.CheckColumn;
如果您有更复杂的条件,那么您仍然可以应用此逻辑,但只需使用伪结果进行连接:
SELECT t.CheckColumn,
v.ColumnA,
v.ColumnB
FROM dbo.YourTable AS t
LEFT JOIN
(VALUES
(1, 'Result1', 'Result2'),
(2, 'Result3', 'Result4'),
(3, 'Result5', 'Result6')
) AS v (ConditionID, ColumnA, ColumnB)
ON v.ConditionID = CASE WHEN <some long expression> THEN 1
WHEN <some other long expression> THEN 2
ELSE 3
END;
答案 2 :(得分:1)
select
的等效update
是:
select 'result1', 'result2'
. . .
where CheckColumn = 'condition';
您的select
不同,因为它会生成NULL
个值。使用outer apply
:
select t2.*
from . . . outer apply
(select t.*
from (select 'result1' as col1, 'result2' as col2) t
where CheckColumn = 'condition'
) t2;
当没有匹配时,这将返回NULL
个值。并且,您可以拥有任意数量的列。
答案 3 :(得分:0)
我从您的问题中了解到,如果某些条件为真,您希望更新多个列。
对于这种情况,您必须使用MERGE语句。
使用MERGE的示例如msdn here所示。
代码示例:
-- MERGE statement for update.
USE [Database Name];
GO
MERGE Inventory ity
USING Order ord
ON ity.ProductID = ord.ProductID
WHEN MATCHED THEN
UPDATE
SET ity.Quantity = ity.Quantity - ord.Quantity;
更多MERGE语句示例here。
答案 4 :(得分:0)
你可以通过CTE或CROSS APPLY解决这个问题,有点像
DECLARE @tbl2 TABLE(inx INT, val1 VARCHAR(10),val2 VARCHAR(10));
INSERT INTO @tbl2 VALUES(1,'value1a','value1b'),(2,'value2a','value2b'),(3,'value2a','value2b');
UPDATE yourTable SET col1=subTable.val1,col2=subTable.val2
FROM yourTable
CROSS APPLY(
SELECT val1,val2
FROM @tbl2
WHERE inx=1 --YourCondition
) AS subTable