for-xml-path

How do I avoid character encoding when using “FOR XML PATH”?

不打扰是莪最后的温柔 提交于 2019-12-17 17:45:12
问题 I'm looking to create a comma-separated list of values from a SQL Server 2005 table, just like in JanetOhara's question. I'm using a query similar to the one presented in techdo's answer to the question. Everything is working, except the list of values is getting XML encoded. What should be: Sports & Recreation,x >= y Is instead returning as: Sports & Recreation,x <= y Is there a way to disable the XML character encoding when using "FOR XML" in SQL Server? 回答1: You just need to use the right

FOR XML PATH(''): Escaping “special” characters

假装没事ソ 提交于 2019-12-17 09:43:31
问题 This code basically translates characters based on position in one string to the character at the same position in another string and it runs for all rows in the table. When I run this (simplified version): DECLARE @R char(40) DECLARE @U char(40) SET @R=' abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+'+char(181) SET @U=REVERSE(@R) DECLARE @TestTable TABLE (RowID int identity(1,1) primary key, Unreadable varchar(500)) INSERT INTO @TestTable VALUES ('+µt$zw!*µsu+yt!+s$xy') INSERT INTO @TestTable VALUES

FOR XML SQL Server - Variable Element name in output XML

放肆的年华 提交于 2019-12-13 14:17:12
问题 I'm quite new to FOR XML in SQL Server, I've searched considerable and I can't find an answer to this. Can I have a variable element name using 'for xml' where the element name is not hard-coded and is instead take from a cell in each row? Take the following example... Table ORDERS : ID STATUS TIME AMOUNT ------------------------------------ 1 COMPLETE 02:31 2355 2 ACCEPTED 02:39 6653 3 ACCEPTED 04:21 4102 4 RECEIVED 05:03 4225 FOR XML query: select ID, TIME as STATUS_TIME, AMOUNT as CURRENT

How to resolve maximum length of 128 on a column alias when using FOR XML PATH?

对着背影说爱祢 提交于 2019-12-12 03:14:13
问题 With the following SQL, I am generating 2 rows and wrapping them in individual soap envelopes. declare @XmlDoc xml ;with XMLNAMESPACES ( 'http://schemas.xmlsoap.org/soap/envelope/' as soapenv, 'http://amsa.com/contract/baserequestcontract/v1.0' as H1, 'http://MyCompany.org/contract/mrmPerson/v1.0' as N1, 'http://MyCompany.org/contracts/person' as N2, 'http://MyCompany.org/contracts/demogTypes' as N3 ) select @XmlDoc = ( select top 2 PersonID as 'soapenv:Header/H1:PersonID', IsAuthorizedForUse

SQL Server XML file with multiple nodes named the same

懵懂的女人 提交于 2019-12-12 02:33:27
问题 I have this inner XML which I am passing across to a SQL Server stored procedure. As you can see, it contains multiple root nodes but additionally, it can also contain 1 to ' n ' number of LotResults child nodes. Is there a way I can manipulate this in the stored procedure so that I can retrieve all LotResults / Result nodes for each root node? So far I have declared the cursor which can deal with the top-level nodes: DECLARE cur CURSOR FOR SELECT tab.col.value('ID[1]','NCHAR(10)') as INT

SQL For xml path on int columns?

▼魔方 西西 提交于 2019-12-11 14:41:34
问题 Can I use FOR XML PATH in sql for int columns ? So that I could use it for something like: declare @contactIds = (select id from contacts) and then use it like this: select * from calls where contactId in (@contactIds) Is it possible ? 回答1: Is this what you want? select @contactIds = stuff((select ','+cast(id as varchar(8000)) from contacts for xml path('') ), 1, 1, ''); You can also use a subquery directly or a table variable: select * from calls where contactId in (select id from contacts);

Delete empty XML nodes using T-SQL FOR XML PATH

白昼怎懂夜的黑 提交于 2019-12-11 02:13:09
问题 I'm using FOR XML PATH to construct XML out of a table in SQL Server 2008R2. The XML has to be constructed as follows: <Root> <OuterElement> <NumberNode>1</NumberNode> <FormattedNumberNode>0001</KFormattedNumberNode> <InnerContainerElement> <InnerNodeOne>0240</InnerNodeOne> <InnerNodeStartDate>201201</InnerNodeStartDate> </InnerContainerElement> </OuterElement> </Root> According to the schema files, the InnerContainerElement is optional, while the InnerNodeOne is required. The schema files

I want generate XML file in a hierarchical form

元气小坏坏 提交于 2019-12-10 21:02:07
问题 I have a table like this (Actually it contains more 6000 records) IdIndustry | IndustryCode | IndustryName | ParentId --------------------------------- 1 | IND | Industry | NULL 2 | PHARM | Pharmacy | 1 3 | FIN | Finance | NULL 4 | CFIN | Corporate | 3 5 | CMRKT | Capital M | 4 DDL: CREATE TABLE [dbo].[tblIndustryCodes]( [IdIndustry] [int] IDENTITY(1,1) NOT NULL, [IndustryCode] [nvarchar](5) NULL, [IndustryName] [nvarchar](50) NULL, [ParentId] [int] NULL, CONSTRAINT [PK_tblIndustryCodes]

FOR XML PATH and xsi:nil attributes

吃可爱长大的小学妹 提交于 2019-12-10 16:47:26
问题 Good morning all, I have a large query utilising FOR XML PATH to output a .xml file.I have the main select which basically just represents the root ie select * from tbl for xml path ('root'),elements xsinil I then have subsequent nested selects within this main select i.e. select ( select null [level1], '2' [level2] from tbl for xml path('nested1'),type ), ( select null [level1], '2' [level2] from tbl for xml path('nested2'),type ) for xml path('root'),elements xsinil However, the element

Are CDATA sections really unnecessary?

爱⌒轻易说出口 提交于 2019-12-10 15:30:08
问题 This question is prompted by the rather militant refusal of developer Michael Rys to include the parsing of CDATA sections into FOR XML PATH because "There is no semantic difference in the data that you store." I have stored nuggets of HTML in CDATA nodes and other content that requires the use of special or awkward characters. However I don't feel qualified to challenge Rys's controversial assertion because, I suppose, technically he is correct in the scenarios where I've employed CDATA for