| Author |
Topic |
|
sivrin
Starting Member
4 Posts |
Posted - 2007-12-19 : 21:55:11
|
| Hi,Can some one help me to complete this query.My requirement is to convert the existing stored proc(given below) to one that allows dynamic paging as well as sorting(sql server 2005). I gave a try but invein. I am running out of time.Existing procedure:ALTER PROCEDURE [dbo].[spGetAllItems]@condition varchar(7500),@ModelId int =0,@CategoryId int=0,@SourcingType int=0,@StartRow as int = 1,@StopRow as int = 100000ASdeclare @erow intdeclare @Cond varchar(255)select @Cond=''if @CategoryId<>0select @Cond= ' and tbItemMaster.Category=' + cast(@CategoryId as varchar)if @SourcingType<>0select @Cond= ' and tbItemMaster.SourcingType=' + cast(@SourcingType as varchar)if @StopRow=-1select @StopRow=60set @erow=@StartRow + @StopRowexec('Declare @t_table table([SpecialId] [int] IDENTITY (1, 1) Primary key NOT NULL,[id] [int],[category] [int] NULL ,[model] [int] NULL ,[mtype] [varchar] (50) NULL,[itemclass] [varchar] (50) NULL,[itemcode] [nvarchar] (25) NULL ,[ItemCondition] [tinyint] NULL ,[SourcingType] int,[Title] [nvarchar] (50) NULL ,[Description] [nvarchar] (4000) NULL ,[InternalUse] [nvarchar] (4000) NULL,[CompatibleModels] [varchar] (8000) NULL ,[Quantity] [int] NULL ,[MinOrder] [int] NULL ,[Warranty] [tinyint] NULL ,[Price1] [decimal](6, 2) NULL ,[Price2] [decimal](6, 2) NULL ,[Price3] [decimal](6, 2) NULL ,[Price4] [decimal](6, 2) NULL ,[LotSale] [bit],[PricingRule] int ,[Weight] [decimal](7, 2) NULL ,[PackageSize] [int] NULL ,[promo] [int] NULL ,[SmallImage] [nvarchar] (255) NULL ,[BigImage] [nvarchar] (255) NULL ,[StartDate] [datetime] NULL ,[EndDate] [datetime] NULL ,[Info1] [nvarchar] (50) NULL ,[Info2] [nvarchar] (50) NULL ,[Info3] [nvarchar] (50) NULL ,[Info4] [nvarchar] (50) NULL ,[Info5] [nvarchar] (50) NULL ,[Info6] [nvarchar] (50) NULL ,[Info7] [nvarchar] (50) NULL ,[Info8] [nvarchar] (50) NULL ,[Info9] [nvarchar] (50) NULL ,[Info10] [nvarchar] (50) NULL ,[Info11] [nvarchar] (50) NULL ,[Info12] [nvarchar] (50) NULL ,[Info13] [nvarchar] (50) NULL ,[Info14] [nvarchar] (50) NULL ,[Info15] [nvarchar] (50) NULL ,[Info16] [nvarchar] (50) NULL ,[Info17] [nvarchar] (50) NULL ,[Info18] [nvarchar] (50) NULL ,[Info19] [nvarchar] (50) NULL ,[Info20] [nvarchar] (50) NULL ,[Info21] [nvarchar] (50) NULL ,[Info22] [nvarchar] (50) NULL ,[Info23] [nvarchar] (50) NULL ,[Info24] [nvarchar] (50) NULL ,[Info25] [nvarchar] (50) NULL ,[Status] [tinyint] NULL ,[AllowBuy] [char] (1) NULL ,[PageName] [varchar] (200) NULL ,[Locality] [int] NULL ,[Location] [int] NULL ,[CreatedBy] [int] NULL ,[CreatedOn] [datetime] NULL ,[UpdatedBy] [int] NULL ,[UpdatedOn] [datetime] NULL,[BrandId] int,[CategoryId] int,[ModelId] int,[ModelName] varchar(255),[BrandName] varchar(255),[CategoryName] varchar(255));DECLARE @EndRow intDECLARE @reccount intselect @reccount=count(*) from (SELECT tbItemMaster.idFROM tbItemMaster INNER JOINtbModel ON tbItemMaster.model = tbModel.id INNER JOINtbCategory ON tbItemMaster.category = tbCategory.Id INNER JOINtbBrand ON tbModel.brand = tbBrand.id' + @Condition + ' Union SELECT tbItemMaster.idFROM tbItemMaster INNER JOINtbModel ON tbItemMaster.model = tbModel.id INNER JOINtbCategory ON tbItemMaster.category = tbCategory.Id INNER JOINtbBrand ON tbModel.brand = tbBrand.id inner join tbCompatibleModels on tbItemMaster.Id=tbCompatibleModels.ItemId where tbCompatibleModels.ModelId=' + @ModelId + ') tblset @EndRow= + ' + @StartRow + '+' + @StopRow + '--Set RowCount @EndRow;insert @t_table([id] ,[category] ,[model] ,[mtype],[itemclass],[itemcode] ,[ItemCondition] ,[SourcingType],[Title] ,[Description] ,[InternalUse],[CompatibleModels],[Quantity] ,[MinOrder] ,[Warranty] ,[Price1] ,[Price2] ,[Price3] ,[Price4] ,[LotSale] ,[PricingRule],[Weight] ,[PackageSize] ,[promo] ,[SmallImage] ,[BigImage] ,[StartDate] ,[EndDate] ,[Info1] ,[Info2] ,[Info3] ,[Info4] ,[Info5] ,[Info6] ,[Info7] ,[Info8] ,[Info9] ,[Info10] ,[Info11] ,[Info12] ,[Info13] ,[Info14] ,[Info15] ,[Info16] ,[Info17] ,[Info18] ,[Info19] ,[Info20] ,[Info21] ,[Info22] ,[Info23] ,[Info24] ,[Info25] ,[Status] ,[AllowBuy] ,[PageName] ,[Locality] ,[Location] ,[CreatedBy] ,[CreatedOn] ,[UpdatedBy] ,[UpdatedOn] ,[BrandId] ,[CategoryId] ,[ModelId],[ModelName],[BrandName],[CategoryName])select * from (SELECT top 100 percent tbItemMaster.id,tbItemMaster.category,tbItemMaster.model,tbItemMaster.mtype,tbItemMaster.ItemClass, tbItemMaster.itemcode, tbItemMaster.ItemCondition,tbItemMaster.SourcingType, tbItemMaster.Title, tbItemMaster.Description,tbItemMaster.InternalUse, tbItemMaster.CompatibleModels,tbItemMaster.Quantity,tbItemMaster.MinOrder, tbItemMaster.Warranty, tbItemMaster.Price1, tbItemMaster.Price2, tbItemMaster.Price3,tbItemMaster.Price4,tbItemMaster.LotSale,tbItemMaster.PricingRule, tbItemMaster.Weight, tbItemMaster.PackageSize, tbItemMaster.promo,tbItemMaster.SmallImage, tbItemMaster.BigImage, tbItemMaster.StartDate, tbItemMaster.EndDate, tbItemMaster.Info1, tbItemMaster.Info2, tbItemMaster.Info3, tbItemMaster.Info4, tbItemMaster.Info5, tbItemMaster.Info6, tbItemMaster.Info7, tbItemMaster.Info8,tbItemMaster.Info9, tbItemMaster.Info10, tbItemMaster.Info11, tbItemMaster.Info12, tbItemMaster.Info13, tbItemMaster.Info14, tbItemMaster.Info15, tbItemMaster.Info16, tbItemMaster.Info17, tbItemMaster.Info18, tbItemMaster.Info19, tbItemMaster.Info20, tbItemMaster.Info21, tbItemMaster.Info22, tbItemMaster.Info23, tbItemMaster.Info24, tbItemMaster.Info25,tbItemMaster.Status, tbItemMaster.AllowBuy, tbItemMaster.PageName, tbItemMaster.Locality, tbItemMaster.Location,tbItemMaster.CreatedBy, tbItemMaster.CreatedOn, tbItemMaster.UpdatedBy,tbItemMaster.UpdatedOn, tbBrand.id AS BrandId, tbCategory.Id AS CategoryId, tbModel.id AS ModelId,tbModel.Model as ModelName,tbBrand.Brand as BrandName,tbCategory.Category as CategoryNameFROM tbItemMaster INNER JOINtbModel ON tbItemMaster.model = tbModel.id INNER JOINtbCategory ON tbItemMaster.category = tbCategory.Id INNER JOINtbBrand ON tbModel.brand = tbBrand.id' + @Condition + ' Order By tbItemMaster.UpdatedOn desc) tbl Union SELECT top 100 percent tbItemMaster.id,tbItemMaster.category,tbItemMaster.model,tbItemMaster.mtype,tbItemMaster.ItemClass, tbItemMaster.itemcode, tbItemMaster.ItemCondition,tbItemMaster.SourcingType, tbItemMaster.Title, tbItemMaster.Description,tbItemMaster.InternalUse, tbItemMaster.CompatibleModels,tbItemMaster.Quantity,tbItemMaster.MinOrder, tbItemMaster.Warranty, tbItemMaster.Price1, tbItemMaster.Price2, tbItemMaster.Price3,tbItemMaster.Price4,tbItemMaster.LotSale,tbItemMaster.PricingRule, tbItemMaster.Weight, tbItemMaster.PackageSize, tbItemMaster.promo,tbItemMaster.SmallImage, tbItemMaster.BigImage, tbItemMaster.StartDate, tbItemMaster.EndDate, tbItemMaster.Info1, tbItemMaster.Info2, tbItemMaster.Info3, tbItemMaster.Info4, tbItemMaster.Info5, tbItemMaster.Info6, tbItemMaster.Info7, tbItemMaster.Info8,tbItemMaster.Info9, tbItemMaster.Info10, tbItemMaster.Info11, tbItemMaster.Info12, tbItemMaster.Info13, tbItemMaster.Info14, tbItemMaster.Info15, tbItemMaster.Info16, tbItemMaster.Info17, tbItemMaster.Info18, tbItemMaster.Info19, tbItemMaster.Info20, tbItemMaster.Info21, tbItemMaster.Info22, tbItemMaster.Info23, tbItemMaster.Info24, tbItemMaster.Info25,tbItemMaster.Status, tbItemMaster.AllowBuy, tbItemMaster.PageName, tbItemMaster.Locality, tbItemMaster.Location,tbItemMaster.CreatedBy, tbItemMaster.CreatedOn, tbItemMaster.UpdatedBy,tbItemMaster.UpdatedOn, tbBrand.id AS BrandId, tbCategory.Id AS CategoryId, tbModel.id AS ModelId,tbModel.Model as ModelName,tbBrand.Brand as BrandName,tbCategory.Category as CategoryNameFROM tbItemMaster INNER JOINtbModel ON tbItemMaster.model = tbModel.id INNER JOINtbCategory ON tbItemMaster.category = tbCategory.Id INNER JOINtbBrand ON tbModel.brand = tbBrand.id inner join tbCompatibleModels on tbItemMaster.Id=tbCompatibleModels.ItemId where tbCompatibleModels.ModelId=' + @ModelId + @Cond + ' Order By UpdatedOn desc;SELECT *,@reccount as ItemsCount FROM @t_table WHERE SpecialId >=' + @StartRow + ' and SpecialId<@EndRow ORDER BY UpdatedOn DESC')RETURNcan anybody help is greatly appreciated. ITs very urgent and running out of time.Thanks in advance. |
|
|
sivrin
Starting Member
4 Posts |
Posted - 2007-12-19 : 23:17:23
|
| Hi,I tried this but gives problem when the size of variable that exceeds the maximum limit. can any give me some idea since i am new this./****** Object: StoredProcedure [dbo].[spGetAllItemsTestingVer4] Script Date: 12/20/2007 10:42:20 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[spGetAllItemsTestingVer4] @condition varchar(7500), @ModelId int =0, @CategoryId int=0, @SourcingType int=0, @StartRow as int = 1, @StopRow as int = 100000, @sort as varchar(500)ASdeclare @erow intdeclare @Cond varchar(255)select @Cond=''if @CategoryId<>0 select @Cond= ' and tbItemMaster.Category=' + cast(@CategoryId as varchar)if @SourcingType<>0 select @Cond= ' and tbItemMaster.SourcingType=' + cast(@SourcingType as varchar)declare @sql nvarchar(max)set @sql='with tempresult as (SELECT top 100 tbItemMaster.id,Row_Number() over (order by tbItemMaster.UpdatedOn)as Row,tbItemMaster.category,tbItemMaster.model,tbItemMaster.mtype,tbItemMaster.ItemClass, tbItemMaster.itemcode, tbItemMaster.ItemCondition,tbItemMaster.SourcingType, tbItemMaster.Title, tbItemMaster.Description,tbItemMaster.InternalUse, tbItemMaster.CompatibleModels,tbItemMaster.Quantity,tbItemMaster.MinOrder, tbItemMaster.Warranty, tbItemMaster.Price1, tbItemMaster.Price2, tbItemMaster.Price3,tbItemMaster.Price4,tbItemMaster.LotSale,tbItemMaster.PricingRule, tbItemMaster.Weight, tbItemMaster.PackageSize,tbItemMaster.promo,tbItemMaster.SmallImage, tbItemMaster.BigImage, tbItemMaster.StartDate, tbItemMaster.EndDate, tbItemMaster.Info1, tbItemMaster.Info2, tbItemMaster.Info3, tbItemMaster.Info4, tbItemMaster.Info5, tbItemMaster.Info6, tbItemMaster.Info7, tbItemMaster.Info8,tbItemMaster.Info9, tbItemMaster.Info10, tbItemMaster.Info11, tbItemMaster.Info12, tbItemMaster.Info13, tbItemMaster.Info14, tbItemMaster.Info15, tbItemMaster.Info16, tbItemMaster.Info17, tbItemMaster.Info18,tbItemMaster.Info19, tbItemMaster.Info20, tbItemMaster.Info21, tbItemMaster.Info22, tbItemMaster.Info23, tbItemMaster.Info24, tbItemMaster.Info25,tbItemMaster.Status, tbItemMaster.AllowBuy, tbItemMaster.PageName, tbItemMaster.Locality, tbItemMaster.Location,tbItemMaster.CreatedBy, tbItemMaster.CreatedOn, tbItemMaster.UpdatedBy,tbItemMaster.UpdatedOn,tbBrand.id AS BrandId, tbCategory.Id AS CategoryId, tbModel.id AS ModelId,tbModel.Model as ModelName,tbBrand.Brand as BrandName,tbCategory.Category as CategoryName FROM tbItemMaster INNER JOIN tbModel ON tbItemMaster.model = tbModel.id INNER JOIN tbCategory ON tbItemMaster.category = tbCategory.Id INNER JOIN tbBrand ON tbModel.brand = tbBrand.id ' + @Condition + 'unionSELECT top 100 tbItemMaster.id,Row_Number() over (order by tbItemMaster.UpdatedOn)as Row,tbItemMaster.category,tbItemMaster.model,tbItemMaster.mtype,tbItemMaster.ItemClass, tbItemMaster.itemcode, tbItemMaster.ItemCondition,tbItemMaster.SourcingType, tbItemMaster.Title, tbItemMaster.Description,tbItemMaster.InternalUse, tbItemMaster.CompatibleModels,tbItemMaster.Quantity,tbItemMaster.MinOrder, tbItemMaster.Warranty, tbItemMaster.Price1, tbItemMaster.Price2, tbItemMaster.Price3,tbItemMaster.Price4,tbItemMaster.LotSale,tbItemMaster.PricingRule, tbItemMaster.Weight, tbItemMaster.PackageSize,tbItemMaster.promo,tbItemMaster.SmallImage, tbItemMaster.BigImage, tbItemMaster.StartDate, tbItemMaster.EndDate, tbItemMaster.Info1, tbItemMaster.Info2, tbItemMaster.Info3, tbItemMaster.Info4, tbItemMaster.Info5, tbItemMaster.Info6, tbItemMaster.Info7, tbItemMaster.Info8,tbItemMaster.Info9, tbItemMaster.Info10, tbItemMaster.Info11, tbItemMaster.Info12,tbItemMaster.Info13, tbItemMaster.Info14, tbItemMaster.Info15, tbItemMaster.Info16, tbItemMaster.Info17, tbItemMaster.Info18,tbItemMaster.Info19, tbItemMaster.Info20, tbItemMaster.Info21, tbItemMaster.Info22, tbItemMaster.Info23, tbItemMaster.Info24, tbItemMaster.Info25,tbItemMaster.Status, tbItemMaster.AllowBuy, tbItemMaster.PageName,tbItemMaster.Locality, tbItemMaster.Location,tbItemMaster.CreatedBy, tbItemMaster.CreatedOn, tbItemMaster.UpdatedBy,tbItemMaster.UpdatedOn,tbBrand.id AS BrandId, tbCategory.Id AS CategoryId, tbModel.id AS ModelId,tbModel.Model as ModelName,tbBrand.Brand as BrandName,tbCategory.Category as CategoryName FROM tbItemMaster INNER JOIN tbModel ON tbItemMaster.model = tbModel.id INNER JOIN tbCategory ON tbItemMaster.category = tbCategory.Id INNER JOIN tbBrand ON tbModel.brand = tbBrand.id inner join tbCompatibleModels on tbItemMaster.Id=tbCompatibleModels.ItemId where tbCompatibleModels.ModelId= ' + cast(@ModelId as nvarchar(10)) + @Cond + ') select *from tempresult WHERE Row >= ' + cast(@StartRow as nvarchar(10)) + ' and Row <= ' + (cast(@StartRow as nvarchar(10))+ '+' + cast(@StopRow as nvarchar(10))) + '-' + cast(1 as nvarchar(10)) + ' Order by case ' + @sort +' when CategoryName then CategoryName when ModelName then ModelName when BrandName then BrandName when quantity then Quantity end' print @sql --exec(@sql)Thanks in advance. |
 |
|
|
matty
Posting Yak Master
161 Posts |
Posted - 2007-12-19 : 23:27:22
|
| ALTER PROCEDURE [dbo].[spGetAllItems]@condition varchar(7500),@ModelId int =0,@CategoryId int=0,@SourcingType int=0,@StartRow as int = 1,@StopRow as int = 100000,@SortField varchar(50),@SortOrder varchar(4)ASDECLARE @l_sql NVARCHAR(MAX)Declare @t_table table(Rownum int,...(other columns))SET @l_sql = 'INSERT INTO @t_table(<column names>,rownum) select <column names>,ROW_NUMBER() OVER (ORDER BY ' + @SortField + ' ' + @SortOrder + ')' 'from <your table> where <condition>'EXEC Sp_ExecuteSQL @l_sqlSELECT <columnnames>FROM @t_tableWHERE Rownum BETWEEN @l_StartRow AND @l_EndRow |
 |
|
|
sivrin
Starting Member
4 Posts |
Posted - 2007-12-19 : 23:38:34
|
| Hi,Thanks for your suggestion. I'll try and let u know. |
 |
|
|
|
|
|