Let’s create the table with sample data:
CREATE TABLE [dbo].[Product] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (256) NULL,
[CategoryId] INT NULL,
[Description] NVARCHAR (512) NULL,
[IsActive] BIT NULL,
[Price] DECIMAL (18) NOT NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_Product_Category] FOREIGN KEY ([CategoryId]) REFERENCES [dbo].[Category]([Id]) ON DELETE CASCADE ON UPDATE CASCADE
);
//Insert Data
SET IDENTITY_INSERT [dbo].[Product] ON
INSERT INTO [dbo].[Product] ([Id], [Name], [CategoryId], [Description], [IsActive], [Price])VALUES (1, N'Htc on', 1, NULL, 1, CAST(34 AS Decimal(18, 0)))
INSERT INTO [dbo].[Product] ([Id], [Name], [CategoryId], [Description], [IsActive], [Price])VALUES (2, N'Samsung galaxy', 1, N' Thids jsdfhns kndfksd', 1, CAST(45 AS Decimal(18, 0)))
INSERT INTO [dbo].[Product] ([Id], [Name], [CategoryId], [Description], [IsActive], [Price])VALUES (3, N'Lumia 1320', 1, N'test test', 1, CAST(45 AS Decimal(18, 0)))
INSERT INTO [dbo].[Product] ([Id], [Name], [CategoryId], [Description], [IsActive], [Price])VALUES (4, N'Ramond', 2, N' theisbdf ', 1, CAST(45 AS Decimal(18, 0)))
INSERT INTO [dbo].[Product] ([Id], [Name], [CategoryId], [Description], [IsActive], [Price])VALUES (5, N'Peter England trouser', 2, N'sdfj', 1, CAST(4 AS Decimal(18, 0)))
INSERT INTO [dbo].[Product] ([Id], [Name], [CategoryId], [Description], [IsActive], [Price])VALUES (6, N'Niva', 3, N'dhfdh', 1, CAST(34 AS Decimal(18, 0)))
INSERT INTO [dbo].[Product] ([Id], [Name], [CategoryId], [Description], [IsActive], [Price])VALUES (7, N'Ponds', 3, NULL, 1, CAST(45 AS Decimal(18, 0)))
SET IDENTITY_INSERT [dbo].[Product] OFF
//Store Procedure
create PROCEDURE [dbo].[Sorting]
@SortByCloumnName varchar(128),
@SortByDirection varchar(4)
AS
select * from Product order by
case when @SortByCloumnName='Name' and LOWER( @SortByDirection)= 'asc' thenName end asc,
case when @SortByCloumnName='Name' and LOWER(@SortByDirection) = 'desc' thenName end Desc
RETURN 0
No comments:
Post a Comment