Question : @Xml.nodes - Query

Hi,
 
    I am trying to query an xml string in Sql Server 2005.  The following code works fine.

   SELECT
      [ColumnName] = ParamValues.Col.value('ColumnName[1]','VARCHAR(50)'),
      [ColumnValue] = ParamValues.Col.value('ColumnValue[1]','VARCHAR(500)')  
      FROM @Xml.nodes( '/TestEntry/HeaderColumns/Col' ) as ParamValues(Col)

But when I tried to pass  '/TestEntry/HeaderColumns/Col' dynamically, SQL Server 2005 throws an error. Please find the code and the error message below.

DECLARE @Path NVarchar(500)

SET @Path =  '/TestEntry/HeaderColumns/Col'

SELECT
      [ColumnName] = ParamValues.Col.value('ColumnName[1]','VARCHAR(50)'),
      [ColumnValue] = ParamValues.Col.value('ColumnValue[1]','VARCHAR(500)')  
      FROM @Xml.nodes(@Path) as ParamValues(Col)

ERROR:
Msg 8172, Level 16, State 1, Procedure pr_PAFDetailsDataSave, Line 63
The argument 1 of the xml data type method "nodes" must be a string literal.

Could anyone help me to solve this issue?

Thanks and regards
Vivek  

Answer : @Xml.nodes - Query

ok, try this:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
 
ALTER PROCEDURE [dbo].[pr_DataSave]
(
        @Xml XML
)
AS
 
SET NOCOUNT ON  
 
CREATE TABLE #tblXmlData
(
        Id INT IDENTITY(1,1) NOT NULL,
        ColumnName VARCHAR(80),
        ColumnValue VARCHAR(500),
        ColStatus VARCHAR(1) DEFAULT 0
)
 
DECLARE @Index INT
DECLARE @Sql VARCHAR(5000)
DECLARE @Values VARCHAR(5000)  
DECLARE @ColName VARCHAR(50)
DECLARE @ColValue VARCHAR(500)
 
DECLARE @x nvarchar(max)
DECLARE @Path NVarchar(500)
 
 
SET @x = N'
   
        SELECT  
                [ColumnName] = ParamValues.Col.value(''ColumnName[1]'',''VARCHAR(50)''),
                [ColumnValue] = ParamValues.Col.value(''ColumnValue[1]'',''VARCHAR(500)'')    
        FROM  @Xml.nodes(''' +  @Path + ''' ) as ParamValues(Col)'
       
INSERT INTO #tblXmlData(ColumnName, ColumnValue)    
exec sp_executesql @x, N'@Xml XML', @Xml
 
GO
Random Solutions  
 
programming4us programming4us