T-SQL Merge - 匹配更新列时和不匹配时插入整行

时间:2017-06-11 12:31:25

标签: sql sql-server tsql

我有两张桌子;

DummyTable

Column1 | Column2 | Column3
a       | b       | c

GoodTable

Column1 | Column2 | Column3
a       | b       | c

我是T-SQL的新手,需要帮助做以下事情;

使用条件

将数据从DummyTable合并到GoodTable中
  • 条件:当DummyTable的Column2与GoodTable的Column2匹配时,然后是 使用DummyTable的第3列更新GoodTable的Column3,不匹配时更新,将DummyTable的行插入GoodTable

以下是我的不工作尝试

MERGE INTO GoodTable AS Target
USING (VALUES (SELECT Column2 FROM DummyTable)) AS source (Column2)
ON Target.Column2 = Source.Column2
WHEN MATCHED BY TARGET THEN
UPDATE (Column2) VALUES (Column2)
WHEN NOT MATCHED BY TARGET THEN
INSERT INTO GoodTable VALUES DummyTable

1 个答案:

答案 0 :(得分:0)

看看这是否适合您。这是基于您的逻辑。

@Override
    public void bindView(View view, Context context, Cursor cursor){
        int row_id = cursor.get('_id');  //Your row id (might need to replace)
        Button button = (Button) view.findViewById(R.id.buttonEE);
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                //ADD STUFF HERE you know which row is clicked. and which button
            }
        });
    }