Modify the solution to exercise 1 to fetch each row into a set of local variables. Use
Question:
Modify the solution to exercise 1 to fetch each row into a set of local variables. Use the PRINT statement to return each row in the format “Name, $0.00” to the Messages tab.
Here is question 1 that it is referring to:1. Write a script that creates a cursor for a result set that consists of the ProductName and ListPrice columns for each product with a list price that’s greater than $700. The rows in this result set should be sorted in descending sequence by list price. Then, the script should print the product name and list price for each product so it looks something like this: Gibson SG, $2517.00 Gibson Les Paul, $1199.00
Here is my code for question 1: SET NOCOUNT ON DECLARE @ProductName VARCHAR(50) DECLARE @ListPrice NUMERIC(10,2) DECLARE PRODUCTS_CUR CURSOR STATIC FOR SELECT ProductName, ListPrice FROM PRODUCTS WHERE ListPrice>700 ORDER BY ListPrice DESC OPEN PRODUCTS_CUR IF @@CURSOR_ROWS > 0 BEGIN FETCH NEXT FROM PRODUCTS_CUR INTO @ProductName,@ListPrice WHILE @@Fetch_status = 0 BEGIN PRINT @ProductName +', $'+CONVERT(VARCHAR,@ListPrice) FETCH NEXT FROM PRODUCTS_CUR INTO @ProductName,@ListPrice END END CLOSE PRODUCTS_CUR DEALLOCATE PRODUCTS_CUR SET NOCOUNT OFF
Probability and Statistics
ISBN: 978-0321500465
4th edition
Authors: Morris H. DeGroot, Mark J. Schervish