0% found this document useful (0 votes)
78 views3 pages

Problem Solution Methods: 1. Using Cursor

This document describes three different methods for summarizing product sales data from a source table into grouped totals: 1) Using a cursor to iterate through the data row-by-row and insert summary rows, 2) Using table variables to store intermediate results before inserting summary data, and 3) Using temporary tables to store intermediate results before inserting final summary data. Each method performs similar logic of selecting distinct product IDs, calculating sales totals for each product, and inserting the results into a output table or variable.

Uploaded by

aksharma36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views3 pages

Problem Solution Methods: 1. Using Cursor

This document describes three different methods for summarizing product sales data from a source table into grouped totals: 1) Using a cursor to iterate through the data row-by-row and insert summary rows, 2) Using table variables to store intermediate results before inserting summary data, and 3) Using temporary tables to store intermediate results before inserting final summary data. Each method performs similar logic of selecting distinct product IDs, calculating sales totals for each product, and inserting the results into a output table or variable.

Uploaded by

aksharma36
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

CREATE TABLE ProductsSales


2.
(
3.
ID int IDENTITY(1,1) NOT NLL,
4.
ProductID int NOT NLL,
5.
ProductNa!e "arc#ar($%) NOT NLL,
6.
&t' int NOT NLL,
7.
A!ount deci!al(1%, () NOT NLL )
8.
)O
9.
SELECT * +RO, ProductsSales
10.
--We have the table with below data
Problem solution methods
1. Using Cursor
1.
SET NOCONT ON
2.
DECLARE -ProductID INT
3.
DECLARE -ProductNa!e .ARC/AR(1%%)
4.
DECLARE -Total&t' INT
5.
DECLARE -Total INT
6.
DECLARE -TProductSales TABLE
7.
(
8.
SNo INT IDENTITY(1,1),
9.
ProductID INT,
10.
ProductNa!e .ARC/AR(1%%),
11.
Total&t' INT,
12.
)randTotal INT
13.
)
14.
--Declare Cursor
15.
DECLARE Cur0Product CRSOR +OR SELECT DISTINCT ProductID +RO, ProductsSales
16.
--Open Cursor
17.
OPEN Cur0Product
18.
--Fetch Cursor
19.
+ETC/ NE1T +RO, Cur0Product INTO -ProductID
20.
2/ILE --+ETC/0STATS 3 %
21.
BE)IN
22.
SELECT -ProductNa!e 3 ProductNa!e +RO, ProductsSales 2/ERE ProductID 3 -ProductID
23.
SELECT -Total&t' 3 S,(&t'),-Total 3 S,(A!ount) +RO, ProductsSales 2/ERE ProductID 3
-ProductID
24.
INSERT INTO -TProductSales(ProductID,ProductNa!e,Total&t',)randTotal)
.ALES(-ProductID,-ProductNa!e,-Total&t',-Total)
25.
+ETC/ NE1T +RO, Cur0Product INTO -ProductID END
26.
--Close and Deallocate Cursor
27.
CLOSE Cur0Product
28.
DEALLOCATE Cur0Product
29.
--See Calculated data
30.
SELECT * +RO, -TProductSales
2. Using Table Variable
1.
SET NOCONT ON
2.
DECLARE -ProductID INT
3.
DECLARE -ProductNa!e .ARC/AR(1%%)
4.
DECLARE -Total&t' INT
5.
DECLARE -Total INT
6.
DECLARE -i INT 31
7.
DECLARE -count INT
8.
--Declare Table variables for storing data
9.
DECLARE -TProduct TABLE ( SNo INT IDENTITY(1,1),
10.
ProductID INT
11.
)
12.
DECLARE -TProductSales TABLE
13.
(
14.
SNo INT IDENTITY(1,1),
15.
ProductID INT,
16.
ProductNa!e .ARC/AR(1%%),
17.
Total&t' INT,
18.
)randTotal INT
19.
)
20.
--Insert data to Table variable @Product
21.
INSERT INTO -TProduct(ProductID)
22.
SELECT DISTINCT ProductID +RO, ProductsSales ORDER BY ProductID ASC
23.
-- Count nuber of rows
24.
SELECT -count 3 CONT(SNo) +RO, -TProduct 2/ILE (-i 43 -count)
25.
BE)IN
26.
SELECT -ProductID 3 ProductID +RO, -TProduct 2/ERE SNo 3 -i
27.
SELECT -ProductNa!e 3 ProductNa!e +RO, ProductsSales 2/ERE ProductID 3 -ProductID
28.
SELECT -Total&t' 3 S,(&t'),-Total 3 S,(A!ount) +RO, ProductsSales 2/ERE ProductID 3
-ProductID
29.
INSERT INTO -TProductSales(ProductID,ProductNa!e,Total&t',)randTotal)
.ALES(-ProductID,-ProductNa!e,-Total&t',-Total)
30.
SELECT -i 3 -i 5 1
31.
END
32.
--See Calculated data
33.
SELECT * +RO, -TProductSales
3. Using Temporary Table
1.
SET NOCONT ON
2.
DECLARE -ProductID INT
3.
DECLARE -ProductNa!e .ARC/AR(1%%)
4.
DECLARE -Total&t' INT
5.
DECLARE -Total INT
6.
DECLARE -i INT 31
7.
DECLARE -count INT
8.
--Create Teporar! Tables for storing data
9.
CREATE TABLE 6TProduct ( SNo INT IDENTITY(1,1),
10.
ProductID INT
11.
)
12.
CREATE TABLE 6TProductSales
13.
(
14.
SNo INT IDENTITY(1,1),
15.
ProductID INT, ProductNa!e .ARC/AR(1%%), Total&t' INT, )randTotal INT )
16.
--Insert data to teporar! table "Product
17.
INSERT INTO 6TProduct(ProductID) SELECT DISTINCT ProductID +RO, ProductsSales ORDER BY
ProductID ASC
18.
SELECT -count 3 CONT(SNo) +RO, 6TProduct
19.
2/ILE (-i 43 -count)
20.
BE)IN
21.
SELECT -ProductID 3 ProductID +RO, 6TProduct 2/ERE SNo 3 -i
22.
SELECT -ProductNa!e 3 ProductNa!e +RO, ProductsSales 2/ERE ProductID 3 -ProductID
23.
SELECT -Total&t' 3 S,(&t'),-Total 3 S,(A!ount) +RO, ProductsSales 2/ERE ProductID 3
-ProductID
24.
INSERT INTO 6TProductSales(ProductID,ProductNa!e,Total&t',)randTotal)
.ALES(-ProductID,-ProductNa!e,-Total&t',-Total)
25.
SELECT -i 3 -i 5 1
26.
END
27.
--See Calculated data
28.
SELECT * +RO, 6TProductSales
29.
--#ow Drop Teporar! Tables
30.
DROP TABLE 6TProduct
31.
DROP TABLE 6TProductSales

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy