MICROSOFT SQL Server how do i update a column after a transfer is complete (homework)












0















The homework question:



Write a stored procedure called TransferRegion that accepts a RegionID and a DistributorID. The procedure will transfer a particular region from one distributor to another. When the region is transferred the old distributor will have $1.00 subtracted from their wage and the new distributor will have $1.00 added their wage. Ensure all necessary tables are updated as required.



My question: how do I update the old distributor wage after its been transferred out?



CREATE PROCEDURE TransferRegion 
(@RegionID INT = NULL, @DistributorID INT = NULL)
AS
IF @RegionID IS NULL OR @DistributorID IS NULL
BEGIN
raiserror('Must provide a region id and distributor id',16,1)
END
else
begin
begin transaction

update Region
set DistributorID = @DistributorID
where RegionID = @RegionID

if @@ERROR<> 0
begin
raiserror('transfering distributor failed',16,1)
rollback transaction
end
else
begin
update Distributor
set wage = wage + 1
where DistributorID = @DistributorID

if @@ERROR<> 0
begin
raiserror('transfering distributor failed',16,1)
rollback transaction
end
else
begin
commit transaction
end
end
end
return









share|improve this question

























  • which database are you using?

    – codeLover
    Nov 23 '18 at 4:55











  • Looks like sqlserver from the raiserror call?

    – Caius Jard
    Nov 23 '18 at 5:38
















0















The homework question:



Write a stored procedure called TransferRegion that accepts a RegionID and a DistributorID. The procedure will transfer a particular region from one distributor to another. When the region is transferred the old distributor will have $1.00 subtracted from their wage and the new distributor will have $1.00 added their wage. Ensure all necessary tables are updated as required.



My question: how do I update the old distributor wage after its been transferred out?



CREATE PROCEDURE TransferRegion 
(@RegionID INT = NULL, @DistributorID INT = NULL)
AS
IF @RegionID IS NULL OR @DistributorID IS NULL
BEGIN
raiserror('Must provide a region id and distributor id',16,1)
END
else
begin
begin transaction

update Region
set DistributorID = @DistributorID
where RegionID = @RegionID

if @@ERROR<> 0
begin
raiserror('transfering distributor failed',16,1)
rollback transaction
end
else
begin
update Distributor
set wage = wage + 1
where DistributorID = @DistributorID

if @@ERROR<> 0
begin
raiserror('transfering distributor failed',16,1)
rollback transaction
end
else
begin
commit transaction
end
end
end
return









share|improve this question

























  • which database are you using?

    – codeLover
    Nov 23 '18 at 4:55











  • Looks like sqlserver from the raiserror call?

    – Caius Jard
    Nov 23 '18 at 5:38














0












0








0








The homework question:



Write a stored procedure called TransferRegion that accepts a RegionID and a DistributorID. The procedure will transfer a particular region from one distributor to another. When the region is transferred the old distributor will have $1.00 subtracted from their wage and the new distributor will have $1.00 added their wage. Ensure all necessary tables are updated as required.



My question: how do I update the old distributor wage after its been transferred out?



CREATE PROCEDURE TransferRegion 
(@RegionID INT = NULL, @DistributorID INT = NULL)
AS
IF @RegionID IS NULL OR @DistributorID IS NULL
BEGIN
raiserror('Must provide a region id and distributor id',16,1)
END
else
begin
begin transaction

update Region
set DistributorID = @DistributorID
where RegionID = @RegionID

if @@ERROR<> 0
begin
raiserror('transfering distributor failed',16,1)
rollback transaction
end
else
begin
update Distributor
set wage = wage + 1
where DistributorID = @DistributorID

if @@ERROR<> 0
begin
raiserror('transfering distributor failed',16,1)
rollback transaction
end
else
begin
commit transaction
end
end
end
return









share|improve this question
















The homework question:



Write a stored procedure called TransferRegion that accepts a RegionID and a DistributorID. The procedure will transfer a particular region from one distributor to another. When the region is transferred the old distributor will have $1.00 subtracted from their wage and the new distributor will have $1.00 added their wage. Ensure all necessary tables are updated as required.



My question: how do I update the old distributor wage after its been transferred out?



CREATE PROCEDURE TransferRegion 
(@RegionID INT = NULL, @DistributorID INT = NULL)
AS
IF @RegionID IS NULL OR @DistributorID IS NULL
BEGIN
raiserror('Must provide a region id and distributor id',16,1)
END
else
begin
begin transaction

update Region
set DistributorID = @DistributorID
where RegionID = @RegionID

