Sunteți pe pagina 1din 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

S-ar putea să vă placă și