Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SSIS sorts the data

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

There are two ways for SSIS to sort data, one is to use the Sort component, and the other is to sort using sql command's order by clause.

First, use the Sort component to sort

SortType: ascending ascending, descending descending

SortOrder: the position of the sequence, increasing gradually from 1 to 1

Remove wors with duplicate sort values: if the sequence is repeated, whether to delete the duplicate row or not, unlike distinct,distinct, where all output columns are not duplicated, check this option only to ensure that the sort (part of the output column) is not duplicated.

This property can be viewed and set from Sort Transformation Advanced Editor

Second, use the order by clause of sql command to sort the data

Step1, which uses OLEDB to provide sorted data, must be sorted data

Select * from dbo.course c with (nolock) order by c.cid asc,c.score desc

Step2, open the Advanced Editor of OLEDB and view the Input and Output Properties tab

1, click OLEDB Source Ouput, and set the IsSorted property to True. Setting this property to true will not sort the data, but will only tell the downstream components that the output data has been sorted.

If the IsSorted property is set to True, the actual data is not sorted and will cause errors when package is running, so you must provide the sorted data (sort using order by in the sql clause)

2. Click Output Columns, and set the SortKeyPosition attribute of Order by Column_List one by one

The SortKeyPosition attribute has two metadata, Sort Position and Direction:

Positive integers are sorted in ascending order, 0 is not a sorted column, negative integers are sorted in descending order, and numbers represent the serial number of the sorted sequence.

For example, the following sql statement

Select Col_1,Col_2,Col_3,Col_4from dbo.TableNameorder Col_1 asc, Col_2 desc,Col_3 desc

In Output Columns, you need to set the SortKeyPosition of Col_1,Col_2,Col_3,Col_4 one by one.

Since Col_1,Col_2,Col_3 is a sorted column, the sequence number is incremented from 1, and Col_4 is not a sorted column, the configuration of SortKeyPosition is as follows

The SortKeyPosition of Col_1 is 1, the first sort, and sort in ascending order

The SortKeyPosition of Col_2 is-2, the second sort, and sort in descending order

The SortKeyPosition of Col_3 is 3, the third sort, and sort in ascending order

The SortKeyPosition of Col_4 is 0, not a sorted column

MSDN official documentation

Sort Data for the Merge and Merge Join Transformations

In Integration Services, the Merge and Merge Join transformations require sorted data for their inputs. The input data must be sorted physically, and sort options must be set on the outputs and the output columns in the source or in the upstream transformation. If the sort options indicate that the data is sorted, but the data is not actually sorted, the results of the merge or merge join operation are unpredictable.

You can sort this data by using one of the following methods:

In the source, use an ORDER BY clause in the statement that is used to load the data.

In the data flow, insert a Sort transformation before the Merge or Merge Join transformation.

If the data is string data, both the Merge and Merge Join transformations expect the string values to have been sorted by using Windows collation. To provide string values to the Merge and Merge Join transformations that are sorted by using Windows collation, use the following procedure.

To provide string values that are sorted by using Windows collation

Use a Sort transformation to sort the data.

The Sort transformation uses Windows collation to sort string values.

-or-

Use the Transact-SQL CAST operator to first cast varchar values to nvarchar values, and then use the Transact-SQL ORDER BY clause to sort the data.

Important

You cannot use the ORDER BY clause alone because the ORDER BY clause uses a SQL Server collation to sort string values. The use of the SQL Server collation might result in a different sort order than Windows collation, which can cause the Merge or Merge Join transformation to produce unexpected results.

Setting Sort Options on the Data

There are two important sort properties that must be set for the source or upstream transformation that supplies data to the Merge and Merge Join transformations:

The IsSorted property of the output that indicates whether the data has been sorted. This property must be set to True.

Important

Setting the value of the IsSorted property to True does not sort the data. This property only provides a hint to downstream components that the data has been previously sorted.

The SortKeyPosition property of output columns that indicates whether a column is sorted, the column's sort order, and the sequence in which multiple columns are sorted. This property must be set for each column of sorted data.

If you use a Sort transformation to sort the data, the Sort transformation sets both of these properties as required by the Merge or Merge Join transformation. That is, the Sort transformation sets the IsSorted property of its output to True, and sets the SortKeyPosition properties of its output columns.

However, if you do not use a Sort transformation to sort the data, you must set these sort properties manually on the source or the upstream transformation. To manually set the sort properties on the source or upstream transformation, use the following procedure.

To manually set sort attributes on a source or transformation component

In SQL Server Data Tools (SSDT), open the Integration Services project that contains the package you want.

In Solution Explorer, double-click the package to open it.

On the Data Flow tab, locate the appropriate source or upstream transformation, or drag it from the Toolbox to the design surface.

Right-click the component and click Show Advanced Editor.

Click the Input and Output Properties tab.

Click Output, and set the IsSorted property to True.

Note

If you manually set the IsSorted property of the output to True and the data is not sorted, there might be missing data or bad data comparisons in the downstream Merge or Merge Join transformation when you run the package.

Expand Output Columns.

Click the column that you want to indicate is sorted and set its SortKeyPosition property to a nonzero integer value by following these guidelines:

As an example of how to set the SortKeyPosition property, consider the following Transact-SQL statement that loads data in a source:

SELECT * FROM MyTable ORDER BY ColumnA, ColumnB DESC, ColumnC

For this statement, you would set the SortKeyPosition property for each column as follows:

Set the SortKeyPosition property of ColumnA to 1. This indicates that ColumnA is the first column to be sorted and is sorted in ascending order.

Set the SortKeyPosition property of ColumnB to-2. This indicates that ColumnB is the second column to be sorted and is sorted in descending order

Set the SortKeyPosition property of ColumnC to 3. This indicates that ColumnC is the third column to be sorted and is sorted in ascending order.

The integer value must represent a numeric sequence, starting with 1 and incremented by 1.

A positive integer value indicates an ascending sort order.

A negative integer value indicates a descending sort order. If set to a negative number, the absolute value of the number determines the column's position in the sort sequence.

The default value of 0 indicates that the column is not sorted. Leave the value of 0 for output columns that do not participate in the sort.

Repeat step 8 for each sorted column.Click OK.To save the updated package, click Save Selected Items on the File menu.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report