How do I bulk create rows with a foreign key dependency?
I wrote some SQL statements that work for updating a single customer. I have to update all the customers when this code gets pushed out.
Right now the customer ID is hardcoded and the SQL statements insert one record based on that ID. Prototype works, now I want to do like 10,000 inserts for all of the customers using the same algorithm.
DECLARE @customerID BIGINT = 47636;
DECLARE @limitFourAdjustment MONEY;
DECLARE @appliesToDateTime DATETIME2(7) = SYSUTCDATETIME();
DECLARE @dp_y INT = DATEPART(YEAR, @appliesToDateTime);
DECLARE @dp_m INT = DATEPART(MONTH, @appliesToDateTime);
DECLARE @dp_w INT = DATEPART(WEEK, @appliesToDateTime);
DECLARE @dp_d INT = DATEPART(DAY, @appliesToDateTime);
DECLARE @dp_h INT = DATEPART(HOUR, @appliesToDateTime);
DECLARE @d_h DATETIME2(7) = DATEADD(HOUR, DATEDIFF(HOUR, 0, @appliesToDateTime), 0);
SELECT
@limitFourAdjustment = -COALESCE(SUM(COALESCE(Amount, 0)), 0)
FROM
[dbo].Transactions
WHERE
CustomerID = @customerID AND
IsSystemVoid = 0 AND
TransactionTypeID IN (SELECT ID FROM TransactionTypes WHERE TransactionTypeGroupID = 3)
INSERT INTO dbo.CustomerAccounts_TransactionSummation (CustomerID, LimitTypeID, Y, M, W, D, H, YMDH, Amount)
VALUES (@customerID, 4, @dp_y, @dp_m, @dp_w, @dp_d, @dp_h, @d_h, @limitFourAdjustment);
I tried adding a while loop, seems like not the fastest solution. Maybe collect the ID's first and then feed it to through the loop? My first attempt below doesn't work because I just get the last customer ID, not a unique one every time.
SELECT @numberOfCustomers = COUNT(*)
FROM dbo.Customers
WHILE(@numberOfCustomers > 0)
BEGIN
SELECT @customerID = ID FROM dbo.Customers
OTHER LOGIC FROM ABOVE
SET @numberOfCustomers = @numberOfCustomers - 1;
END
So the question is, how to run these SQL statements (first code block) on every customer's ID?
sql-server tsql
add a comment |
I wrote some SQL statements that work for updating a single customer. I have to update all the customers when this code gets pushed out.
Right now the customer ID is hardcoded and the SQL statements insert one record based on that ID. Prototype works, now I want to do like 10,000 inserts for all of the customers using the same algorithm.
DECLARE @customerID BIGINT = 47636;
DECLARE @limitFourAdjustment MONEY;
DECLARE @appliesToDateTime DATETIME2(7) = SYSUTCDATETIME();
DECLARE @dp_y INT = DATEPART(YEAR, @appliesToDateTime);
DECLARE @dp_m INT = DATEPART(MONTH, @appliesToDateTime);
DECLARE @dp_w INT = DATEPART(WEEK, @appliesToDateTime);
DECLARE @dp_d INT = DATEPART(DAY, @appliesToDateTime);
DECLARE @dp_h INT = DATEPART(HOUR, @appliesToDateTime);
DECLARE @d_h DATETIME2(7) = DATEADD(HOUR, DATEDIFF(HOUR, 0, @appliesToDateTime), 0);
SELECT
@limitFourAdjustment = -COALESCE(SUM(COALESCE(Amount, 0)), 0)
FROM
[dbo].Transactions
WHERE
CustomerID = @customerID AND
IsSystemVoid = 0 AND
TransactionTypeID IN (SELECT ID FROM TransactionTypes WHERE TransactionTypeGroupID = 3)
INSERT INTO dbo.CustomerAccounts_TransactionSummation (CustomerID, LimitTypeID, Y, M, W, D, H, YMDH, Amount)
VALUES (@customerID, 4, @dp_y, @dp_m, @dp_w, @dp_d, @dp_h, @d_h, @limitFourAdjustment);
I tried adding a while loop, seems like not the fastest solution. Maybe collect the ID's first and then feed it to through the loop? My first attempt below doesn't work because I just get the last customer ID, not a unique one every time.
SELECT @numberOfCustomers = COUNT(*)
FROM dbo.Customers
WHILE(@numberOfCustomers > 0)
BEGIN
SELECT @customerID = ID FROM dbo.Customers
OTHER LOGIC FROM ABOVE
SET @numberOfCustomers = @numberOfCustomers - 1;
END
So the question is, how to run these SQL statements (first code block) on every customer's ID?
sql-server tsql
add a comment |
I wrote some SQL statements that work for updating a single customer. I have to update all the customers when this code gets pushed out.
Right now the customer ID is hardcoded and the SQL statements insert one record based on that ID. Prototype works, now I want to do like 10,000 inserts for all of the customers using the same algorithm.
DECLARE @customerID BIGINT = 47636;
DECLARE @limitFourAdjustment MONEY;
DECLARE @appliesToDateTime DATETIME2(7) = SYSUTCDATETIME();
DECLARE @dp_y INT = DATEPART(YEAR, @appliesToDateTime);
DECLARE @dp_m INT = DATEPART(MONTH, @appliesToDateTime);
DECLARE @dp_w INT = DATEPART(WEEK, @appliesToDateTime);
DECLARE @dp_d INT = DATEPART(DAY, @appliesToDateTime);
DECLARE @dp_h INT = DATEPART(HOUR, @appliesToDateTime);
DECLARE @d_h DATETIME2(7) = DATEADD(HOUR, DATEDIFF(HOUR, 0, @appliesToDateTime), 0);
SELECT
@limitFourAdjustment = -COALESCE(SUM(COALESCE(Amount, 0)), 0)
FROM
[dbo].Transactions
WHERE
CustomerID = @customerID AND
IsSystemVoid = 0 AND
TransactionTypeID IN (SELECT ID FROM TransactionTypes WHERE TransactionTypeGroupID = 3)
INSERT INTO dbo.CustomerAccounts_TransactionSummation (CustomerID, LimitTypeID, Y, M, W, D, H, YMDH, Amount)
VALUES (@customerID, 4, @dp_y, @dp_m, @dp_w, @dp_d, @dp_h, @d_h, @limitFourAdjustment);
I tried adding a while loop, seems like not the fastest solution. Maybe collect the ID's first and then feed it to through the loop? My first attempt below doesn't work because I just get the last customer ID, not a unique one every time.
SELECT @numberOfCustomers = COUNT(*)
FROM dbo.Customers
WHILE(@numberOfCustomers > 0)
BEGIN
SELECT @customerID = ID FROM dbo.Customers
OTHER LOGIC FROM ABOVE
SET @numberOfCustomers = @numberOfCustomers - 1;
END
So the question is, how to run these SQL statements (first code block) on every customer's ID?
sql-server tsql
I wrote some SQL statements that work for updating a single customer. I have to update all the customers when this code gets pushed out.
Right now the customer ID is hardcoded and the SQL statements insert one record based on that ID. Prototype works, now I want to do like 10,000 inserts for all of the customers using the same algorithm.
DECLARE @customerID BIGINT = 47636;
DECLARE @limitFourAdjustment MONEY;
DECLARE @appliesToDateTime DATETIME2(7) = SYSUTCDATETIME();
DECLARE @dp_y INT = DATEPART(YEAR, @appliesToDateTime);
DECLARE @dp_m INT = DATEPART(MONTH, @appliesToDateTime);
DECLARE @dp_w INT = DATEPART(WEEK, @appliesToDateTime);
DECLARE @dp_d INT = DATEPART(DAY, @appliesToDateTime);
DECLARE @dp_h INT = DATEPART(HOUR, @appliesToDateTime);
DECLARE @d_h DATETIME2(7) = DATEADD(HOUR, DATEDIFF(HOUR, 0, @appliesToDateTime), 0);
SELECT
@limitFourAdjustment = -COALESCE(SUM(COALESCE(Amount, 0)), 0)
FROM
[dbo].Transactions
WHERE
CustomerID = @customerID AND
IsSystemVoid = 0 AND
TransactionTypeID IN (SELECT ID FROM TransactionTypes WHERE TransactionTypeGroupID = 3)
INSERT INTO dbo.CustomerAccounts_TransactionSummation (CustomerID, LimitTypeID, Y, M, W, D, H, YMDH, Amount)
VALUES (@customerID, 4, @dp_y, @dp_m, @dp_w, @dp_d, @dp_h, @d_h, @limitFourAdjustment);
I tried adding a while loop, seems like not the fastest solution. Maybe collect the ID's first and then feed it to through the loop? My first attempt below doesn't work because I just get the last customer ID, not a unique one every time.
SELECT @numberOfCustomers = COUNT(*)
FROM dbo.Customers
WHILE(@numberOfCustomers > 0)
BEGIN
SELECT @customerID = ID FROM dbo.Customers
OTHER LOGIC FROM ABOVE
SET @numberOfCustomers = @numberOfCustomers - 1;
END
So the question is, how to run these SQL statements (first code block) on every customer's ID?
sql-server tsql
sql-server tsql
edited Jan 1 at 21:41
Dale Burrell
3,25932553
3,25932553
asked Jan 1 at 21:33
Ryan DinesRyan Dines
1
1
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The key to working with databases is getting your mind around set based operations as opposed to procedural operations. Databases are designed to operate naturally on sets of data at a time, but you have to change how you think about the problem to one where you are manipulating the entire set of data as opposed to one record at a time.
So here is the SQL which I think carry out your complete update in one hit:
INSERT INTO dbo.CustomerAccounts_TransactionSummation (CustomerID, LimitTypeID, Y, M, W, D, H, YMDH, Amount)
SELECT
id
, 4
, @dp_y
, @dp_m
, @dp_w
, @dp_d
, @dp_h
, @d_h
, -COALESCE(SUM(COALESCE(Amount, 0)), 0) limitFourAdjustment
FROM [dbo].Transactions
WHERE IsSystemVoid = 0
and TransactionTypeID IN (SELECT ID FROM TransactionTypes WHERE TransactionTypeGroupID = 3)
--and CustomerID = @customerID
Note that the insert
can be combined directly with a select
as opposed to using values
.
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%2f53999110%2fhow-do-i-bulk-create-rows-with-a-foreign-key-dependency%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
The key to working with databases is getting your mind around set based operations as opposed to procedural operations. Databases are designed to operate naturally on sets of data at a time, but you have to change how you think about the problem to one where you are manipulating the entire set of data as opposed to one record at a time.
So here is the SQL which I think carry out your complete update in one hit:
INSERT INTO dbo.CustomerAccounts_TransactionSummation (CustomerID, LimitTypeID, Y, M, W, D, H, YMDH, Amount)
SELECT
id
, 4
, @dp_y
, @dp_m
, @dp_w
, @dp_d
, @dp_h
, @d_h
, -COALESCE(SUM(COALESCE(Amount, 0)), 0) limitFourAdjustment
FROM [dbo].Transactions
WHERE IsSystemVoid = 0
and TransactionTypeID IN (SELECT ID FROM TransactionTypes WHERE TransactionTypeGroupID = 3)
--and CustomerID = @customerID
Note that the insert
can be combined directly with a select
as opposed to using values
.
add a comment |
The key to working with databases is getting your mind around set based operations as opposed to procedural operations. Databases are designed to operate naturally on sets of data at a time, but you have to change how you think about the problem to one where you are manipulating the entire set of data as opposed to one record at a time.
So here is the SQL which I think carry out your complete update in one hit:
INSERT INTO dbo.CustomerAccounts_TransactionSummation (CustomerID, LimitTypeID, Y, M, W, D, H, YMDH, Amount)
SELECT
id
, 4
, @dp_y
, @dp_m
, @dp_w
, @dp_d
, @dp_h
, @d_h
, -COALESCE(SUM(COALESCE(Amount, 0)), 0) limitFourAdjustment
FROM [dbo].Transactions
WHERE IsSystemVoid = 0
and TransactionTypeID IN (SELECT ID FROM TransactionTypes WHERE TransactionTypeGroupID = 3)
--and CustomerID = @customerID
Note that the insert
can be combined directly with a select
as opposed to using values
.
add a comment |
The key to working with databases is getting your mind around set based operations as opposed to procedural operations. Databases are designed to operate naturally on sets of data at a time, but you have to change how you think about the problem to one where you are manipulating the entire set of data as opposed to one record at a time.
So here is the SQL which I think carry out your complete update in one hit:
INSERT INTO dbo.CustomerAccounts_TransactionSummation (CustomerID, LimitTypeID, Y, M, W, D, H, YMDH, Amount)
SELECT
id
, 4
, @dp_y
, @dp_m
, @dp_w
, @dp_d
, @dp_h
, @d_h
, -COALESCE(SUM(COALESCE(Amount, 0)), 0) limitFourAdjustment
FROM [dbo].Transactions
WHERE IsSystemVoid = 0
and TransactionTypeID IN (SELECT ID FROM TransactionTypes WHERE TransactionTypeGroupID = 3)
--and CustomerID = @customerID
Note that the insert
can be combined directly with a select
as opposed to using values
.
The key to working with databases is getting your mind around set based operations as opposed to procedural operations. Databases are designed to operate naturally on sets of data at a time, but you have to change how you think about the problem to one where you are manipulating the entire set of data as opposed to one record at a time.
So here is the SQL which I think carry out your complete update in one hit:
INSERT INTO dbo.CustomerAccounts_TransactionSummation (CustomerID, LimitTypeID, Y, M, W, D, H, YMDH, Amount)
SELECT
id
, 4
, @dp_y
, @dp_m
, @dp_w
, @dp_d
, @dp_h
, @d_h
, -COALESCE(SUM(COALESCE(Amount, 0)), 0) limitFourAdjustment
FROM [dbo].Transactions
WHERE IsSystemVoid = 0
and TransactionTypeID IN (SELECT ID FROM TransactionTypes WHERE TransactionTypeGroupID = 3)
--and CustomerID = @customerID
Note that the insert
can be combined directly with a select
as opposed to using values
.
answered Jan 1 at 21:39
Dale BurrellDale Burrell
3,25932553
3,25932553
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%2f53999110%2fhow-do-i-bulk-create-rows-with-a-foreign-key-dependency%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