for-xml-path

Eliminate row in XML based on criteria

时光怂恿深爱的人放手 提交于 2019-12-31 03:38:05
问题 here's the script to create the needed data: CREATE TABLE [dbo].[TestXML]( [ID] [int] NOT NULL, [PID] [int] NULL, [Code] [int] NULL, [Col1] [int] NULL, [Col2] [int] NULL, [Col3] [decimal](6, 2) NULL, [Col4] [decimal](6, 2) NULL, [Col5] [int] NULL, CONSTRAINT [PK_TestXML] PRIMARY KEY CLUSTERED ([ID] ASC) ) GO CREATE TABLE [dbo].[XML_Columns_Reference]( [TableName] nvarchar(10) NULL, [TableNameXML] nvarchar(10) NULL, [ColumnName] nvarchar(10) NULL, [ColumnNameXML] nvarchar(10) NULL,

Replace “<” and “>” with “<” and “>” in sql server

若如初见. 提交于 2019-12-30 06:02:09
问题 Hi I am new to for xml I have a query like this SELECT ProjectId, ProjectCode, ProjectName, TechId, -- LocationId, ( SELECT GeoId,PoliticalDivisionId ,GeographicLocationName,IsoCode,Longitude,Latitude,ParentLocationId, t2.CreatedBy,t2.CreatedOn,t2.LastUpdatedBy,t2.LastUpdatedOn FROM GeographicLocation t2 WHERE GeoId = t1.LocationId FOR XML PATH('Location') ), RtoId, CreatedBy, CreatedOn, LastUpdatedBy, LastUpdatedOn FROM Project t1 where ProjectId=1 FOR XML PATH('ProjectInfo') it return the

Unable to add condition in FOR XML part

折月煮酒 提交于 2019-12-25 09:52:01
问题 I have generated a FOR XML with the help from this Link. In the solution there is a condition SELECT 'String' as [Cell/Data/@ss:Type], [Col1] as [Cell/Data], '', 'String' as [Cell/Data/@ss:Type], [Col2] as [Cell/Data], '', I want to add a condition to this part which says SELECT CASE WHEN COL1 LIKE '%SUP%' THEN 'String' as [Cell/ss:Data/@ss:Type] ELSE 'String' as [Cell/Data/@ss:Type] END , [Col1] as [Cell/Data], '', But i am getting issues. Kindly help me with this. This basic code above

Unable to add condition in FOR XML part

大憨熊 提交于 2019-12-25 09:51:22
问题 I have generated a FOR XML with the help from this Link. In the solution there is a condition SELECT 'String' as [Cell/Data/@ss:Type], [Col1] as [Cell/Data], '', 'String' as [Cell/Data/@ss:Type], [Col2] as [Cell/Data], '', I want to add a condition to this part which says SELECT CASE WHEN COL1 LIKE '%SUP%' THEN 'String' as [Cell/ss:Data/@ss:Type] ELSE 'String' as [Cell/Data/@ss:Type] END , [Col1] as [Cell/Data], '', But i am getting issues. Kindly help me with this. This basic code above

How do I remove redundant namespace in nested query when using FOR XML in SQL Function

烈酒焚心 提交于 2019-12-25 08:56:21
问题 There is a solution for remove namespace here!, but I need it works in SQL function, I'm getting this error: The FOR XML clause is invalid in views, inline functions, derived tables, and subqueries when they contain a set operator. To work around, wrap the SELECT containing a set operator using derived table syntax and apply FOR XML on top of it. Can somebody help me? Thanks Xtyan From comment: This is the needed XML-output <zoo xmlns:an="uri:animal"> <an:animal species="Mouse"> <an:legs> <an

For XML Path: How to keep Attribute and Value in the same node

戏子无情 提交于 2019-12-24 01:36:08
问题 I have some problem when using FOR XML PATH . My situation is: I have run the script as below, the attribute CCY and value of AMOUNT is merged to the same node: Script 1: SELECT 'USD' AS 'Amount/@Ccy', 123000 AS Amount, 'Foo' AS Foo FOR XML PATH('root'), TYPE; Result 1: Only 1 AMOUNT node in root node <root> <Amount Ccy="USD">123000</Amount> <Foo>Foo</Foo> </root> Script 2: I change the order of Foo to the middle, and the result is wrong SELECT 'USD' AS 'Amount/@Ccy', 'Foo' AS Foo, 123000 AS

SQL Server FOR XML - Basic query

大兔子大兔子 提交于 2019-12-24 00:58:12
问题 I have been given an XML document that I want to generate via a SQL script, I've not done something like this and haven't been able to find any examples that can lead me to being able to generate the final XML I need (and I'm not sure which of the possible methods available if one is better suited to what I need - EXPLICIT or PATH or if its even possible). I'm hoping somebody with some experience in generating XML from SQL will be able to point me in the right direction (or tell me what I'm

SQL FOR XML PATH list and COUNT

纵然是瞬间 提交于 2019-12-24 00:55:38
问题 I have a table such as: |Date |Name| -------------------- |'20-May-2011'|Bob | |'20-May-2011'|Fred| |'20-May-2011'|Jim | |'21-May-2011'|Bob | |'21-May-2011'|Ed | |'22-May-2011'|Bill| I need a query to return: |Date |Count|Names | -------------------------------------- |'20-May-2011'| 3|'Bob, Fred, Jim'| |'21-May-2011'| 2|'Bob, Ed' | |'22-May-2011'| 1|'Bill' | In other words, I want a list and a count of the names by date. The best I can come up with is: SELECT list.[Date], [Count], [Names]

TSQL: FOR XML PATH('') Failing To Group

非 Y 不嫁゛ 提交于 2019-12-19 15:25:21
问题 I'm trying to group column values by a specific column using FOR XML PATH('') in TSQL. This is the result in both cases (note that the without XML code - ie: SELECT * FROM @xml - is the same as the with XML code): Class | Animals ================================= Asteroidea | Starfish Mammalia | Dog Mammalia | Cat Mammalia | Coyote Reptilia | Crocodile Reptilia | Lizard According to this article and this article (note that the second article leaves out the GROUP BY , which I'm unsure how the

Concatenation of strings by for xml path

淺唱寂寞╮ 提交于 2019-12-19 09:26:50
问题 Hi! Today I learned for xml path technique to concatenate strings in mssql. Since I've never worked with xml in mssql and google hasn't helped, I need to ask you. Let's imagine the default case. We need to concatenate some strings from a table: declare @xmlRepNames xml = ( select ', [' + report_name + ']' from ( select distinct report_order, report_name from #report ) x order by report_order for xml path(''), type) select stuff((select @xmlRepNames.value('.', 'nvarchar(max)')), 1, 1, '') So I