Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

- - 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

If the person is a professor, what courses do they teach?

Answered: 1 week ago

Question

Explain the role of research design in HRD evaluation

Answered: 1 week ago