for-xml

Cast FOR XML to Varchar(max) [duplicate]

巧了我就是萌 提交于 2019-12-12 13:26:10
问题 This question already has an answer here : For XML length limitation (1 answer) Closed 6 years ago . I have a query that returns XML which I want to convert to varchar. My query returns 93,643 characters of XML. When I try to cast my xml result as varchar, I only get 43,679 characters when I copy the result set to a text editor. When I do len(xmlString), I get 93,643 characters. I know from this post that varchar(max) can have up to 2^31 characters and 1 byte = 1 character, but it seems to be

SQL FOR XML list of nested rows

◇◆丶佛笑我妖孽 提交于 2019-12-11 12:20:34
问题 I have this super long SQL query, now what this query does is the following, creates the first level which is the Job_No....next in the second level gets all the 'BaselineStart' however this separates each BaselineStart with baseOrSchedStartList, what I am looking to do is have 1 baseOrSchedStartList for each Job_No and have each 'string' under baseOrSchedStartList. I hope this makes sense. Here is my query: SELECT [Job_No] as '@Key', ( SELECT ISNULL(UserDate1,ScheduleTasks.BaselineStart) AS

Field name with space problem in case of for xml auto output

天大地大妈咪最大 提交于 2019-12-11 07:31:00
问题 my sql is very simple select ID as [Employee ID], EmpName as [Employee Name], Sal as [Salary] from Emp FOR XML AUTO, ELEMENTS, ROOT('customers') when i execute this sql then i am getting output in xml format. the xml output is <customers> <Emp> <Employee_x0020_ID>1</Employee_x0020_ID> <Employee_x0020_Name>Tridip</Employee_x0020_Name> <Salary>2500</Salary> </Emp> <Emp> <Employee_x0020_ID>2</Employee_x0020_ID> <Employee_x0020_Name>Ari</Employee_x0020_Name> <Salary>4000</Salary> </Emp> <Emp>

Assign CSS class to HTML tags generated with SQL 'FOR XML'

末鹿安然 提交于 2019-12-10 19:53:23
问题 I am getting table rows and table data (with HTML tags) from SQL using 'FOR XML'. Is there a way I could assign CSS classes to the html tags in SQL? What I am currently getting: <tr><td>Name</td><td>Value</td></tr> SQL query: SELECT (SELECT [Name] as [td] FOR XML PATH(''), type), (SELECT [Value] as [td] FOR XML PATH(''), type) FROM table FOR XML PATH('tr') Desired output: <tr class="test1"> <td class="test2">Name</td> <td class="test3">Value</td> </tr> 回答1: I know I am answering my own

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

Sql for xml: how to avoid a specific field is output as attribute?

筅森魡賤 提交于 2019-12-10 12:18:31
问题 The stating point for this qusetion is Control on XML elements nesting using FOR XML I would like that the output changes from <security AccessLevel="5" /> to <security>5<security/> Basically instead of displaying AccessLevel as attribute I would like its value becomes the value of the element security . How to achieve such a result. I copy here the example from the linked post for clarity: DECLARE @Employees table( EmpID int NOT NULL, Name nvarchar(50), Surname nvarchar(50), DateOfBirth date

FOR XML PATH in Sql Server

对着背影说爱祢 提交于 2019-12-08 00:45:02
问题 NOTE: I have solved the majority of this problem but have run into a snag. Read to the bottom please. You will see where I added a (NOTE) section. TIA. I have a rather extensive join query that I want dumped to XML. I have it almost working but I am missing a concept in here somewhere. My query (abbreviated) looks like: SELECT Campaign.CampaignId "Campaign/ID" , Campaign.CompanyId "Campaign/CompanyID" , Campaign.CampaignName "Campaign/Name" ... , Audio.AudioID "Campaign/Audio/ID" , Audio.

Using sqlcmd with FOR XML returns Invalid Object name 'dbo.Table' yet Query runs in SSMS

…衆ロ難τιáo~ 提交于 2019-12-07 17:33:59
问题 This is my query: SELECT title1, [precinct percent] AS [PrecinctPercent], leader, [leader percent] AS [LeaderPercent], Winner, WinningVotes, leader2, [leader2 percent] AS [Leader2Percent], Loser, LosingVotes FROM [leader].dbo.[RACE] r inner join (select rc.[race number], max(case when seqnum = 1 then [candidate num] end) as Winner, max(case when seqnum = 1 then Votes end) as WinningVotes, max(case when seqnum = 2 then [candidate num] end) as Loser, max(case when seqnum = 2 then Votes end) as

SQL Server 2008: How to remove namespace from the elements, but let it display on root

偶尔善良 提交于 2019-12-07 15:57:31
问题 I am using SQL Server2008 FOR XML clause to generate XML file using bcp. When I use the below query, the result is fine: WITH XMLNAMESPACES ( 'http://base.google.com/ns/1.0' AS g, DEFAULT 'http://www.w3.org/2005/Atom' ) select ID, Name AS "g:Name" from PaymentMethods For XML PATH ('entry'), Root('feed') Result: <feed xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0"> <entry> <ID>1</ID> <g:Name>BPay</g:Name> </entry> <entry> <ID>2</ID> <g:Name>Cash</g:Name> </entry> <

Can't set output of 'WITH XMLNAMESPACES…FOR XML PATH' to a variable?

半世苍凉 提交于 2019-12-06 22:39:59
问题 I have a query like the following: ;WITH XMLNAMESPACES ( DEFAULT 'http://www.somewhere.com') SELECT ( 'SOMETHING' ) FOR XML PATH('RootNode'), TYPE Running this works fine. However, I run into troubles when I try to set the XML output to a variable like this: DECLARE @MYXML AS XML SELECT @MYXML = ( ;WITH XMLNAMESPACES ( DEFAULT 'http://www.somewhere.com') SELECT ( 'SOMETHING' ) FOR XML PATH('RootNode'), TYPE ) This just give me a syntax error :-( Any ideas on how to accomplish this would be