DECLARE @Rowcount INT
SELECT @Rowcount = COUNT(*) FROM [dbo].[AddressTable];
WHILE( @Rowcount > 0)
BEGIN
SELECT @Rowcount = @Rowcount - 1;
SELECT [AddressId]
,[Name]
,[Address]
FROM [dbo].[AddressTable]
ORDER BY [AddressId] DESC
OFFSET @Rowcount ROWS
FETCH NEXT 1 ROWS ONLY;
END
----------------------------------------------------
SET NOCOUNT ON
SET XACT_ABORT ON
DECLARE @ProductID INT
DECLARE @ProductName VARCHAR(100)
DECLARE @TotalQty INT
DECLARE @Total MONEY
DECLARE @Index INT
DECLARE @RecordCnt INT
DECLARE @ProdID TABLE(
iSNo INT IDENTITY(1,1)
,iProductID INT
)
DECLARE @ProductSales TABLE
(
iSNo INT IDENTITY(1,1)
,iProductID INT
,vProductName VARCHAR(100)
,iTotalQty INT
,iGrandTotal MONEY
)
SELECT @Index = 1
INSERT INTO @ProdID(iProductID)
SELECT ProductID FROM Products ORDER BY ProductID ASC
SELECT @RecordCnt = COUNT(iSNo) FROM @ProdID
WHILE (@Index <= @RecordCnt)
BEGIN
SELECT @ProductID = iProductID FROM @ProdID WHERE iSNo = @Index
SELECT @ProductName = ProductName FROM Products
WHERE ProductID = @ProductID
SELECT @TotalQty = SUM(Quantity),@Total = SUM(UnitPrice*Quantity) FROM "Order Details" WHERE ProductID = @ProductID
INSERT INTO @ProductSales(iProductID,vProductName,iTotalQty,iGrandTotal)
VALUES(@ProductID,@ProductName,@TotalQty,@Total)
SELECT @Index = @Index + 1
END
SELECT * FROM @ProductSales
END
GO
----------------------------------------------------
--Audit Entry - Starts
DECLARE @AuditRowcount INT
DECLARE @UserName VARCHAR(100)
DECLARE @TableName VARCHAR(100)
DECLARE @OldData NVARCHAR(MAX)
DECLARE @NewData NVARCHAR(MAX)
SELECT @AuditRowcount = COUNT(*) FROM @TblAuditHistory;
WHILE( @AuditRowcount > 0)
BEGIN
SELECT @AuditRowcount = @AuditRowcount - 1;
SELECT @UserName = [UserName]
,@TableName = [TableName]
,@OldData = [OldData]
,@NewData = [NewData]
FROM @TblAuditHistory
ORDER BY [Date] DESC
OFFSET @AuditRowcount ROWS
FETCH NEXT 1 ROWS ONLY;
EXEC [Audit].[usp_InitiateAuditService] @UserName,@TableName,@OldData,@NewData;
END
--Audit Entry - Ends
----------------------------------------------------------
SELECT @Rowcount = COUNT(*) FROM [dbo].[AddressTable];
WHILE( @Rowcount > 0)
BEGIN
SELECT @Rowcount = @Rowcount - 1;
SELECT [AddressId]
,[Name]
,[Address]
FROM [dbo].[AddressTable]
ORDER BY [AddressId] DESC
OFFSET @Rowcount ROWS
FETCH NEXT 1 ROWS ONLY;
END
----------------------------------------------------
SET NOCOUNT ON
SET XACT_ABORT ON
DECLARE @ProductID INT
DECLARE @ProductName VARCHAR(100)
DECLARE @TotalQty INT
DECLARE @Total MONEY
DECLARE @Index INT
DECLARE @RecordCnt INT
DECLARE @ProdID TABLE(
iSNo INT IDENTITY(1,1)
,iProductID INT
)
DECLARE @ProductSales TABLE
(
iSNo INT IDENTITY(1,1)
,iProductID INT
,vProductName VARCHAR(100)
,iTotalQty INT
,iGrandTotal MONEY
)
SELECT @Index = 1
INSERT INTO @ProdID(iProductID)
SELECT ProductID FROM Products ORDER BY ProductID ASC
SELECT @RecordCnt = COUNT(iSNo) FROM @ProdID
WHILE (@Index <= @RecordCnt)
BEGIN
SELECT @ProductID = iProductID FROM @ProdID WHERE iSNo = @Index
SELECT @ProductName = ProductName FROM Products
WHERE ProductID = @ProductID
SELECT @TotalQty = SUM(Quantity),@Total = SUM(UnitPrice*Quantity) FROM "Order Details" WHERE ProductID = @ProductID
INSERT INTO @ProductSales(iProductID,vProductName,iTotalQty,iGrandTotal)
VALUES(@ProductID,@ProductName,@TotalQty,@Total)
SELECT @Index = @Index + 1
END
SELECT * FROM @ProductSales
END
GO
----------------------------------------------------
--Audit Entry - Starts
DECLARE @AuditRowcount INT
DECLARE @UserName VARCHAR(100)
DECLARE @TableName VARCHAR(100)
DECLARE @OldData NVARCHAR(MAX)
DECLARE @NewData NVARCHAR(MAX)
SELECT @AuditRowcount = COUNT(*) FROM @TblAuditHistory;
WHILE( @AuditRowcount > 0)
BEGIN
SELECT @AuditRowcount = @AuditRowcount - 1;
SELECT @UserName = [UserName]
,@TableName = [TableName]
,@OldData = [OldData]
,@NewData = [NewData]
FROM @TblAuditHistory
ORDER BY [Date] DESC
OFFSET @AuditRowcount ROWS
FETCH NEXT 1 ROWS ONLY;
EXEC [Audit].[usp_InitiateAuditService] @UserName,@TableName,@OldData,@NewData;
END
--Audit Entry - Ends
----------------------------------------------------------
--Create another temp table with identity column CREATE TABLE #EmployeeCopy (LoopId INT IDENTITY(1,1), Id INT , Name NVARCHAR(100), Status TINYINT) --Copy data to the table with identity column INSERT INTO #EmployeeCopy(Id, Name , Status) SELECT Id, Name , Status FROM #Employee DECLARE @LoopCounter INT , @MaxEmployeeId INT , @EmployeeName NVARCHAR(100) SELECT @LoopCounter = min (LoopId),@MaxEmployeeId = max (LoopId) FROM #EmployeeCopy WHILE ( @LoopCounter IS NOT NULL AND @LoopCounter <= @MaxEmployeeId) BEGIN SELECT @EmployeeName = Name FROM #EmployeeCopy WHERE LoopId = @LoopCounter PRINT @EmployeeName SELECT @LoopCounter = min (LoopId) FROM #EmployeeCopy WHERE LoopId > @LoopCounter END ------------------------------------------------------- |