Insert Timestamp on value change from other table
I have an EvidenceTimeStamp table that is triggered with data when the case table has data inserted into it.
EvidenceTimeStamp:
CaseIDEvidenceNumEvidenceTypeEvidence_Initiated_TimestampEvidence_Returned_Timestamp
I created a trigger to fill the first four columns of data whenever a new case is inserted in the case table and it creates a timestamp on Evidence_Initiated_Timestamp when this happens.
However, I need to create a closing timestamp when the process is complete. I wanted to do this by my Outbound Ship Table- everytime the Attribute (bit value) Outbound_Item_Recevied is changed from false to true (or 0 to 1) I want it to initate a timestamp on the Evidence_Returned_Timestamp column in my EvidenceTimeStamp table that matches the corresponding CaseID and EvidenceNum.
Note: the EvidenceTimeStamp table does not have a primary key or foreign key technically... the CaseID is primary key in the Case table and EvidenceNum is an attribute in the Evidence Table. This is a "trigger" table so it really doesn't have its own specific data, it's just filling data from other tables.
Any idea of how to do this?
Also, other quick question, how do I get bit values to show "True" or "False" instead of 1 or 0? Thank you!!
trigger timestamp
bumped to the homepage by Community♦ 12 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
|
show 1 more comment
I have an EvidenceTimeStamp table that is triggered with data when the case table has data inserted into it.
EvidenceTimeStamp:
CaseIDEvidenceNumEvidenceTypeEvidence_Initiated_TimestampEvidence_Returned_Timestamp
I created a trigger to fill the first four columns of data whenever a new case is inserted in the case table and it creates a timestamp on Evidence_Initiated_Timestamp when this happens.
However, I need to create a closing timestamp when the process is complete. I wanted to do this by my Outbound Ship Table- everytime the Attribute (bit value) Outbound_Item_Recevied is changed from false to true (or 0 to 1) I want it to initate a timestamp on the Evidence_Returned_Timestamp column in my EvidenceTimeStamp table that matches the corresponding CaseID and EvidenceNum.
Note: the EvidenceTimeStamp table does not have a primary key or foreign key technically... the CaseID is primary key in the Case table and EvidenceNum is an attribute in the Evidence Table. This is a "trigger" table so it really doesn't have its own specific data, it's just filling data from other tables.
Any idea of how to do this?
Also, other quick question, how do I get bit values to show "True" or "False" instead of 1 or 0? Thank you!!
trigger timestamp
bumped to the homepage by Community♦ 12 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Does yourOutbound Shiptable hasCaseIdandEvidenceNumcolumn?
– SqlWorldWide
Jun 8 '17 at 15:45
Yes,..... It does. But "Ship ID" is primary key, but that won't be used. Just had to throw a PK in there to keep it unique. CaseID is FK in that table
– Alee
Jun 8 '17 at 15:50
Can't you have another trigger based onCaseIDandEvidenceNum? If you add schema of all 3 tables might help folks answer your question. On your 2nd question you have to use a case statement to seeTrueandFalse.
– SqlWorldWide
Jun 8 '17 at 15:54
Yes, I don't know how to do that. I am very new with SQL
– Alee
Jun 8 '17 at 17:17
I suggest you post schema of all 3 tables with few rows of sample data. Somebody will be able to answer your question if it is doable.
– SqlWorldWide
Jun 8 '17 at 18:14
|
show 1 more comment
I have an EvidenceTimeStamp table that is triggered with data when the case table has data inserted into it.
EvidenceTimeStamp:
CaseIDEvidenceNumEvidenceTypeEvidence_Initiated_TimestampEvidence_Returned_Timestamp
I created a trigger to fill the first four columns of data whenever a new case is inserted in the case table and it creates a timestamp on Evidence_Initiated_Timestamp when this happens.
However, I need to create a closing timestamp when the process is complete. I wanted to do this by my Outbound Ship Table- everytime the Attribute (bit value) Outbound_Item_Recevied is changed from false to true (or 0 to 1) I want it to initate a timestamp on the Evidence_Returned_Timestamp column in my EvidenceTimeStamp table that matches the corresponding CaseID and EvidenceNum.
Note: the EvidenceTimeStamp table does not have a primary key or foreign key technically... the CaseID is primary key in the Case table and EvidenceNum is an attribute in the Evidence Table. This is a "trigger" table so it really doesn't have its own specific data, it's just filling data from other tables.
Any idea of how to do this?
Also, other quick question, how do I get bit values to show "True" or "False" instead of 1 or 0? Thank you!!
trigger timestamp
I have an EvidenceTimeStamp table that is triggered with data when the case table has data inserted into it.
EvidenceTimeStamp:
CaseIDEvidenceNumEvidenceTypeEvidence_Initiated_TimestampEvidence_Returned_Timestamp
I created a trigger to fill the first four columns of data whenever a new case is inserted in the case table and it creates a timestamp on Evidence_Initiated_Timestamp when this happens.
However, I need to create a closing timestamp when the process is complete. I wanted to do this by my Outbound Ship Table- everytime the Attribute (bit value) Outbound_Item_Recevied is changed from false to true (or 0 to 1) I want it to initate a timestamp on the Evidence_Returned_Timestamp column in my EvidenceTimeStamp table that matches the corresponding CaseID and EvidenceNum.
Note: the EvidenceTimeStamp table does not have a primary key or foreign key technically... the CaseID is primary key in the Case table and EvidenceNum is an attribute in the Evidence Table. This is a "trigger" table so it really doesn't have its own specific data, it's just filling data from other tables.
Any idea of how to do this?
Also, other quick question, how do I get bit values to show "True" or "False" instead of 1 or 0? Thank you!!
trigger timestamp
trigger timestamp
edited Jun 8 '17 at 20:00
RDFozz
9,90231531
9,90231531
asked Jun 8 '17 at 15:10
AleeAlee
272
272
bumped to the homepage by Community♦ 12 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 12 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Does yourOutbound Shiptable hasCaseIdandEvidenceNumcolumn?
– SqlWorldWide
Jun 8 '17 at 15:45
Yes,..... It does. But "Ship ID" is primary key, but that won't be used. Just had to throw a PK in there to keep it unique. CaseID is FK in that table
– Alee
Jun 8 '17 at 15:50
Can't you have another trigger based onCaseIDandEvidenceNum? If you add schema of all 3 tables might help folks answer your question. On your 2nd question you have to use a case statement to seeTrueandFalse.
– SqlWorldWide
Jun 8 '17 at 15:54
Yes, I don't know how to do that. I am very new with SQL
– Alee
Jun 8 '17 at 17:17
I suggest you post schema of all 3 tables with few rows of sample data. Somebody will be able to answer your question if it is doable.
– SqlWorldWide
Jun 8 '17 at 18:14
|
show 1 more comment
Does yourOutbound Shiptable hasCaseIdandEvidenceNumcolumn?
– SqlWorldWide
Jun 8 '17 at 15:45
Yes,..... It does. But "Ship ID" is primary key, but that won't be used. Just had to throw a PK in there to keep it unique. CaseID is FK in that table
– Alee
Jun 8 '17 at 15:50
Can't you have another trigger based onCaseIDandEvidenceNum? If you add schema of all 3 tables might help folks answer your question. On your 2nd question you have to use a case statement to seeTrueandFalse.
– SqlWorldWide
Jun 8 '17 at 15:54
Yes, I don't know how to do that. I am very new with SQL
– Alee
Jun 8 '17 at 17:17
I suggest you post schema of all 3 tables with few rows of sample data. Somebody will be able to answer your question if it is doable.
– SqlWorldWide
Jun 8 '17 at 18:14
Does your
Outbound Ship table has CaseId and EvidenceNum column?– SqlWorldWide
Jun 8 '17 at 15:45
Does your
Outbound Ship table has CaseId and EvidenceNum column?– SqlWorldWide
Jun 8 '17 at 15:45
Yes,..... It does. But "Ship ID" is primary key, but that won't be used. Just had to throw a PK in there to keep it unique. CaseID is FK in that table
– Alee
Jun 8 '17 at 15:50
Yes,..... It does. But "Ship ID" is primary key, but that won't be used. Just had to throw a PK in there to keep it unique. CaseID is FK in that table
– Alee
Jun 8 '17 at 15:50
Can't you have another trigger based on
CaseID and EvidenceNum? If you add schema of all 3 tables might help folks answer your question. On your 2nd question you have to use a case statement to see True and False.– SqlWorldWide
Jun 8 '17 at 15:54
Can't you have another trigger based on
CaseID and EvidenceNum? If you add schema of all 3 tables might help folks answer your question. On your 2nd question you have to use a case statement to see True and False.– SqlWorldWide
Jun 8 '17 at 15:54
Yes, I don't know how to do that. I am very new with SQL
– Alee
Jun 8 '17 at 17:17
Yes, I don't know how to do that. I am very new with SQL
– Alee
Jun 8 '17 at 17:17
I suggest you post schema of all 3 tables with few rows of sample data. Somebody will be able to answer your question if it is doable.
– SqlWorldWide
Jun 8 '17 at 18:14
I suggest you post schema of all 3 tables with few rows of sample data. Somebody will be able to answer your question if it is doable.
– SqlWorldWide
Jun 8 '17 at 18:14
|
show 1 more comment
1 Answer
1
active
oldest
votes
Logically (based on the names of your tables), a particular item of evidence cannot be checked out to more than one person at once. We'll assume, then, that no more than one EvidenceTimeStamp table record for a given CaseID and EvidenceNum should have a NULL Evidence_Returned_Timestamp.
In that case, you'd need a trigger on your Outbound Ship Table with something like the following in it:
UPDATE ets
SET ets.Evidence_Returned_Timestamp = GETDATE()
FROM EvidenceTimeStamp ets
INNER JOIN inserted I ON ( ets.CaseID = i.CaseID
AND ets.EvidenceNum = i.EvidenceNum
)
WHERE i.Outbound_Item_Recevied = 1
AND ets.Evidence_Returned_Timestamp IS NULL
;
(SQL Server syntax; modify as needed for other platforms)
This will work whether you're modifying/inserting one record or many in the Outbound Ship Table. However, this will update all rows for the given CaseID and EvidenceNum pair(s) that currently have NULL in Evidence_Returned_Timestamp, so if you can have more than one open row for a specific CaseID and EvidenceNum, you'll need some other way to find the right row(s) to update.
(To clarify: If you've got open entries for case 123, evidence numbers 2 and 7, and case 456, evidence numbers 3 and 7, and you updated an Outbound Ship item for case 123, evidence num 7, then the other three open entries are fine. However, if you had two EvidenceTimeStamp rows with case 123, evidence number 7, then both of them would have their Evidence_Returned_Timestamp set.)
NOTE: Every table should have a column (or group of columns) that is guaranteed to uniquely identify every row in the table and it (or they) really should be the primary key. You may not want to set that up as a clustered index, but you enforce uniqueness by making that the primary key.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdba.stackexchange.com%2fquestions%2f175783%2finsert-timestamp-on-value-change-from-other-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Logically (based on the names of your tables), a particular item of evidence cannot be checked out to more than one person at once. We'll assume, then, that no more than one EvidenceTimeStamp table record for a given CaseID and EvidenceNum should have a NULL Evidence_Returned_Timestamp.
In that case, you'd need a trigger on your Outbound Ship Table with something like the following in it:
UPDATE ets
SET ets.Evidence_Returned_Timestamp = GETDATE()
FROM EvidenceTimeStamp ets
INNER JOIN inserted I ON ( ets.CaseID = i.CaseID
AND ets.EvidenceNum = i.EvidenceNum
)
WHERE i.Outbound_Item_Recevied = 1
AND ets.Evidence_Returned_Timestamp IS NULL
;
(SQL Server syntax; modify as needed for other platforms)
This will work whether you're modifying/inserting one record or many in the Outbound Ship Table. However, this will update all rows for the given CaseID and EvidenceNum pair(s) that currently have NULL in Evidence_Returned_Timestamp, so if you can have more than one open row for a specific CaseID and EvidenceNum, you'll need some other way to find the right row(s) to update.
(To clarify: If you've got open entries for case 123, evidence numbers 2 and 7, and case 456, evidence numbers 3 and 7, and you updated an Outbound Ship item for case 123, evidence num 7, then the other three open entries are fine. However, if you had two EvidenceTimeStamp rows with case 123, evidence number 7, then both of them would have their Evidence_Returned_Timestamp set.)
NOTE: Every table should have a column (or group of columns) that is guaranteed to uniquely identify every row in the table and it (or they) really should be the primary key. You may not want to set that up as a clustered index, but you enforce uniqueness by making that the primary key.
add a comment |
Logically (based on the names of your tables), a particular item of evidence cannot be checked out to more than one person at once. We'll assume, then, that no more than one EvidenceTimeStamp table record for a given CaseID and EvidenceNum should have a NULL Evidence_Returned_Timestamp.
In that case, you'd need a trigger on your Outbound Ship Table with something like the following in it:
UPDATE ets
SET ets.Evidence_Returned_Timestamp = GETDATE()
FROM EvidenceTimeStamp ets
INNER JOIN inserted I ON ( ets.CaseID = i.CaseID
AND ets.EvidenceNum = i.EvidenceNum
)
WHERE i.Outbound_Item_Recevied = 1
AND ets.Evidence_Returned_Timestamp IS NULL
;
(SQL Server syntax; modify as needed for other platforms)
This will work whether you're modifying/inserting one record or many in the Outbound Ship Table. However, this will update all rows for the given CaseID and EvidenceNum pair(s) that currently have NULL in Evidence_Returned_Timestamp, so if you can have more than one open row for a specific CaseID and EvidenceNum, you'll need some other way to find the right row(s) to update.
(To clarify: If you've got open entries for case 123, evidence numbers 2 and 7, and case 456, evidence numbers 3 and 7, and you updated an Outbound Ship item for case 123, evidence num 7, then the other three open entries are fine. However, if you had two EvidenceTimeStamp rows with case 123, evidence number 7, then both of them would have their Evidence_Returned_Timestamp set.)
NOTE: Every table should have a column (or group of columns) that is guaranteed to uniquely identify every row in the table and it (or they) really should be the primary key. You may not want to set that up as a clustered index, but you enforce uniqueness by making that the primary key.
add a comment |
Logically (based on the names of your tables), a particular item of evidence cannot be checked out to more than one person at once. We'll assume, then, that no more than one EvidenceTimeStamp table record for a given CaseID and EvidenceNum should have a NULL Evidence_Returned_Timestamp.
In that case, you'd need a trigger on your Outbound Ship Table with something like the following in it:
UPDATE ets
SET ets.Evidence_Returned_Timestamp = GETDATE()
FROM EvidenceTimeStamp ets
INNER JOIN inserted I ON ( ets.CaseID = i.CaseID
AND ets.EvidenceNum = i.EvidenceNum
)
WHERE i.Outbound_Item_Recevied = 1
AND ets.Evidence_Returned_Timestamp IS NULL
;
(SQL Server syntax; modify as needed for other platforms)
This will work whether you're modifying/inserting one record or many in the Outbound Ship Table. However, this will update all rows for the given CaseID and EvidenceNum pair(s) that currently have NULL in Evidence_Returned_Timestamp, so if you can have more than one open row for a specific CaseID and EvidenceNum, you'll need some other way to find the right row(s) to update.
(To clarify: If you've got open entries for case 123, evidence numbers 2 and 7, and case 456, evidence numbers 3 and 7, and you updated an Outbound Ship item for case 123, evidence num 7, then the other three open entries are fine. However, if you had two EvidenceTimeStamp rows with case 123, evidence number 7, then both of them would have their Evidence_Returned_Timestamp set.)
NOTE: Every table should have a column (or group of columns) that is guaranteed to uniquely identify every row in the table and it (or they) really should be the primary key. You may not want to set that up as a clustered index, but you enforce uniqueness by making that the primary key.
Logically (based on the names of your tables), a particular item of evidence cannot be checked out to more than one person at once. We'll assume, then, that no more than one EvidenceTimeStamp table record for a given CaseID and EvidenceNum should have a NULL Evidence_Returned_Timestamp.
In that case, you'd need a trigger on your Outbound Ship Table with something like the following in it:
UPDATE ets
SET ets.Evidence_Returned_Timestamp = GETDATE()
FROM EvidenceTimeStamp ets
INNER JOIN inserted I ON ( ets.CaseID = i.CaseID
AND ets.EvidenceNum = i.EvidenceNum
)
WHERE i.Outbound_Item_Recevied = 1
AND ets.Evidence_Returned_Timestamp IS NULL
;
(SQL Server syntax; modify as needed for other platforms)
This will work whether you're modifying/inserting one record or many in the Outbound Ship Table. However, this will update all rows for the given CaseID and EvidenceNum pair(s) that currently have NULL in Evidence_Returned_Timestamp, so if you can have more than one open row for a specific CaseID and EvidenceNum, you'll need some other way to find the right row(s) to update.
(To clarify: If you've got open entries for case 123, evidence numbers 2 and 7, and case 456, evidence numbers 3 and 7, and you updated an Outbound Ship item for case 123, evidence num 7, then the other three open entries are fine. However, if you had two EvidenceTimeStamp rows with case 123, evidence number 7, then both of them would have their Evidence_Returned_Timestamp set.)
NOTE: Every table should have a column (or group of columns) that is guaranteed to uniquely identify every row in the table and it (or they) really should be the primary key. You may not want to set that up as a clustered index, but you enforce uniqueness by making that the primary key.
answered Jun 8 '17 at 20:19
RDFozzRDFozz
9,90231531
9,90231531
add a comment |
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- 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%2fdba.stackexchange.com%2fquestions%2f175783%2finsert-timestamp-on-value-change-from-other-table%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
Does your
Outbound Shiptable hasCaseIdandEvidenceNumcolumn?– SqlWorldWide
Jun 8 '17 at 15:45
Yes,..... It does. But "Ship ID" is primary key, but that won't be used. Just had to throw a PK in there to keep it unique. CaseID is FK in that table
– Alee
Jun 8 '17 at 15:50
Can't you have another trigger based on
CaseIDandEvidenceNum? If you add schema of all 3 tables might help folks answer your question. On your 2nd question you have to use a case statement to seeTrueandFalse.– SqlWorldWide
Jun 8 '17 at 15:54
Yes, I don't know how to do that. I am very new with SQL
– Alee
Jun 8 '17 at 17:17
I suggest you post schema of all 3 tables with few rows of sample data. Somebody will be able to answer your question if it is doable.
– SqlWorldWide
Jun 8 '17 at 18:14