Question

In: Computer Science

Increase all of the listing prices by 5% for all listings under $500,000 and 10% for...

Increase all of the listing prices by 5% for all listings under $500,000 and 10% for all listings $500,000 and higher. Update the listings table with the new prices.

update LISTING
set LISTING_PRICE = LISTING_PRICE +
case
  when LISTING_PRICE < 500000 then (LISTING_PRICE*5)/100
  when LISTING_PRICE >= 500000 then (LISTING_PRICE*10)/100
  else 0
end

-- Add 30 days to the date expires for all listings.

update LISTING set DATE_EXPIRES = DATEADD(dd, 30, DATE_EXPIRES)

-- Add "Listing updated on [current date]." to the remarks. Replace [current date] with the current systems date. Do not replace the remarks currently in the table. Add these remarks to the remarks already in the table.

update LISTING set REMARKS=CONCAT(REMARKS ," ","Listing updated on ",GETDATE())

-- Return the following information from the listings table from the stored procedure to display the information in the new real estate app: address, city, state, zip, updated listing price, updated date expires, and updated remarks.

CREATE PROCEDURE DISPLAYLISTING
AS
BEGIN
select ADDRESS,CITY,STATE,ZIP,LISTING_PRICE,DATE_EXPIRES,REMARKS from LISTING;
END;

-- Call and run the stored procedure to make the appropriate updates and return the proper results.

In order to execute the stored procedures we use the following SQL script :

EXEC DISPLAYLISTING;

We can update and display the information from the listings table using following stored procedure as :

CREATE PROCEDURE DISPLAYLISTING
AS
BEGIN

update LISTING
set LISTING_PRICE = LISTING_PRICE +
case
  when LISTING_PRICE < 500000 then (LISTING_PRICE*5)/100
  when LISTING_PRICE >= 500000 then (LISTING_PRICE*10)/100
  else 0
end;

update LISTING set DATE_EXPIRES = DATEADD(dd, 30, DATE_EXPIRES);
  
update LISTING set REMARKS=CONCAT(REMARKS ," ","Listing updated on ",GETDATE());

select ADDRESS,CITY,STATE,ZIP,LISTING_PRICE,DATE_EXPIRES,REMARKS from LISTING;

END;

For this lab exercise you will be working with and modifying the stored procedure you created in the last module. In your stored procedure, the first three requirements performed updates to your database and the last two requirements returned the data that was updated. Perform the following:

1. Enclose the code for the first requirement in its own single transaction with the proper commit and rollback functions.

2. Enclose the code for both the second and third requirements in one single transaction with the proper commit and rollback functions.

3. Call and run the stored procedure to make the appropriate updates and return the proper results. Take the appropriate screenshots to show this working and insert into your screenshot document.

4. In the second transaction you created that included the code for the second and third requirements - create an error in the middle of the transaction between the code for the two requirements. For example, you can add an insert or an update statement that uses a field that does not exist. This would cause an error. When you run it, the error should cause the entire transaction to rollback. Because of this error, no changes should be made by this transaction. This is one way of testing your rollback code.

5. Call and run the stored procedure a second time to make the appropriate updates and return the proper results. The first transaction should run without any issues but the second transaction should rollback any changes it made.

You are going to take the stored procedure you created in the lab exercise from the last module and create two transactions within it. The first transaction will contain the first requirement from that module's lab exercise. The second transaction will contain the second and third requirements from that module's lab exercise. You will run it and take screenshots to show it working properly. Then you will purposely insert code to create an error in the middle of the second transaction, in between the two items to cause the transaction to fail and rollback any changes. You will run it again and take screenshots to show your rollback working properly.

Solutions

Expert Solution

Initial entry in database:

STORED PROCEDURE:

CREATE PROCEDURE DISPLAYLISTING
AS
BEGIN
begin try
        begin transaction;
                update LISTING
                set LISTING_PRICE = LISTING_PRICE +
                case
                  when LISTING_PRICE < 500000 then (LISTING_PRICE*5)/100
                  when LISTING_PRICE >= 500000 then (LISTING_PRICE*10)/100
                  else 0
                end;
        commit transaction;
end try 
begin catch  
        if @@trancount > 0
        begin
                rollback transaction;
        end
end catch 

begin try
        begin transaction;
                update LISTING set DATE_EXPIRES = DATEADD(dd, 30, DATE_EXPIRES);  
                update LISTING set REMARKS=CONCAT(REMARKS ,' ','Listing updated on ',GETDATE());
        commit transaction;
end try 
begin catch  
        if @@trancount > 0
        begin
                rollback transaction;
        end
end catch 
select ADDRESS,CITY,STATE,ZIP,LISTING_PRICE,DATE_EXPIRES,REMARKS from LISTING;
END;

Snippet of SP:

Execution Result/Output:

Stored Procedure with error:

ALTER PROCEDURE DISPLAYLISTING
AS
BEGIN
begin try
        begin transaction;
                update LISTING
                set LISTING_PRICE = LISTING_PRICE +
                case
                  when LISTING_PRICE < 500000 then (LISTING_PRICE*5)/100
                  when LISTING_PRICE >= 500000 then (LISTING_PRICE*10)/100
                  else 0
                end;
        commit transaction;
end try 
begin catch  
        if @@trancount > 0
        begin
                rollback transaction;
        end
end catch 

begin try
        begin transaction;
                update LISTING set DATE_EXPIRES = DATEADD(dd, 30, DATE_EXPIRES);  
                declare @SqlQuery nvarchar(max);
                set @SqlQuery = N'update LISTING set TIME = GETDATE()';
                EXEC @SqlQuery;
                update LISTING set REMARKS=CONCAT(REMARKS ,' ','Listing updated on ',GETDATE());
        commit transaction;
end try 
begin catch  
        if @@trancount > 0
        begin
                rollback transaction;
        end
end catch 
select ADDRESS,CITY,STATE,ZIP,LISTING_PRICE,DATE_EXPIRES,REMARKS from LISTING;
END;

Snippet of code:

Execution Result/Output:


Related Solutions

Create a Post listing five (5) ways to increase productivity on the job.
Create a Post listing five (5) ways to increase productivity on the job.
Assembly Language Programming Exercise 5. Listing File for AddTwoSum ( 5 pts ) Generate a listing...
Assembly Language Programming Exercise 5. Listing File for AddTwoSum ( 5 pts ) Generate a listing file for the AddTwoSum program and write a description of the machine code bytes generated for each instruction. You might have to guess at some of the meanings of the byte values. Hint: Watch my tutorial and read a little bit of Ch 4.
Verizon offers to sell cellular phones listing for $95.24 with a chain discount of 15/10/5. Cellular...
Verizon offers to sell cellular phones listing for $95.24 with a chain discount of 15/10/5. Cellular Company offers to sell its cellular phones that list at $98.49 with a chain discount of 25/5. If Irene is to buy ten phones, how much could she save if she buys from the lower-priced company? (Do not round intermediate calculations. Round all dollar amounts to the nearest hundredth. Round your final answer to the nearest cent.
A 5-year, 10%, $500,000 loan requires equal principal payments in years 4 and 5 ($250,000 each...
A 5-year, 10%, $500,000 loan requires equal principal payments in years 4 and 5 ($250,000 each year). Show the cash flows (interest and principal payments) for years 1-5.
On July 1, 2020, a company borrows $500,000 under a long-term loan. The $500,000 will be...
On July 1, 2020, a company borrows $500,000 under a long-term loan. The $500,000 will be paid back in five annual instalments of $100,000 each, starting on June 30, 2021. What will be the company's long-term loan balance, shown in the long-term liability section of its statement of financial position, at December 31, 2021? a)$400,000. b)$350,000. c)$300,000. d)Cannot tell, as the interest rate on the loan has not been provided
Is it possible for OPEC to lower prices to $10 a barrel and drive all non-OPEC...
Is it possible for OPEC to lower prices to $10 a barrel and drive all non-OPEC competitors, including the fracking industry out of business? Why or Why not! Use Economic Theory to answer this question
Suppose a firm is expected to increase dividends by 5% in one year and by 10%...
Suppose a firm is expected to increase dividends by 5% in one year and by 10% in year two. After that, dividends will increase at a rate of 4% per year indefinitely. If the last dividend was $4 and the required return is 10%, what is the price of the stock?
A study reported that, all else the same, a 10% increase in the hourly minimum wage reduced annual hours of employment of young workers by 5%.
Agree or disagree and explain.A study reported that, all else the same, a 10% increase in the hourly minimum wage reduced annual hours of employment of young workers by 5%.a. Assuming this information is correct: is the wage-elasticity of demand for the labor for young workers about -2 or about -0.5 (that is, 2 or 0.5 in absolute value)? Briefly explain.b.  Assuming the elasticity estimate from part “a” is correct, would increasing the minimum wage increase the annual earnings of youth?...
Draw the binomial tree listing only the option prices at each node. Assume the following data...
Draw the binomial tree listing only the option prices at each node. Assume the following data on a 6-month call option, using 3-month intervals as the time period. K = $40, S = $37.90, r = 5.0%, σ = 0.35
When prices increase the interest rate effect A. will increase and the aggregate demand for goods...
When prices increase the interest rate effect A. will increase and the aggregate demand for goods and services will decrease. B. will decrease and the aggregate demand for goods and services will decrease. C. will not be affected. D. will increase and the aggregate demand for goods and services will increase.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT