Showing posts with label sorted. Show all posts
Showing posts with label sorted. Show all posts

Tuesday, March 27, 2012

Configuration of Reporting Services

obviously until I get this sorted I cannot deploy my reports

in my Reporting Services Configuration

All ticked except initialisation (3)

in Database services ticked but on the right hand side there are warnings/errors

Verifying Database Edition - ticked

Verifying Database Version - warning

The current connection string is not valid and can't be used to connect to SQL Server

Where do I check this? (1)

Creating A Grant Rights script for LocalSystem = ticked

Assigning Reporting Services Rights to User = error (2)

System.Data.SqlClient.SqlException: The role 'RSExecRole' does not exist in the current database.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Microsoft.ReportingServices.Common.DBUtils.ApplyScript(String connectionString, String script)
at ReportServicesConfigUI.SqlClientTools.SqlTools.ApplyScript(String connectionString, String script)

Can someone tell me what I should be checking please?

thanks

HI !!!

I am also facing the same problem, The database server is sql 2000 personal edition while reporting server is sql 2005 evaluation.

In 'Database Setup' option displayes 'success'; but 'Verify Database Version' and 'Setting Connection Info For Reporting Server' are warnings while 'Create grants rights script' task is failing.

The 'Initialization' option is also failing. The message displayed is 'The report server cannot open connection to report server database (rsReportServerDatabaseUnavailable)'

Which setting should I check in report configuration? Or is this backward compatibility issue?

|||

On your reporting database,

Make sure under Security/Roles - that the RSExecRole exist, if not create it. It should also be assigned to the schema of RSExecRole. Finally, on your report service login User make sure that this user is assigned under the RSExecRole.

I hope this helps.

Ham

|||

HI Ham,

Thanks a lot for the reply.

I'll try out the solution suggested by you and let you know the outcome after that.

-Nishad

|||

Ham,

I am having the same problem. I need extra clarification, though. When you say check under Security/Roles, are you talking about doing this on the Reporting Services server or the database engine server that reporting services is connected to?

For instance, in my Object Explorer I see my reporting services server and under that in the tree is Security -> Roles. However, when I try to right-click on anything there it gives me the following message:

Unhandled exception has occured in a component in your application. If you click Continue, the application will ignore this error and attempt to continue.

The version of the report server database is either in a format that is not valid, or it cannot be read. The found version is 'Unknown'. the expected version is 'C.0.8.40'. To continue, update the version of the report server database and verify access rights. (rsInvalidReportServerDatabase).

Furthermore, when I try to upgrade the database by clicking 'Upgrade' in the Database Setup section of the Reporting Services Configuration Manager it gives me a green check on Verifying Database Edition and a yellow exclamation mark on Verifying Database Version. The warning says:

The current connection string is not valid and can't be used to connect to SQL Server.

So, if you mean I should add the RSExecRole to Security/Roles under my reporting services server, then I am in even deeper trouble than I thought. However, if you mean I should add them to Security/Server Roles under my database engine tree then I'm probably back in business.

I am loathe to try anything until I get clarification. Thanks for your kind help!

|||

Nevermind, I solved my problem.

I just needed to click the New button on the Database Setup page before trying to connect. Pretty silly on my part, but all's well that ends well.

|||

Ham and Philip,

I am having similary problem. When i am trying to get connection with my existing database, It says in task status

1- Varifying Database Edition (Pass)

2- Verifying Database Version (! Exclamitaion)

3- Creating Grant Rights for ServerName\UserName (Not Pass X)

In the browser when i am typing http://LocalServer/ReportServer That generate error that is

Reporting Services Error


The report server cannot open a connection to the report server database. The logon failed. (rsReportServerDatabaseLogonFailed) Get Online Help

Logon failure: unknown user name or bad password. (Exception from HRESULT: 0x8007052E)|||

Irfan,

If I remember correctly from when I was having my troubles, this may be due to ASPNET permissions on your web server.

You might google ASPNET reporting services server persmissions or something like that.

Best luck to you!

|||

Irfan,

This is stating that the "Report Database is failing" - First, verify that the login setup for Report Server logon in the Report Configuration Manager is correct.

You can verify that by connecting to the SQL2005 via studio management with the report server logon credentials

let me know if this works for you.

Configuration of Reporting Services

obviously until I get this sorted I cannot deploy my reports

in my Reporting Services Configuration

All ticked except initialisation (3)

in Database services ticked but on the right hand side there are warnings/errors

Verifying Database Edition - ticked

Verifying Database Version - warning

The current connection string is not valid and can't be used to connect to SQL Server

Where do I check this? (1)

Creating A Grant Rights script for LocalSystem = ticked

Assigning Reporting Services Rights to User = error (2)

System.Data.SqlClient.SqlException: The role 'RSExecRole' does not exist in the current database.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Microsoft.ReportingServices.Common.DBUtils.ApplyScript(String connectionString, String script)
at ReportServicesConfigUI.SqlClientTools.SqlTools.ApplyScript(String connectionString, String script)

Can someone tell me what I should be checking please?

thanks

HI !!!

I am also facing the same problem, The database server is sql 2000 personal edition while reporting server is sql 2005 evaluation.

In 'Database Setup' option displayes 'success'; but 'Verify Database Version' and 'Setting Connection Info For Reporting Server' are warnings while 'Create grants rights script' task is failing.

The 'Initialization' option is also failing. The message displayed is 'The report server cannot open connection to report server database (rsReportServerDatabaseUnavailable)'

Which setting should I check in report configuration? Or is this backward compatibility issue?

|||

On your reporting database,

Make sure under Security/Roles - that the RSExecRole exist, if not create it. It should also be assigned to the schema of RSExecRole. Finally, on your report service login User make sure that this user is assigned under the RSExecRole.

I hope this helps.

Ham

|||

HI Ham,

Thanks a lot for the reply.

I'll try out the solution suggested by you and let you know the outcome after that.

-Nishad

|||

Ham,

I am having the same problem. I need extra clarification, though. When you say check under Security/Roles, are you talking about doing this on the Reporting Services server or the database engine server that reporting services is connected to?

For instance, in my Object Explorer I see my reporting services server and under that in the tree is Security -> Roles. However, when I try to right-click on anything there it gives me the following message:

Unhandled exception has occured in a component in your application. If you click Continue, the application will ignore this error and attempt to continue.

The version of the report server database is either in a format that is not valid, or it cannot be read. The found version is 'Unknown'. the expected version is 'C.0.8.40'. To continue, update the version of the report server database and verify access rights. (rsInvalidReportServerDatabase).

Furthermore, when I try to upgrade the database by clicking 'Upgrade' in the Database Setup section of the Reporting Services Configuration Manager it gives me a green check on Verifying Database Edition and a yellow exclamation mark on Verifying Database Version. The warning says:

The current connection string is not valid and can't be used to connect to SQL Server.

So, if you mean I should add the RSExecRole to Security/Roles under my reporting services server, then I am in even deeper trouble than I thought. However, if you mean I should add them to Security/Server Roles under my database engine tree then I'm probably back in business.

I am loathe to try anything until I get clarification. Thanks for your kind help!

|||

Nevermind, I solved my problem.

I just needed to click the New button on the Database Setup page before trying to connect. Pretty silly on my part, but all's well that ends well.

|||

Ham and Philip,

I am having similary problem. When i am trying to get connection with my existing database, It says in task status

1- Varifying Database Edition (Pass)

2- Verifying Database Version (! Exclamitaion)

3- Creating Grant Rights for ServerName\UserName (Not Pass X)

In the browser when i am typing http://LocalServer/ReportServer That generate error that is

Reporting Services Error


The report server cannot open a connection to the report server database. The logon failed. (rsReportServerDatabaseLogonFailed) Get Online Help

Logon failure: unknown user name or bad password. (Exception from HRESULT: 0x8007052E)|||

Irfan,

If I remember correctly from when I was having my troubles, this may be due to ASPNET permissions on your web server.

You might google ASPNET reporting services server persmissions or something like that.

Best luck to you!

|||

Irfan,

This is stating that the "Report Database is failing" - First, verify that the login setup for Report Server logon in the Report Configuration Manager is correct.

You can verify that by connecting to the SQL2005 via studio management with the report server logon credentials

let me know if this works for you.

Monday, March 19, 2012

Conditional Split to send top 10, top20, and top 30 in three different flows ?

I have dataflow that i wish to split. The recordset that i uses is sorted by the Sort Task. After that i'll like to take the Top 10 records and send in one direction, then ill like to take the top 20 and send in another direction and finally Top 30 in a third direction.

How can i do that ?

You can use a script component to give each record a number using the technique described here: http://www.sqlis.com/default.aspx?37

Then you can filter on the values that are created using a conditional split transform component.

Make sense?

-Jamie

|||Yes but it seems to be a bit of a workaround. I was looking for an easier way like using SQL select top 10 * from....|||

cgpl wrote:

Yes but it seems to be a bit of a workaround. I was looking for an easier way like using SQL select top 10 * from....

Yeah I agree, that would be nice. Perhaps you could request it at betaplace or http://lab.msdn.microsoft.com/productfeedback/Default.aspx?

-Jamie|||You could do it all in one script component. Add some extra outputs and then direct the row to the correct output based on the counter value.

I'd demonstrate if I knew what the equivalent of buffer.DirectRow(<outputindex>) was in the script component, but that would be if the syntax for a full component.|||

DarrenSQLIS wrote:

You could do it all in one script component. Add some extra outputs and then direct the row to the correct output based on the counter value.

I'd demonstrate if I knew what the equivalent of buffer.DirectRow(<outputindex>) was in the script component, but that would be if the syntax for a full component.

Oh yeah, Darren's right. And the syntax he's on about is <buffername>.AddRow()

-Jamie|||The easiest way to deal with that, would be if you could disable the "Random" feature in the Row Sample Task. Then you could do a Multicast to 3 different Row Sample Tasks|||

yep, I have the same problem. I have a dataset containing a record for each bill in the past month and I want to flag the top 10 customers according to the aggregated amount pr customer.

Like this in plain SQL:

select top 10 CustName, sum(Amount) as sum_Amount into #ek_temp

from erhverv_kreditnota group by CustName order by sum_Amount

go

update erhverv_kreditnota

set top10 = 'Top 10'

where CustName in (select CustName from #ek_temp)

go

Any way to do this without scripting?

|||

cgpl wrote:

The easiest way to deal with that, would be if you could disable the "Random" feature in the Row Sample Task. Then you could do a Multicast to 3 different Row Sample Tasks

Yeah another excellent idea, you would assume that selecting the first N rows is easier than selecting a random number. Did you submit the idea to Microsoft?

-Jamie|||

jesal wrote:

yep, I have the same problem. I have a dataset containing a record for each bill in the past month and I want to flag the top 10 customers according to the aggregated amount pr customer.

Like this in plain SQL:

select top 10 CustName, sum(Amount) as sum_Amount into #ek_temp

from erhverv_kreditnota group by CustName order by sum_Amount

go

update erhverv_kreditnota

set top10 = 'Top 10'

where CustName in (select CustName from #ek_temp)

go

Any way to do this without scripting?

As explained, no, not really. Is there any reason why you don't want to do this in a script?

-Jamie|||I did!

http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=2bc4ee52-aec6-409d-b39c-6e2fb4945799|||Out of interest, would you want to send:
Rows 1-10 to output1
Rows 1-20 to output2
Rows 1-30 to output3

or

Rows 1-10 to output1
Rows 11-30 to output2
Rows 31-60 to output3

?

I might have a crack at this myself...it sounds quite interesting.

-Jamie|||

I would like the first scenario:

Rows 1-10 to output1
Rows 1-20 to output2
Rows 1-30 to output3

-

There are two reasons why I would like to do this without a script. First I don’t have that much experience with scripting. Second I think that a simple and common task like this should be available “off the shelves”.

I tried scripting based on Jamie’s answer to the thread “Script Transformation” (http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=60464).

1. My problem is that the number of output rows is different from the number of input rows – so I need to limit the script to run on the first 10-30 rows, not the entire recordset.

2. Jamie suggests the syntax <buffername>.AddRow() and Outputbuffer (in the “Script Transformation” thread). I used the default declaration (ByVal Row As Input0Buffer) – but as far as I can see this buffer contains both the input and output and I cannot find Row.AddRow?

Any suggestions to a script that redirects the first 10 rows from input to output?

- Jeppe

|||

jesal wrote:

I would like the first scenario:

Rows 1-10 to output1
Rows 1-20 to output2
Rows 1-30 to output3

-

There are two reasons why I would like to do this without a script. First I don’t have that much experience with scripting. Second I think that a simple and common task like this should be available “off the shelves”.

I tried scripting based on Jamie’s answer to the thread “Script Transformation” (http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=60464).

1. My problem is that the number of output rows is different from the number of input rows – so I need to limit the script to run on the first 10-30 rows, not the entire recordset.

2. Jamie suggests the syntax <buffername>.AddRow() and Outputbuffer (in the “Script Transformation” thread). I used the default declaration (ByVal Row As Input0Buffer) – but as far as I can see this buffer contains both the input and output and I cannot find Row.AddRow?

Any suggestions to a script that redirects the first 10 rows from input to output?

- Jeppe

Jeppe,
This should do it: http://blogs.conchango.com/jamiethomson/archive/2005/07/27/1877.aspx

-Jamie|||Thank you Jamie!

Your "SSIS Nugget: Select Top N in a data-flow" worked great. I have one question though - I couldnt get the "Output0Buffer" to work, but when I changed the SynchronousInputID for the Output to 0 it worked, what is the connection between the declaration of the Output0Buffer and the Synchronous setting?

Thansk again

- Jeppe

Sunday, March 11, 2012

Conditional Split Question

I have a package which has a conditional task which directs rows to its respective OLEDB command. The records are sorted from the source system in chronological order. The problem I am experiencing is that some of the operations do not seem to be occurring in the same order. An example of this would be someone inserts a record, deletes the record and reinserts in the record in that order. When we run the package we can see the records are coming down in chronological order but the delete from the split seems to occur after the inserts. Has anyone else experienced this? Is there anything I might be missing to ensure things happen in the order they should? Any advice would be greatly appreciated. Thank you.Can you provide more details surrounding your data flow setup? Try to illustrate to us how you have the data flow built from source all of the way through the destinations (or OLE DB Commands).|||

Bagles1 wrote:

I have a package which has a conditional task which directs rows to its respective OLEDB command. The records are sorted from the source system in chronological order. The problem I am experiencing is that some of the operations do not seem to be occurring in the same order. An example of this would be someone inserts a record, deletes the record and reinserts in the record in that order. When we run the package we can see the records are coming down in chronological order but the delete from the split seems to occur after the inserts. Has anyone else experienced this? Is there anything I might be missing to ensure things happen in the order they should? Any advice would be greatly appreciated. Thank you.

If you have 2 OLE DB Command tasks in the same dataflow you should not rely on the rows being actioned in the order that they enter the pipeline. There is no sychronisation between the two paths. Once the rows are in different paths then they are two seperate streams of data and will be teated as such. If you want to ensure that the deleted happen after the insertions push the data for deletioninto a raw file and issue the deletes from another data-flow.

-Jamie

|||

Jamie Thomson wrote:


If you have 2 OLE DB Command tasks in the same dataflow you should not rely on the rows being actioned in the order that they enter the pipeline. There is no sychronisation between the two paths. Once the rows are in different paths then they are two seperate streams of data and will be teated as such. If you want to ensure that the deleted happen after the insertions push the data for deletioninto a raw file and issue the deletes from another data-flow.

-Jamie

Yep, or create separate data flows, with precedence enforced at the control flow level.|||I have a data reader that pulls data from a staging environment whereas the data is sorted in chronological order. It immediately goes down into a conditional split where a field holds 1 of 3 values; I, U, D (Insert, Update, Delete). From there each condition has a RowCount transformation to count the rows as they pass through and then onto an OLEDB command that performs the necessary operation. The stream stops at the OLEDB command and that is all there is.|||Ugggghhhhh! That hurts.|||

Bagles1 wrote:

I have a data reader that pulls data from a staging environment whereas the data is sorted in chronological order. It immediately goes down into a conditional split where a field holds 1 of 3 values; I, U, D (Insert, Update, Delete). From there each condition has a RowCount transformation to count the rows as they pass through and then onto an OLEDB command that performs the necessary operation. The stream stops at the OLEDB command and that is all there is.

As I said above, there is no guarantee of the order in which rows get processed, especially when you send them to different data paths.

There is also no guarantee that data will actually enter the pipeline from the staging environment in the order that you think it does. There is no concept of a set of data being ordered - there are lot of things that can influence the order that rows appear in the pipeline.

-Jamie

|||

Jamie Thomson wrote:

Bagles1 wrote:

I have a data reader that pulls data from a staging environment whereas the data is sorted in chronological order. It immediately goes down into a conditional split where a field holds 1 of 3 values; I, U, D (Insert, Update, Delete). From there each condition has a RowCount transformation to count the rows as they pass through and then onto an OLEDB command that performs the necessary operation. The stream stops at the OLEDB command and that is all there is.

As I said above, there is no guarantee of the order in which rows get processed, especially when you send them to different data paths.

There is also no guarantee that data will actually enter the pipeline from the staging environment in the order that you think it does. There is no concept of a set of data being ordered - there are lot of things that can influence the order that rows appear in the pipeline.

-Jamie

Jamie,

Are you saying that if I have an Order By statement in my data reader that there is no guarantee that it will actually be in that order or have I misunderstood your statement?

|||

Bagles1 wrote:

Jamie Thomson wrote:

Bagles1 wrote:

I have a data reader that pulls data from a staging environment whereas the data is sorted in chronological order. It immediately goes down into a conditional split where a field holds 1 of 3 values; I, U, D (Insert, Update, Delete). From there each condition has a RowCount transformation to count the rows as they pass through and then onto an OLEDB command that performs the necessary operation. The stream stops at the OLEDB command and that is all there is.

As I said above, there is no guarantee of the order in which rows get processed, especially when you send them to different data paths.

There is also no guarantee that data will actually enter the pipeline from the staging environment in the order that you think it does. There is no concept of a set of data being ordered - there are lot of things that can influence the order that rows appear in the pipeline.

-Jamie

Jamie,

Are you saying that if I have an Order By statement in my data reader that there is no guarantee that it will actually be in that order or have I misunderstood your statement?

Well, YES, the order will be retained until some other downstream data flow component rearranges the order. Surely you can't expect a union all to maintain order, for instance.|||

Bagles1 wrote:

Jamie,

Are you saying that if I have an Order By statement in my data reader that there is no guarantee that it will actually be in that order or have I misunderstood your statement?

In that case then yes, it will enter the pipeline in the order decreed by the ORDER BYstatement but thereafter you should not rely on the ordering within the pipeline. And you should DEFINATELY not rely on rows reaching a destination/OLE DB Command in some order when those rows are in different data paths.

-Jamie

|||Thank you for your explanation. A very painful lesson to learn this late in the project. Unfortunately in our case we have to process the rows in chronological order so it looks like it will be the script component once again.|||

Bagles1 wrote:

Thank you for your explanation. A very painful lesson to learn this late in the project. Unfortunately in our case we have to process the rows in chronological order so it looks like it will be the script component once again.

If this is true then it sounds as though you have some sort of procedural logic going on - that's not really possible with the standard components. Script component may help though.

I don't actually know your requirement but wouldn't it just make more sense to count the number of inserts and deletes per "thing". If there are more inserts than deletes then you insert the "thing", otherwise you don't.

Good luck with it anyway.

-Jamie

Thursday, March 8, 2012

Conditional Order by?

Is there a way to do a conditional order by so that a user can give a parameter to a stored proc and it give back results sorted the way they want?

I want it so that the user can do 1 of 4 things,
* sort by "title" ascending,
* sort by "title" descending,
* sort by "synopsis" ascending,
* sort by "synopsis" descending

Can it be done? This is what I have but I get a syntax error:

select * from Blah

Order By
Case
when @.orderId = 1 then title asc
when @.orderId = 2 then title desc
when @.orderId = 3 then synopsis asc
when @.orderId = 4 then synopsis desc
end

Any help is greatly appreciated!

You need to split the query as ASC Query & Desc Query.

You can choose your order by columns (if it is single column) dynamically but Sorting Order you can't.

Use your query as follow as

if @.OrderId in (1,3)

select * from Blah
Order By
Case
when @.orderId = 1 then title
when @.orderId = 3 then synopsis
end asc

else

select * from Blah
Order By
Case
when @.orderId = 2 then title
when @.orderId = 4 then synopsis
end Desc

Or

You can use dynamic SQL

Declare @.SQL as NVarchar(1000)

Select @.SQL = N'select * from Blah

Order By ' +
Case
when @.orderId = 1 then 'title asc'
when @.orderId = 2 then 'title desc'
when @.orderId = 3 then 'synopsis asc'
when @.orderId = 4 then 'synopsis desc'
end

Exec (@.SQL)

|||Why don't you dynamically add the final order by clause to the select query string ?
I guess it is more clear to understand and much more flexible for any future changes.|||Thanks both for the replies, I didnt know it would be so detailed, I have a rather large select query that I am applying this to and I dont really want to create a string then execute it, it just looks messy to me. But I guess if I have no option I guess ill have to.

Thanks again|||In some cases I have done this:

SELECT ....,
SortOrder = Case when @.orderId = 1 then title
when @.orderId = 2 then REVERSE(TITLE)
when @.orderId = 3 then synopsis
when @.orderId = 4 then REVERSE(synopsis) END
ORDER BY SortOrder

The only issue with this is all the vars in the CASE must be the same type, or cast to a certain type.

|||

Easiest is to do below:

order by

case @.orderId when 1 then title end

, case @.orderId when 2 then title end desc

, case @.orderId when 3 then synopsis end

, case @.orderId when 4 then synopsis end desc

Note that you will get the best performance (assuming you have indexes on the column(s) and the plan can use it) if you use dynamic SQL to form the ORDER BY with required columns or use different SELECT statements. But in most cases, I have found that the above construct is easier to use, safe from SQL injection (dynamic SQL is prone to it if you are not careful) and with few conversions which single CASE expression requires.

|||Thanks all for the help with this topic, I think Umachandar's answer will fit my solution best.

Sunday, February 12, 2012

Concatentate sorted strings

Hi All,

I'm new to SQL Server.
I have the following table in a database of SQL Server 2005 Express.

ColA ColB
-- --
A 12
A 10
B 50
B 13

What I want to achieve is the following result :

Col A Aggr
--
A 2,12
B 13, 50

I tried the following query :

SELECT ColA, dbo.Concatenate(ColB) as Aggr
FROM
(SELECT TOP(100) PERCENT ColA, ColB
FROM TABLE_A
ORDER BY ColB) AS Derived_A
GROUP BY ColA

where Concatenate is the CLR User-defined aggreate function written in C#.

What I want to do is to first sort the rows on ColB and then to execute aggregate function over the sorted rows so as to get the above-mentioned results. However, what I got is the following

Col A Aggr

--

A 2,12

B 50,13 -- not sorted

I learnt from the SQL Server documentation that even though the ORDER BY clause is presented in the subquery, the query result is not guaranteed to be sorted. Only ORDER BY clause used in the outer query should work. So, how should this problem be solved? One way of which I'm thinking is to do the sorting in the aggregate function, but I'm worrying if this is harmful to the performance. Could anyone help me to solve the problem?

Thanks!

Regards,
Nathan

I think, it's dbo.Concatenate function which should perform this sorting.|||

You can't do this using SQLCLR UDFs right now. The ORDER BY in the derived table only applies to that scope. And there is no guarantee that rows/values will be sent in sorted order to the UDF within each group. You could do the sorted concatenation using a SQL technique like below:

select pt.ColA, pt.[1] + coalesce(',' + pt.[2], '') + coalesce(',' + pt.[3], '') as Aggr

from (

select ColA, ColB, ROW_NUMBER() OVER(PARTITION BY Col1A ORDER BY ColB) as seq

from tbl

) as t

pivot (min(ColB) for seq in ([1], [2], [3] /* as many items per group that you want to support */)) as pt