if @@ERROR<> 0
begin
raiserror('transfering distributor failed',16,1)
rollback transaction
end
else
begin
update Distributor
set wage = wage + 1
where DistributorID = @DistributorID

if @@ERROR<> 0
begin
raiserror('transfering distributor failed',16,1)
rollback transaction
end
else
begin
commit transaction
end
end
end
return






sql sql-server stored-procedures transactions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 7:21









jarlh

29.1k52138




29.1k52138










asked Nov 23 '18 at 4:39









jonathanjonathan

112




112













  • which database are you using?

    – codeLover
    Nov 23 '18 at 4:55











  • Looks like sqlserver from the raiserror call?

    – Caius Jard
    Nov 23 '18 at 5:38



















  • which database are you using?

    – codeLover
    Nov 23 '18 at 4:55











  • Looks like sqlserver from the raiserror call?

    – Caius Jard
    Nov 23 '18 at 5:38

















which database are you using?

– codeLover
Nov 23 '18 at 4:55





which database are you using?

– codeLover
Nov 23 '18 at 4:55













Looks like sqlserver from the raiserror call?

– Caius Jard
Nov 23 '18 at 5:38





Looks like sqlserver from the raiserror call?

– Caius Jard
Nov 23 '18 at 5:38












1 Answer
1






active

oldest

votes


















0














You need to find the old distributor currently holding the region before you begin:



DECLARE @oldDist INT = (SELECT distributorid FROM region WHERE regionid = @regionid);


I'm not going to do all the work for you cause this is an academic exercise for your learning benefit, but that above, in the right place coupled with a slight copy paste change to a query you've already written will get you to where you need to be



Always always comment your work when learning



Use comments to form the high level algorithm - very few people think in code, they think in native language (eg English) and translate to code



Write the algorithm in English as comments then leave them in



--check values passed are valid and refer to existing region /distrib; throw a not found error if not

--get the old distributor id we are transferring from-will need this later

--update to transfer to the new distrib

--update to increment new distrib wage

--update to decrement old distrib wage


That's what you should write first, then put code in below each comment to implement the comment



Other tips:



Use semicolons- omitting them is now deprecated



Don't get into a multiple nested if else if else he'll, its messy and unnecessary; you can do all this work at the base nest level because if you detect an error and raise it the procedure bombs out at that point (if your in a try block the severity needs to be greater than 10 to jump to the catch block)



Do not use RAISERROR - the docs state "new applications should use throw instead". Embarking on a path of using deprecated syntax is not good, discuss with your supervisor if this code skeleton came from them



Consider starting your transaction in a TRY, execute multiple statements and have a CATCH block that checks the @@TRANCOUNT and rolls the transaction back if it's greater than 0






share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440729%2fmicrosoft-sql-server-how-do-i-update-a-column-after-a-transfer-is-complete-home%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









    0














    You need to find the old distributor currently holding the region before you begin:



    DECLARE @oldDist INT = (SELECT distributorid FROM region WHERE regionid = @regionid);


    I'm not going to do all the work for you cause this is an academic exercise for your learning benefit, but that above, in the right place coupled with a slight copy paste change to a query you've already written will get you to where you need to be



    Always always comment your work when learning



    Use comments to form the high level algorithm - very few people think in code, they think in native language (eg English) and translate to code



    Write the algorithm in English as comments then leave them in



    --check values passed are valid and refer to existing region /distrib; throw a not found error if not

    --get the old distributor id we are transferring from-will need this later

    --update to transfer to the new distrib

    --update to increment new distrib wage

    --update to decrement old distrib wage


    That's what you should write first, then put code in below each comment to implement the comment



    Other tips:



    Use semicolons- omitting them is now deprecated



    Don't get into a multiple nested if else if else he'll, its messy and unnecessary; you can do all this work at the base nest level because if you detect an error and raise it the procedure bombs out at that point (if your in a try block the severity needs to be greater than 10 to jump to the catch block)



    Do not use RAISERROR - the docs state "new applications should use throw instead". Embarking on a path of using deprecated syntax is not good, discuss with your supervisor if this code skeleton came from them



    Consider starting your transaction in a TRY, execute multiple statements and have a CATCH block that checks the @@TRANCOUNT and rolls the transaction back if it's greater than 0






    share|improve this answer






























      0














      You need to find the old distributor currently holding the region before you begin:



      DECLARE @oldDist INT = (SELECT distributorid FROM region WHERE regionid = @regionid);


      I'm not going to do all the work for you cause this is an academic exercise for your learning benefit, but that above, in the right place coupled with a slight copy paste change to a query you've already written will get you to where you need to be



      Always always comment your work when learning



      Use comments to form the high level algorithm - very few people think in code, they think in native language (eg English) and translate to code



      Write the algorithm in English as comments then leave them in



      --check values passed are valid and refer to existing region /distrib; throw a not found error if not

      --get the old distributor id we are transferring from-will need this later

      --update to transfer to the new distrib

      --update to increment new distrib wage

      --update to decrement old distrib wage


      That's what you should write first, then put code in below each comment to implement the comment



      Other tips:



      Use semicolons- omitting them is now deprecated



      Don't get into a multiple nested if else if else he'll, its messy and unnecessary; you can do all this work at the base nest level because if you detect an error and raise it the procedure bombs out at that point (if your in a try block the severity needs to be greater than 10 to jump to the catch block)



      Do not use RAISERROR - the docs state "new applications should use throw instead". Embarking on a path of using deprecated syntax is not good, discuss with your supervisor if this code skeleton came from them



      Consider starting your transaction in a TRY, execute multiple statements and have a CATCH block that checks the @@TRANCOUNT and rolls the transaction back if it's greater than 0






      share|improve this answer




























        0












        0








        0







        You need to find the old distributor currently holding the region before you begin:



        DECLARE @oldDist INT = (SELECT distributorid FROM region WHERE regionid = @regionid);


        I'm not going to do all the work for you cause this is an academic exercise for your learning benefit, but that above, in the right place coupled with a slight copy paste change to a query you've already written will get you to where you need to be



        Always always comment your work when learning



        Use comments to form the high level algorithm - very few people think in code, they think in native language (eg English) and translate to code



        Write the algorithm in English as comments then leave them in



        --check values passed are valid and refer to existing region /distrib; throw a not found error if not

        --get the old distributor id we are transferring from-will need this later

        --update to transfer to the new distrib

        --update to increment new distrib wage

        --update to decrement old distrib wage


        That's what you should write first, then put code in below each comment to implement the comment



        Other tips:



        Use semicolons- omitting them is now deprecated



        Don't get into a multiple nested if else if else he'll, its messy and unnecessary; you can do all this work at the base nest level because if you detect an error and raise it the procedure bombs out at that point (if your in a try block the severity needs to be greater than 10 to jump to the catch block)



        Do not use RAISERROR - the docs state "new applications should use throw instead". Embarking on a path of using deprecated syntax is not good, discuss with your supervisor if this code skeleton came from them



        Consider starting your transaction in a TRY, execute multiple statements and have a CATCH block that checks the @@TRANCOUNT and rolls the transaction back if it's greater than 0






        share|improve this answer















        You need to find the old distributor currently holding the region before you begin:



        DECLARE @oldDist INT = (SELECT distributorid FROM region WHERE regionid = @regionid);


        I'm not going to do all the work for you cause this is an academic exercise for your learning benefit, but that above, in the right place coupled with a slight copy paste change to a query you've already written will get you to where you need to be



        Always always comment your work when learning



        Use comments to form the high level algorithm - very few people think in code, they think in native language (eg English) and translate to code



        Write the algorithm in English as comments then leave them in



        --check values passed are valid and refer to existing region /distrib; throw a not found error if not

        --get the old distributor id we are transferring from-will need this later

        --update to transfer to the new distrib

        --update to increment new distrib wage

        --update to decrement old distrib wage


        That's what you should write first, then put code in below each comment to implement the comment



        Other tips:



        Use semicolons- omitting them is now deprecated



        Don't get into a multiple nested if else if else he'll, its messy and unnecessary; you can do all this work at the base nest level because if you detect an error and raise it the procedure bombs out at that point (if your in a try block the severity needs to be greater than 10 to jump to the catch block)



        Do not use RAISERROR - the docs state "new applications should use throw instead". Embarking on a path of using deprecated syntax is not good, discuss with your supervisor if this code skeleton came from them



        Consider starting your transaction in a TRY, execute multiple statements and have a CATCH block that checks the @@TRANCOUNT and rolls the transaction back if it's greater than 0







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 23 '18 at 5:59

























        answered Nov 23 '18 at 5:45









        Caius JardCaius Jard

        11.7k21239




        11.7k21239
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53440729%2fmicrosoft-sql-server-how-do-i-update-a-column-after-a-transfer-is-complete-home%23new-answer', 'question_page');
            }
            );

            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







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin