Question: - - Create the stored procedure CREATE PROCEDURE cs 4 3 1 _ question 4 AS BEGIN - - Declare variables DECLARE @outputString NVARCHAR (

-- Create the stored procedure
CREATE PROCEDURE cs431_question4
AS
BEGIN
-- Declare variables
DECLARE @outputString NVARCHAR(MAX)
DECLARE @vendor_id INT
DECLARE @vendor_name NVARCHAR(100)
DECLARE @invoice_number NVARCHAR(50)
DECLARE @balance_due DECIMAL(18,2)
-- Initialize the output string
SET @outputString =''
-- Declare and set up the cursor
DECLARE invoice_cursor CURSOR FOR
SELECT v.vendor_id, v.vendor_name, i.invoice_number, (i.invoice_total - i.payment_total) AS balance_due
FROM invoices i
JOIN vendors v ON i.vendor_id = v.vendor_id
WHERE (i.invoice_total - i.payment_total)>=0-- Filter invoices with a balance due >= $0
-- Open the cursor
OPEN invoice_cursor
-- Fetch data into variables and build the output string
FETCH NEXT FROM invoice_cursor INTO @vendor_id, @vendor_name, @invoice_number, @balance_due
WHILE @@FETCH_STATUS =0
BEGIN
SET @outputString = @outputString + CAST(@vendor_id AS NVARCHAR(10))+'|'+
@vendor_name +'|'+
@invoice_number +'|'+
CONVERT(NVARCHAR(20), @balance_due)+'//'
FETCH NEXT FROM invoice_cursor INTO @vendor_id, @vendor_name, @invoice_number, @balance_due
END
-- Close and deallocate the cursor
CLOSE invoice_cursor
DEALLOCATE invoice_cursor
-- Display the output string
SELECT @outputString AS Result
END

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!