Hello all,let me first write down the tables that I have. I am using SQL Server 7.0.if exists (select * from sysobjects where id = object_id(N'[dbo].[Order]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[Order]GOCREATE TABLE [dbo].[Order] ( [OrderID] [varchar] (5) NOT NULL , [CustomerID] [varchar] (5) NOT NULL , [OrderDate] [varchar] (8) NOT NULL , [bitmapTest] [int] NOT NULL )GOif exists (select * from sysobjects where id = object_id(N'[dbo].[codeTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[codeTable]GOCREATE TABLE [dbo].[codeTable] ( [bitmapID] [int] NOT NULL , [description] [varchar] (50) NOT NULL ) ON [PRIMARY]GO
I have following data in both tables.use cascadeTestgoinsert into [dbo].[codeTable] (bitmapID,description) select 1, 'A' UNION all select 2, 'B' UNION all select 4, 'C' UNION all select 8, 'D'insert into [dbo].[Order] (OrderID,CustomerID,OrderDate,bitmapTest) select 1, 'BILBO', '19/04/99', 12 union ALL SELECT 2, 'FRODO', '18/05/99', 9 UNION ALL SELECT 3, 'GNDLF', '17/04/99', 5
Now, the bitmapTest field in dbo.Order table is a number that shows the combination of string value. This combination can be generated by looking up the table dbo.codeTable and perform bitwise AND operation with it. So, its a bitmap implementation. So, in dbo.Order table, first entry has bitmapTest value = 12. That means it is a combination of 'C' and 'D' from dbo.codeTable. So, I am writing a stored procedure that takes an integer as its argument and should return the string value with the combination mentioned above as comma separated string.So, I am creating a stored procedure named 'sp_dynamicDecode' which takes an integer argument. When I execute that procedure like this: exec sp_dynamicDecode 12It should give me output as 'C,D'.The stored procedure code is mentioned below:use cascadeTestgoif exists (select * from sysobjects where id = object_id(N'[dbo].[sp_dynamicDecode]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[sp_dynamicDecode]GOcreate procedure sp_dynamicDecode( @codeNo int)ASset nocount ondeclare @outputString varchar(50)declare @counter intdeclare @rowcounter intdeclare @tempCodeNo_local intdeclare @tempString_local varchar(20)declare @tempOutput varchar(50)declare @queryOne nvarchar(1000)declare @queryTwo nvarchar(1000)set @counter = (select count(*) from [dbo].[codeTable])set @rowcounter = 0set @outputString = ''while(@counter > 0)begin set @queryOne = 'select top 1 bitmapID from [dbo].[codeTable] where bitmapID not in (select top '+ cast(@rowcounter as nvarchar) +' bitmapID from [dbo].[codeTable] order by bitmapID) order by bitmapID' execute sp_executesql @tempCodeNo_local = @queryOne print @tempCodeNo_local set @queryTwo = '(select description from [dbo].[codeTable] where bitmapID = ' + cast(@tempCodeNo_local as nvarchar) execute @tempString_local = sp_executesql @queryTwo set @tempOutput = ', ' + @tempString_local print @tempOutput if (@codeNo & @tempCodeNo_local) = @tempCodeNo_local set @outputString = @outputString + @tempOutput set @counter = @counter - 1 set @rowcounter = @rowcounter + 1endset @outputString = right(@outputString, len(@outputString)-2)select @outputString
This procedure does not work and gives me an error. I tried print statements to debug and find out that the variables @tempCodeNo_local don't get the value assigned from the query that I run. That does not give proper result for the entire procedure.So, any suggestions to make this working.My basic aim is to create a view to display dbo.Order table information where such description will be displayed into a column inplace of the bitmapTest field. So that field gets replaced by a field with comma separated string values for that particular number.Thanks in advance for your suggestions and solutions.Ujjaval