简体   繁体   English

SQL Server报告服务

[英]SQL Server reporting services

how to show all values of a particular field in a text box ??? 如何在文本框中显示特定字段的所有值? ie. 即。 for eg. 例如 when u run the SP, u'll be getting 3 rows. 当您运行SP时,您将获得3行。 and i want to show the (eg.empname) in a textbox each value separated by a comma. 我想在文本框中显示(例如.empname)每个值用逗号分隔。 (ram, john, sita). (拉姆,约翰,西塔)。

I had this problem the other day. 前几天我遇到了这个问题。 If you are using SQL 2005 you can use the CROSS APPLY function. 如果您使用的是SQL 2005,则可以使用CROSS APPLY函数。

Here is a sample; 这是一个示例;

Structure; 
ID TYPE TEXT
1 1 Ram
2 1 Jon
3 2 Sita
4 2 Joe


Expecteed Output;
ID TYPE TEXT
1 1 Ram, Jon
2 2 Sita, Joe

Query; 
SELECT t.TYPE,LEFT(tl.txtlist,LEN(tl.txtlist)-1)
FROM(SELECT DISTINCT TYPE FROM Table)t
CROSS APPLY (SELECT TEXT + ','
             FROM Table
             WHERE TYPE=t.TYPE
             FOR XML PATH(''))tl(txtlist)

Hope this helps :) 希望这可以帮助 :)

Remember you'll need to select this as something in your sp, then bind that to the textbox on your report. 请记住,您需要在sp中选择它,然后将其绑定到报表上的文本框。 Good luck! 祝好运!

In 2005 the cross apply looks like a good solution (haven't used it myself). 在2005年,交叉申请似乎是一个不错的解决方案(我自己从未使用过)。 I have usually solved this by creating a UDF that concatenates the values by looping through a cursor. 我通常通过创建一个UDF来解决此问题,该UDF通过遍历游标将值连接起来。

The question should be "How to concatenate rows" http://databases.aspfaq.com/general/how-do-i-concatenate-strings-from-a-column-into-a-single-row.html 问题应该是“如何连接行” http://databases.aspfaq.com/general/how-do-i-concatenate-strings-from-a-column-into-a-single-row.html

CROSS APPLY works or FOR XML are good alternatives vs a cursor 与游标相比,CROSS APPLY作品或FOR XML是不错的选择

SELECT 
CustomerID, 
SalesOrderIDs = REPLACE( 
    ( 
        SELECT 
            SalesOrderID AS [data()] 
        FROM 
            Sales.SalesOrderHeader soh 
        WHERE 
            soh.CustomerID = c.CustomerID 
        ORDER BY 
            SalesOrderID 
        FOR XML PATH ('') 
    ), ' ', ',') 

FROM Sales.Customer c ORDER BY CustomerID 从Sales.Customer c按客户ID排序

You could use a Number/Tally table as well 您也可以使用数字/提示表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM