SQL Logic : To implement Case updates using 3 tables
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am looking for SQL to update using 3 tables.
For example:
Table 1:
ID Address Name MutipleID
1 ABC Apple 123xyz
2 DEF Mango 456stu
3 HFG Orange 789wac
Table 2:
ID ADDRESS NAME MutipleID
1 ABC null 123xyz
2 null Mango 456stu
null HGF null 789wac
Table 3:
ID ADDRESS NAME MutlipleID
T 123xyz
null T 456stu
null null T 789wac
so, my logic here should be like,
- 'Case 1: Table1.ID = Table2.id --> Update table 3.ID = T Else'
- 'Case 2: Table1.Address = table2.address --> Update table3. address = 'T'
else' - 'Case 3: Table1.Name = table2.name --> Update Table3.Name = 'T''
my table3 is mainly a lookup table to check which columns matched from TABLE 1 & 2..
All these 3 tables have one common column "MutilpleID" which is their linking column to make udpates
I'm not sure how to implement this logic.
sql
|
show 1 more comment
I am looking for SQL to update using 3 tables.
For example:
Table 1:
ID Address Name MutipleID
1 ABC Apple 123xyz
2 DEF Mango 456stu
3 HFG Orange 789wac
Table 2:
ID ADDRESS NAME MutipleID
1 ABC null 123xyz
2 null Mango 456stu
null HGF null 789wac
Table 3:
ID ADDRESS NAME MutlipleID
T 123xyz
null T 456stu
null null T 789wac
so, my logic here should be like,
- 'Case 1: Table1.ID = Table2.id --> Update table 3.ID = T Else'
- 'Case 2: Table1.Address = table2.address --> Update table3. address = 'T'
else' - 'Case 3: Table1.Name = table2.name --> Update Table3.Name = 'T''
my table3 is mainly a lookup table to check which columns matched from TABLE 1 & 2..
All these 3 tables have one common column "MutilpleID" which is their linking column to make udpates
I'm not sure how to implement this logic.
sql
1
Your explanation isn't clear. Please provide desired results.
– Tab Alleman
Jan 3 at 20:20
How is table 3 related to 1 and 2? This isn't clear in the data shown.... since the ID isn't even an integer for table 3
– scsimon
Jan 3 at 20:21
the explanation is not only unclear, if I read it correctly it actually "works" just for 3 rows (and it actually doesn't really work because table 3 has no value to link to the other tables)
– Lamak
Jan 3 at 20:22
All tables would need a non null and unique column to define the order. Otherwise this won't work as relations are (multi) sets and these are unordered. Can you provide such a column?
– sticky bit
Jan 3 at 20:26
Hey, Yes I forgot to mention there is a column called "multipleID" which is common for all these 3 tables.. I would need to do the updates based off this common field
– atulya dharmaraj
Jan 3 at 20:36
|
show 1 more comment
I am looking for SQL to update using 3 tables.
For example:
Table 1:
ID Address Name MutipleID
1 ABC Apple 123xyz
2 DEF Mango 456stu
3 HFG Orange 789wac
Table 2:
ID ADDRESS NAME MutipleID
1 ABC null 123xyz
2 null Mango 456stu
null HGF null 789wac
Table 3:
ID ADDRESS NAME MutlipleID
T 123xyz
null T 456stu
null null T 789wac
so, my logic here should be like,
- 'Case 1: Table1.ID = Table2.id --> Update table 3.ID = T Else'
- 'Case 2: Table1.Address = table2.address --> Update table3. address = 'T'
else' - 'Case 3: Table1.Name = table2.name --> Update Table3.Name = 'T''
my table3 is mainly a lookup table to check which columns matched from TABLE 1 & 2..
All these 3 tables have one common column "MutilpleID" which is their linking column to make udpates
I'm not sure how to implement this logic.
sql
I am looking for SQL to update using 3 tables.
For example:
Table 1:
ID Address Name MutipleID
1 ABC Apple 123xyz
2 DEF Mango 456stu
3 HFG Orange 789wac
Table 2:
ID ADDRESS NAME MutipleID
1 ABC null 123xyz
2 null Mango 456stu
null HGF null 789wac
Table 3:
ID ADDRESS NAME MutlipleID
T 123xyz
null T 456stu
null null T 789wac
so, my logic here should be like,
- 'Case 1: Table1.ID = Table2.id --> Update table 3.ID = T Else'
- 'Case 2: Table1.Address = table2.address --> Update table3. address = 'T'
else' - 'Case 3: Table1.Name = table2.name --> Update Table3.Name = 'T''
my table3 is mainly a lookup table to check which columns matched from TABLE 1 & 2..
All these 3 tables have one common column "MutilpleID" which is their linking column to make udpates
I'm not sure how to implement this logic.
sql
sql
edited Jan 3 at 21:02
DeanOC
5,51563246
5,51563246
asked Jan 3 at 20:16
atulya dharmarajatulya dharmaraj
13
13
1
Your explanation isn't clear. Please provide desired results.
– Tab Alleman
Jan 3 at 20:20
How is table 3 related to 1 and 2? This isn't clear in the data shown.... since the ID isn't even an integer for table 3
– scsimon
Jan 3 at 20:21
the explanation is not only unclear, if I read it correctly it actually "works" just for 3 rows (and it actually doesn't really work because table 3 has no value to link to the other tables)
– Lamak
Jan 3 at 20:22
All tables would need a non null and unique column to define the order. Otherwise this won't work as relations are (multi) sets and these are unordered. Can you provide such a column?
– sticky bit
Jan 3 at 20:26
Hey, Yes I forgot to mention there is a column called "multipleID" which is common for all these 3 tables.. I would need to do the updates based off this common field
– atulya dharmaraj
Jan 3 at 20:36
|
show 1 more comment
1
Your explanation isn't clear. Please provide desired results.
– Tab Alleman
Jan 3 at 20:20
How is table 3 related to 1 and 2? This isn't clear in the data shown.... since the ID isn't even an integer for table 3
– scsimon
Jan 3 at 20:21
the explanation is not only unclear, if I read it correctly it actually "works" just for 3 rows (and it actually doesn't really work because table 3 has no value to link to the other tables)
– Lamak
Jan 3 at 20:22
All tables would need a non null and unique column to define the order. Otherwise this won't work as relations are (multi) sets and these are unordered. Can you provide such a column?
– sticky bit
Jan 3 at 20:26
Hey, Yes I forgot to mention there is a column called "multipleID" which is common for all these 3 tables.. I would need to do the updates based off this common field
– atulya dharmaraj
Jan 3 at 20:36
1
1
Your explanation isn't clear. Please provide desired results.
– Tab Alleman
Jan 3 at 20:20
Your explanation isn't clear. Please provide desired results.
– Tab Alleman
Jan 3 at 20:20
How is table 3 related to 1 and 2? This isn't clear in the data shown.... since the ID isn't even an integer for table 3
– scsimon
Jan 3 at 20:21
How is table 3 related to 1 and 2? This isn't clear in the data shown.... since the ID isn't even an integer for table 3
– scsimon
Jan 3 at 20:21
the explanation is not only unclear, if I read it correctly it actually "works" just for 3 rows (and it actually doesn't really work because table 3 has no value to link to the other tables)
– Lamak
Jan 3 at 20:22
the explanation is not only unclear, if I read it correctly it actually "works" just for 3 rows (and it actually doesn't really work because table 3 has no value to link to the other tables)
– Lamak
Jan 3 at 20:22
All tables would need a non null and unique column to define the order. Otherwise this won't work as relations are (multi) sets and these are unordered. Can you provide such a column?
– sticky bit
Jan 3 at 20:26
All tables would need a non null and unique column to define the order. Otherwise this won't work as relations are (multi) sets and these are unordered. Can you provide such a column?
– sticky bit
Jan 3 at 20:26
Hey, Yes I forgot to mention there is a column called "multipleID" which is common for all these 3 tables.. I would need to do the updates based off this common field
– atulya dharmaraj
Jan 3 at 20:36
Hey, Yes I forgot to mention there is a column called "multipleID" which is common for all these 3 tables.. I would need to do the updates based off this common field
– atulya dharmaraj
Jan 3 at 20:36
|
show 1 more comment
2 Answers
2
active
oldest
votes
I see it in this way:
update Table3
set
ID = case when t1.ID=t2.ID then 'T' else null end,
ADDRESS = case when t1.ID<>t2.ID and t1.Address=t2.ADDRESS then 'T' else null end,
NAME = case when t1.ID<>t2.ID and t1.Address<>t2.ADDRESS
and t1.Name=t2.NAME then 'T' else null end
from Table3 t3
join Table2 t2 on (t3.MultipleID=t2.MultipleID)
join Table1 t1 on (t2.MultipleID=t1.MultipleID)
add a comment |
This can be done within a Data Flow Task by using a Lookup component to match table 1 and table 2. From this point the rows can then redirected based off the values in the ID, Address, and Name columns and table 3 updated accordingly. Of course some updates for your exact table and column names will be needed, but an overview of this process is listed below.
- Create a Data Flow Task with an OLE DB source that selects the necessary columns from table 1.
- Add a Lookup component next and connect the OLE DB source to it. Since the
MutipleIDcolumn contains text I'd recommend using theNo cacheoption to avoid potential collation/case comparison issues unless you want to match by case. By default the Lookup will fail rows without a corresponding match so you will want to either use theRedirect Rows to no Match OutputorIgnore Failureoption for handling rows without matching entries (on the General pane of the Lookup editor). - On the Connection pane of the Lookup use the SQL query option and enter a query that returns only the needed columns from table two. Don't use the table/view option unless you need all of the columns the table contains, as this will add unnecessary columns otherwise.
- On the Columns page, link the
MutipleIDcolumn from both sources. Next add the columns from table 2 to the data flow by checking the box next to each column and selecting the<add as new column>option in the Lookup Operation field. Give them a descriptive name in the Output Alias field, such as LKUP_ID. - Next add a Conditional Split in the Data Flow Task and connect this to the
Lookup Match Outputoutput. For the first condition add a comparison for the ID column of both tables. Note that if these are null this will cause an error. To avoid this, wrap each column in theREPLACENULLfunction. If you don't want to match nulls use a different value for the second argument on each column. An example of this is at the end of this post (the example doesn't match nulls). The Conditional Split will direct the rows to only one output, being the first output with a condition that returns true. Going by the order in your question, add a condition for the ID columns, followed by the Address and then Name column last. Give each output a name that matches the condition, i.e. Matching IDs Output for the ID comparison. - Add three OLE DB Commands and connect each one to one of the outputs you created. In the OLE DB Command editor select the same Connection Manager. On the Component Properties page enter an update command that will update the corresponding column in table 3 based off the match that was done in the Conditional Split. An example of this is below. This example uses the first condition, matching IDs. The question mark is a parameter placeholder. The parameter will be mapped on the Column Mappings page. On this page, drag/link the
MutlipleIDcolumn from the "Available Input Columns" box to the parameter (likely label Param_0) in the "Available Destination Columns" box. This will useMutlipleIDcolumn from the data flow to match to the corresponding row in table 3. Update the other OLE DB Command connected to the matching address output to make the same update to the Address column and repeat the same step for the Name output and column as well. - The OLE DB Commands do operate on a row-by-row basis, so if you're working with a large volume of data I'd suggest replacing these with staging tables and doing the update as a set based update via a join to table 3. For example replacing the OLE DB Commands with staging tables as OLE DB Destinations and doing a set-based update via a T-SQL join in an Execute SQL Task after the Data Flow Task.
Conditional Split Example Condition:
REPLACENULL(ID, 0) == REPLACENULL(LKUP_ID, 1)
OLE DB Command Update SQL:
UPDATE YourSchema.Table3 SET ID = 'T' WHERE MultipleID = ?
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029196%2fsql-logic-to-implement-case-updates-using-3-tables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I see it in this way:
update Table3
set
ID = case when t1.ID=t2.ID then 'T' else null end,
ADDRESS = case when t1.ID<>t2.ID and t1.Address=t2.ADDRESS then 'T' else null end,
NAME = case when t1.ID<>t2.ID and t1.Address<>t2.ADDRESS
and t1.Name=t2.NAME then 'T' else null end
from Table3 t3
join Table2 t2 on (t3.MultipleID=t2.MultipleID)
join Table1 t1 on (t2.MultipleID=t1.MultipleID)
add a comment |
I see it in this way:
update Table3
set
ID = case when t1.ID=t2.ID then 'T' else null end,
ADDRESS = case when t1.ID<>t2.ID and t1.Address=t2.ADDRESS then 'T' else null end,
NAME = case when t1.ID<>t2.ID and t1.Address<>t2.ADDRESS
and t1.Name=t2.NAME then 'T' else null end
from Table3 t3
join Table2 t2 on (t3.MultipleID=t2.MultipleID)
join Table1 t1 on (t2.MultipleID=t1.MultipleID)
add a comment |
I see it in this way:
update Table3
set
ID = case when t1.ID=t2.ID then 'T' else null end,
ADDRESS = case when t1.ID<>t2.ID and t1.Address=t2.ADDRESS then 'T' else null end,
NAME = case when t1.ID<>t2.ID and t1.Address<>t2.ADDRESS
and t1.Name=t2.NAME then 'T' else null end
from Table3 t3
join Table2 t2 on (t3.MultipleID=t2.MultipleID)
join Table1 t1 on (t2.MultipleID=t1.MultipleID)
I see it in this way:
update Table3
set
ID = case when t1.ID=t2.ID then 'T' else null end,
ADDRESS = case when t1.ID<>t2.ID and t1.Address=t2.ADDRESS then 'T' else null end,
NAME = case when t1.ID<>t2.ID and t1.Address<>t2.ADDRESS
and t1.Name=t2.NAME then 'T' else null end
from Table3 t3
join Table2 t2 on (t3.MultipleID=t2.MultipleID)
join Table1 t1 on (t2.MultipleID=t1.MultipleID)
edited Jan 3 at 21:14
answered Jan 3 at 21:02
XabiXabi
19626
19626
add a comment |
add a comment |
This can be done within a Data Flow Task by using a Lookup component to match table 1 and table 2. From this point the rows can then redirected based off the values in the ID, Address, and Name columns and table 3 updated accordingly. Of course some updates for your exact table and column names will be needed, but an overview of this process is listed below.
- Create a Data Flow Task with an OLE DB source that selects the necessary columns from table 1.
- Add a Lookup component next and connect the OLE DB source to it. Since the
MutipleIDcolumn contains text I'd recommend using theNo cacheoption to avoid potential collation/case comparison issues unless you want to match by case. By default the Lookup will fail rows without a corresponding match so you will want to either use theRedirect Rows to no Match OutputorIgnore Failureoption for handling rows without matching entries (on the General pane of the Lookup editor). - On the Connection pane of the Lookup use the SQL query option and enter a query that returns only the needed columns from table two. Don't use the table/view option unless you need all of the columns the table contains, as this will add unnecessary columns otherwise.
- On the Columns page, link the
MutipleIDcolumn from both sources. Next add the columns from table 2 to the data flow by checking the box next to each column and selecting the<add as new column>option in the Lookup Operation field. Give them a descriptive name in the Output Alias field, such as LKUP_ID. - Next add a Conditional Split in the Data Flow Task and connect this to the
Lookup Match Outputoutput. For the first condition add a comparison for the ID column of both tables. Note that if these are null this will cause an error. To avoid this, wrap each column in theREPLACENULLfunction. If you don't want to match nulls use a different value for the second argument on each column. An example of this is at the end of this post (the example doesn't match nulls). The Conditional Split will direct the rows to only one output, being the first output with a condition that returns true. Going by the order in your question, add a condition for the ID columns, followed by the Address and then Name column last. Give each output a name that matches the condition, i.e. Matching IDs Output for the ID comparison. - Add three OLE DB Commands and connect each one to one of the outputs you created. In the OLE DB Command editor select the same Connection Manager. On the Component Properties page enter an update command that will update the corresponding column in table 3 based off the match that was done in the Conditional Split. An example of this is below. This example uses the first condition, matching IDs. The question mark is a parameter placeholder. The parameter will be mapped on the Column Mappings page. On this page, drag/link the
MutlipleIDcolumn from the "Available Input Columns" box to the parameter (likely label Param_0) in the "Available Destination Columns" box. This will useMutlipleIDcolumn from the data flow to match to the corresponding row in table 3. Update the other OLE DB Command connected to the matching address output to make the same update to the Address column and repeat the same step for the Name output and column as well. - The OLE DB Commands do operate on a row-by-row basis, so if you're working with a large volume of data I'd suggest replacing these with staging tables and doing the update as a set based update via a join to table 3. For example replacing the OLE DB Commands with staging tables as OLE DB Destinations and doing a set-based update via a T-SQL join in an Execute SQL Task after the Data Flow Task.
Conditional Split Example Condition:
REPLACENULL(ID, 0) == REPLACENULL(LKUP_ID, 1)
OLE DB Command Update SQL:
UPDATE YourSchema.Table3 SET ID = 'T' WHERE MultipleID = ?
add a comment |
This can be done within a Data Flow Task by using a Lookup component to match table 1 and table 2. From this point the rows can then redirected based off the values in the ID, Address, and Name columns and table 3 updated accordingly. Of course some updates for your exact table and column names will be needed, but an overview of this process is listed below.
- Create a Data Flow Task with an OLE DB source that selects the necessary columns from table 1.
- Add a Lookup component next and connect the OLE DB source to it. Since the
MutipleIDcolumn contains text I'd recommend using theNo cacheoption to avoid potential collation/case comparison issues unless you want to match by case. By default the Lookup will fail rows without a corresponding match so you will want to either use theRedirect Rows to no Match OutputorIgnore Failureoption for handling rows without matching entries (on the General pane of the Lookup editor). - On the Connection pane of the Lookup use the SQL query option and enter a query that returns only the needed columns from table two. Don't use the table/view option unless you need all of the columns the table contains, as this will add unnecessary columns otherwise.
- On the Columns page, link the
MutipleIDcolumn from both sources. Next add the columns from table 2 to the data flow by checking the box next to each column and selecting the<add as new column>option in the Lookup Operation field. Give them a descriptive name in the Output Alias field, such as LKUP_ID. - Next add a Conditional Split in the Data Flow Task and connect this to the
Lookup Match Outputoutput. For the first condition add a comparison for the ID column of both tables. Note that if these are null this will cause an error. To avoid this, wrap each column in theREPLACENULLfunction. If you don't want to match nulls use a different value for the second argument on each column. An example of this is at the end of this post (the example doesn't match nulls). The Conditional Split will direct the rows to only one output, being the first output with a condition that returns true. Going by the order in your question, add a condition for the ID columns, followed by the Address and then Name column last. Give each output a name that matches the condition, i.e. Matching IDs Output for the ID comparison. - Add three OLE DB Commands and connect each one to one of the outputs you created. In the OLE DB Command editor select the same Connection Manager. On the Component Properties page enter an update command that will update the corresponding column in table 3 based off the match that was done in the Conditional Split. An example of this is below. This example uses the first condition, matching IDs. The question mark is a parameter placeholder. The parameter will be mapped on the Column Mappings page. On this page, drag/link the
MutlipleIDcolumn from the "Available Input Columns" box to the parameter (likely label Param_0) in the "Available Destination Columns" box. This will useMutlipleIDcolumn from the data flow to match to the corresponding row in table 3. Update the other OLE DB Command connected to the matching address output to make the same update to the Address column and repeat the same step for the Name output and column as well. - The OLE DB Commands do operate on a row-by-row basis, so if you're working with a large volume of data I'd suggest replacing these with staging tables and doing the update as a set based update via a join to table 3. For example replacing the OLE DB Commands with staging tables as OLE DB Destinations and doing a set-based update via a T-SQL join in an Execute SQL Task after the Data Flow Task.
Conditional Split Example Condition:
REPLACENULL(ID, 0) == REPLACENULL(LKUP_ID, 1)
OLE DB Command Update SQL:
UPDATE YourSchema.Table3 SET ID = 'T' WHERE MultipleID = ?
add a comment |
This can be done within a Data Flow Task by using a Lookup component to match table 1 and table 2. From this point the rows can then redirected based off the values in the ID, Address, and Name columns and table 3 updated accordingly. Of course some updates for your exact table and column names will be needed, but an overview of this process is listed below.
- Create a Data Flow Task with an OLE DB source that selects the necessary columns from table 1.
- Add a Lookup component next and connect the OLE DB source to it. Since the
MutipleIDcolumn contains text I'd recommend using theNo cacheoption to avoid potential collation/case comparison issues unless you want to match by case. By default the Lookup will fail rows without a corresponding match so you will want to either use theRedirect Rows to no Match OutputorIgnore Failureoption for handling rows without matching entries (on the General pane of the Lookup editor). - On the Connection pane of the Lookup use the SQL query option and enter a query that returns only the needed columns from table two. Don't use the table/view option unless you need all of the columns the table contains, as this will add unnecessary columns otherwise.
- On the Columns page, link the
MutipleIDcolumn from both sources. Next add the columns from table 2 to the data flow by checking the box next to each column and selecting the<add as new column>option in the Lookup Operation field. Give them a descriptive name in the Output Alias field, such as LKUP_ID. - Next add a Conditional Split in the Data Flow Task and connect this to the
Lookup Match Outputoutput. For the first condition add a comparison for the ID column of both tables. Note that if these are null this will cause an error. To avoid this, wrap each column in theREPLACENULLfunction. If you don't want to match nulls use a different value for the second argument on each column. An example of this is at the end of this post (the example doesn't match nulls). The Conditional Split will direct the rows to only one output, being the first output with a condition that returns true. Going by the order in your question, add a condition for the ID columns, followed by the Address and then Name column last. Give each output a name that matches the condition, i.e. Matching IDs Output for the ID comparison. - Add three OLE DB Commands and connect each one to one of the outputs you created. In the OLE DB Command editor select the same Connection Manager. On the Component Properties page enter an update command that will update the corresponding column in table 3 based off the match that was done in the Conditional Split. An example of this is below. This example uses the first condition, matching IDs. The question mark is a parameter placeholder. The parameter will be mapped on the Column Mappings page. On this page, drag/link the
MutlipleIDcolumn from the "Available Input Columns" box to the parameter (likely label Param_0) in the "Available Destination Columns" box. This will useMutlipleIDcolumn from the data flow to match to the corresponding row in table 3. Update the other OLE DB Command connected to the matching address output to make the same update to the Address column and repeat the same step for the Name output and column as well. - The OLE DB Commands do operate on a row-by-row basis, so if you're working with a large volume of data I'd suggest replacing these with staging tables and doing the update as a set based update via a join to table 3. For example replacing the OLE DB Commands with staging tables as OLE DB Destinations and doing a set-based update via a T-SQL join in an Execute SQL Task after the Data Flow Task.
Conditional Split Example Condition:
REPLACENULL(ID, 0) == REPLACENULL(LKUP_ID, 1)
OLE DB Command Update SQL:
UPDATE YourSchema.Table3 SET ID = 'T' WHERE MultipleID = ?
This can be done within a Data Flow Task by using a Lookup component to match table 1 and table 2. From this point the rows can then redirected based off the values in the ID, Address, and Name columns and table 3 updated accordingly. Of course some updates for your exact table and column names will be needed, but an overview of this process is listed below.
- Create a Data Flow Task with an OLE DB source that selects the necessary columns from table 1.
- Add a Lookup component next and connect the OLE DB source to it. Since the
MutipleIDcolumn contains text I'd recommend using theNo cacheoption to avoid potential collation/case comparison issues unless you want to match by case. By default the Lookup will fail rows without a corresponding match so you will want to either use theRedirect Rows to no Match OutputorIgnore Failureoption for handling rows without matching entries (on the General pane of the Lookup editor). - On the Connection pane of the Lookup use the SQL query option and enter a query that returns only the needed columns from table two. Don't use the table/view option unless you need all of the columns the table contains, as this will add unnecessary columns otherwise.
- On the Columns page, link the
MutipleIDcolumn from both sources. Next add the columns from table 2 to the data flow by checking the box next to each column and selecting the<add as new column>option in the Lookup Operation field. Give them a descriptive name in the Output Alias field, such as LKUP_ID. - Next add a Conditional Split in the Data Flow Task and connect this to the
Lookup Match Outputoutput. For the first condition add a comparison for the ID column of both tables. Note that if these are null this will cause an error. To avoid this, wrap each column in theREPLACENULLfunction. If you don't want to match nulls use a different value for the second argument on each column. An example of this is at the end of this post (the example doesn't match nulls). The Conditional Split will direct the rows to only one output, being the first output with a condition that returns true. Going by the order in your question, add a condition for the ID columns, followed by the Address and then Name column last. Give each output a name that matches the condition, i.e. Matching IDs Output for the ID comparison. - Add three OLE DB Commands and connect each one to one of the outputs you created. In the OLE DB Command editor select the same Connection Manager. On the Component Properties page enter an update command that will update the corresponding column in table 3 based off the match that was done in the Conditional Split. An example of this is below. This example uses the first condition, matching IDs. The question mark is a parameter placeholder. The parameter will be mapped on the Column Mappings page. On this page, drag/link the
MutlipleIDcolumn from the "Available Input Columns" box to the parameter (likely label Param_0) in the "Available Destination Columns" box. This will useMutlipleIDcolumn from the data flow to match to the corresponding row in table 3. Update the other OLE DB Command connected to the matching address output to make the same update to the Address column and repeat the same step for the Name output and column as well. - The OLE DB Commands do operate on a row-by-row basis, so if you're working with a large volume of data I'd suggest replacing these with staging tables and doing the update as a set based update via a join to table 3. For example replacing the OLE DB Commands with staging tables as OLE DB Destinations and doing a set-based update via a T-SQL join in an Execute SQL Task after the Data Flow Task.
Conditional Split Example Condition:
REPLACENULL(ID, 0) == REPLACENULL(LKUP_ID, 1)
OLE DB Command Update SQL:
UPDATE YourSchema.Table3 SET ID = 'T' WHERE MultipleID = ?
answered Jan 4 at 19:43
userfl89userfl89
3,2991515
3,2991515
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54029196%2fsql-logic-to-implement-case-updates-using-3-tables%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Your explanation isn't clear. Please provide desired results.
– Tab Alleman
Jan 3 at 20:20
How is table 3 related to 1 and 2? This isn't clear in the data shown.... since the ID isn't even an integer for table 3
– scsimon
Jan 3 at 20:21
the explanation is not only unclear, if I read it correctly it actually "works" just for 3 rows (and it actually doesn't really work because table 3 has no value to link to the other tables)
– Lamak
Jan 3 at 20:22
All tables would need a non null and unique column to define the order. Otherwise this won't work as relations are (multi) sets and these are unordered. Can you provide such a column?
– sticky bit
Jan 3 at 20:26
Hey, Yes I forgot to mention there is a column called "multipleID" which is common for all these 3 tables.. I would need to do the updates based off this common field
– atulya dharmaraj
Jan 3 at 20:36