Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

carriage return in sql server 2012

Hey I am using the following query to display the problem list separated by commas.

SELECT tt.VrNo, STUFF((select ','+ Er1.ErrorDesc 
from ( select * from CallRegErrors )as Main 
left join ErrorMaster ER1 on Main.ErrorCode=ER1.ErrorCode
WHERE (main.VrNo = tt.VrNo)
FOR XML PATH('')) ,1,1,'') AS Problemlist

query is giving the output like a,b,c,d etc

But my actual requirement is I want to display each error description in a new line like,
a
b
c
d
etc

I tried the following query for it:

SELECT tt.VrNo, STUFF((select char(13)+char(10)+ Er1.ErrorDesc
from ( select * from CallRegErrors )as Main 
left join ErrorMaster ER1 on Main.ErrorCode=ER1.ErrorCode
WHERE (main.VrNo = tt.VrNo)
FOR XML PATH('')) ,1,1,'') AS Problemlist

and also i have used

SELECT tt.VrNo,Replace(STUFF((select ','+ Er1.ErrorDesc as [text()] from ( select * from CallRegErrors )as Main left join ErrorMaster ER1 on Main.ErrorCode=ER1.ErrorCode WHERE (main.VrNo = tt.VrNo) FOR XML PATH('')),1,1,''),',',char(13)+char(10)) AS Problemlist from (select main.VrNo, Er1.ErrorDesc from ( select * from CallRegErrors )as Main left join ErrorMaster ER1 on Main.ErrorCode=ER1.ErrorCode )as tt group by tt.VrNo but now get the problem list seperated by spaces instead of commas after using the above query

but its does not give the output that i want.

please help..

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think we need more information before we can help you.

I think you are trying to format the information at the child level in a parent child relationship into a list. You probably saw something like this blog on the web.

However, your query is not correctly formatted.

Is the ErrorMaster (Production.ProductCategory) the parent and CallRegErrors (SUB.ProductCategoryID) the child?

If so just change the query to those table name field names for it to work.

I used the REPLACE function on the overall result to change COMMAS to CR + LF.

-- Sample database
USE AdventureWorks2012
GO

-- Change SQL from www.sqlandme.com for this users problem
SELECT      
    CAT.Name AS [Category],
    REPLACE(STUFF((
        SELECT ',' + SUB.Name AS [text()]                        
        FROM Production.ProductSubcategory SUB
        WHERE SUB.ProductCategoryID = CAT.ProductCategoryID
        FOR XML PATH('') 
        ), 1, 1, '' ), ',', CHAR(13) + CHAR(10))
    AS [Sub Categories]
FROM  Production.ProductCategory CAT

You can only see carriage returns in the output window when the type is set to TEXT in SSMS.

I hope this solves your problem. If not, please write back with more information!!

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...