i have a 4 tables
D2D--------------------------ID,Name (Id pk)
[D2D_IPAddress]--------ID ,D2D_ID,IPAddress D2D_ID is Foreign key to table D2D
D2D_WWN--------------ID ,D2D_ID,WWN
D2D_SerialNumber----ID,D2D_ID,Serialnumber
a D2D can have any number of IPaddresses ,wwn,serialnumbers
Ex:
tables data for Example
ID Name | ID D2D_ID IPAddress | ID D2D_ID WWN | ID D2D_ID SNo
1 D2D-A | 1 1 1.2.3.4 | 1 1 abc | 1 2 Vw123
2 D2D-B | 2 1 1.23.4.5 | 2 1 cdr | 3 2 1.4.5.6
i have a query that would convert rows to columns and display me a result in a single row like below.Please tell me how to ask my where condition to check for a particular IPAddress
ID Name IPAddress1 IPAddress2 WWN1 WWN2 SerialNumber1
1 D2D-A 1.2.3.4 1.23.4.5 abc cdr NULL
2 D2D-B 1.4.5.6 Null NULL NULL EW234
but i would like to search from the dynamic colums created by Query
For Example if i am seraching for Ip i would like the dynamic sql to search the value 1.2.3.4 for IPAddress1 and IPAddress2.Please help to search for IPAddress in the dynamic Query below.how would i search in @col1 for a specific IPaddress.Please help!!
GO
/****** Object: StoredProcedure [dbo].[D2D_SearchByColName_Value] Script Date: 08/15/2013 08:45:00 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[D2D_SearchByColName_Value]
@ColumnName varchar(100), --i will provide these inputs
@ColumnValue varchar(100)
As
DECLARE @MaxCount INT;
DECLARE @MaxCount1 INT;
DECLARE @MaxCount2 INT;
SELECT @MaxCount = max(cnt)
FROM (
SELECT S.Id
,count(A.IPAddress) AS cnt
FROM dbo.D2D S INNER JOIN dbo.D2D_IPAddress A on S.Id = A.D2D_ID
GROUP BY S.Id
) X;
SELECT @MaxCount1 = max(cnt1)
FROM (
SELECT S.Id
,count(B.WWN) AS cnt1
FROM dbo.D2D S INNER JOIN dbo.D2D_WWN B on S.Id = B.D2D_ID
GROUP BY S.Id
) X1
SELECT @MaxCount2 = max(cnt2)
FROM (
SELECT S.Id
,count(C.Serial_Number) AS cnt2
FROM dbo.D2D S INNER JOIN dbo.D2D_SerialNumber C on S.Id = C.D2D_ID
GROUP BY S.Id
) X2
DECLARE @SQL NVARCHAR(max), @Col1 nvarchar(max), @Col2 nvarchar(max),
@Col3 nvarchar(max),@i INT;
SET @i = 0;
WHILE @i < @MaxCount
BEGIN
SET @i = @i + 1;
SET @Col1 = COALESCE(@Col1 + ', ', '') +
'MAX(CASE WHEN Rn1 = ' + cast(@i AS NVARCHAR(10)) +
' THEN IPAddress END) AS IPAddress' + cast(@i AS NVARCHAR(10));
END
SET @i = 0;
WHILE @i < @MaxCount1
BEGIN
SET @i = @i + 1;
SET @Col2 = COALESCE(@Col2 + ', ', '') +
'MAX(CASE WHEN Rn2 = ' + cast(@i AS NVARCHAR(10)) +
' THEN WWN END) AS WWN' + cast(@i AS NVARCHAR(10));
END
SET @i = 0;
WHILE @i < @MaxCount2
BEGIN
SET @i = @i + 1;
SET @Col3 = COALESCE(@Col3 + ', ', '') +
'MAX(CASE WHEN Rn3 = ' + cast(@i AS NVARCHAR(10)) +
' THEN Serial_Number END) AS Serial_Number' + cast(@i AS NVARCHAR(10));
END
SET @SQL = N';WITH CTE AS (
SELECT S.ID,S.Name,S.DeviceName,S.Location_ID,S.Product_Number,S.ILOAddress,S.Make,S.Model,S.Date_Added,S.Purchase_Date,S.Last_Modified_Date
,S.Environment,S.Image,A.IPAddress, row_number() OVER (PARTITION BY S.ID ORDER BY A.IPAddress) AS Rn1
FROM dbo.D2D S INNER JOIN dbo.D2D_IPAddress A on S.Id = A.D2D_ID
), cte2 AS (
SELECT S.ID,S.Name,S.DeviceName,S.Location_ID,S.Product_Number,S.ILOAddress,S.Make,S.Model,S.Date_Added,S.Purchase_Date,S.Last_Modified_Date
,S.Environment,S.Image,B.WWN as WWN, ROW_NUMBER() over (partition by S.ID order by B.wwn) as Rn2
from dbo.D2D S INNER JOIN dbo.D2D_WWN B on S.Id = B.D2D_ID), cte3 AS (
SELECT S.ID,S.Name,S.DeviceName,S.Location_ID,S.Product_Number,S.ILOAddress,S.Make,S.Model,S.Date_Added,S.Purchase_Date,S.Last_Modified_Date
,S.Environment,S.Image,C.Serial_Number as Serial_Number, ROW_NUMBER() over (partition by S.ID order by C.Serial_Number) as Rn3
from dbo.D2D S INNER JOIN dbo.D2D_SerialNumber C on S.Id = C.D2D_ID), cte4 AS (
SELECT COALESCE(A.ID, B.ID,C.ID) as ID,
COALESCE(A.Name,B.Name,C.Name)as Name,
COALESCE(A.DeviceName,B.DeviceName,C.DeviceName)as DeviceName,
COALESCE(A.Location_ID,B.Location_ID,C.Location_ID)as Location_ID,
COALESCE(A.Product_Number,B.Product_Number,C.Product_Number)as Product_Number,
COALESCE(A.ILOAddress,B.ILOAddress,C.ILOAddress)as ILOAddress,
COALESCE(A.Make,B.Make,C.Make)as Make,
COALESCE(A.Model,B.Model,C.Model)as Model,
COALESCE(A.Date_Added,B.Date_Added,C.Date_Added)as Date_Added,
COALESCE(A.Last_Modified_Date,B.Last_Modified_Date,C.Last_Modified_Date)as Last_Modified_Date,
COALESCE(A.Purchase_Date,B.Purchase_Date,C.Purchase_Date)as Purchase_Date,
COALESCE(A.Environment,B.Environment,C.Environment)as Environment,
COALESCE(A.Image,B.Image,C.Image)as Image,
A.IPAddress, A.Rn1,
B.WWN, B.Rn2,C.Serial_Number,c.Rn3 from cte A FULL JOIN cte2 B ON A.ID = B.ID
AND A.Rn1 = B.RN2 FULL JOIN cte3 C ON A.ID=C.ID and B.ID=C.ID and A.Rn1=C.Rn3 AND B.Rn2=C.Rn3 )
select ID,Name,DeviceName,Location_ID,Product_Number,ILOAddress,Make,Model,Date_Added,Purchase_Date,Last_Modified_Date
,Environment,Image,' + @Col1 + ', ' + @Col2 + ' ,' + @Col3 + ' FROM cte4
WHERE
' + @Col1 + '='1.2.3.4' ---how to check this
GROUP BY ID,Name,DeviceName,Location_ID,Product_Number,ILOAddress,Make,Model,Date_Added,Purchase_Date,Last_Modified_Date,Environment,Image;'
PRINT @SQL;
Execute @SQL
DDL statements for the tables
--------------------------------------
CREATE TABLE [dbo].[D2D](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Asset_ID] [int] NULL,
[Name] [varchar](100) NULL,
[DeviceName] [varchar](50) NULL,
[Product_Number] [varchar](50) NULL,
[ILOAddress] [varchar](500) NULL,
[User_ID] [varchar](100) NULL,
[Password] [varchar](100) NULL,
[Description] [varchar](500) NULL,
[Make] [varchar](50) NULL,
[Model] [varchar](50) NULL,
[Date_Added] [datetime] NULL,
[Purchase_Date] [datetime] NULL,
[Last_Modified_Date] [datetime] NULL,
[Environment] [varchar](50) NULL,
[Image] [varbinary](max) NULL,
[Miscellaneous] [varchar](1000) NULL,
CONSTRAINT [PK_D2DS] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
----------------------------------------------------------------------
CREATE TABLE [dbo].[D2D_IPAddress](
[D2D_ID] [int] NOT NULL,
[ID] [int] IDENTITY(1,1) NOT NULL,
[IPAddress] [varchar](20) NULL,
[Description] [varchar](500) NULL,
CONSTRAINT [PK_D2D_IPADDRESS] PRIMARY KEY CLUSTERED
(
[D2D_ID] ASC,
[ID] ASC
)
ALTER TABLE [dbo].[D2D_IPAddress] WITH CHECK ADD CONSTRAINT [FK_IPADDRESS_D2D] FOREIGN KEY([D2D_ID])
REFERENCES [dbo].[D2D] ([ID])
GO
ALTER TABLE [dbo].[D2D_IPAddress] CHECK CONSTRAINT [FK_IPADDRESS_D2D]
GO
----------------------------------------------------------------------
CREATE TABLE [dbo].[D2D_WWN](
[D2D_ID] [int] NOT NULL,
[ID] [int] IDENTITY(1,1) NOT NULL,
[WWN] [varchar](20) NULL,
[Description] [varchar](500) NULL,
CONSTRAINT [PK_D2D_WWN] PRIMARY KEY CLUSTERED
ALTER TABLE [dbo].[D2D_WWN] WITH CHECK ADD CONSTRAINT [FK_WWN_D2D] FOREIGN KEY([D2D_ID])
REFERENCES [dbo].[D2D] ([ID])
GO
ALTER TABLE [dbo].[D2D_WWN] CHECK CONSTRAINT [FK_WWN_D2D]
GO
-----------------------------------------------------------------------------------------
CREATE TABLE [dbo].[D2D_SerialNumber](
[D2D_ID] [int] NULL,
[Serial_Number] [varchar](500) NULL,
CONSTRAINT [uc_D2DSNO] UNIQUE NONCLUSTERED
(
[Serial_Number] ASC
)
ALTER TABLE [dbo].[D2D_SerialNumber] WITH CHECK ADD CONSTRAINT [FK_D2D_SerialNumber_D2D] FOREIGN KEY([D2D_ID])
REFERENCES [dbo].[D2D] ([ID])
GO
ALTER TABLE [dbo].[D2D_SerialNumber] CHECK CONSTRAINT [FK_D2D_SerialNumber_D2D]
GO
lucky