I achieved the above by
DECLARE @var nvarchar(100)
SET @var='/Fields/Field1,/Fields/Field2'
DECLARE @sqlCommand varchar(1000)
SET @sqlCommand = 'SELECT PricingXml.query('''+@var+''') from MyTable'
EXEC (@sqlCommand)
Question
I have a query in sql as following
select PricingXML.query('/Fields/Field1,/Fields/Field2') from T_SMPricingData
PricingXML is a xml column in the table T_SMPricingData.
I need to pass the xquery from a variable to the query like the following
declare @var nvarchar(100)
set @var='/Fields/Field1,/Fields/Field2'
select PricingXML.query('sql:variable("@var")') from T_SMPricingData
How can I use the variable correctly. It is not working in the intended manner
Edit My output for the original correct query is
<Field1>10</Field1><Field2>11</Field2>
<Field1>20</Field1>
The data in the column looks like
<Fields>
<Field1>20</Field1>
<Field3>22</Field3>
<Field4>23</Field4>
</Fields>
Link for sql fiddle http://www.sqlfiddle.com/#!3/12d7c/6/0
La solution
I achieved the above by
DECLARE @var nvarchar(100)
SET @var='/Fields/Field1,/Fields/Field2'
DECLARE @sqlCommand varchar(1000)
SET @sqlCommand = 'SELECT PricingXml.query('''+@var+''') from MyTable'
EXEC (@sqlCommand)