I am new to SSIS and a trying to schedule some automatic importing of text files. All the columns are interpreted properly using numerous examples of test data. However, my data does not have an identity column that can be used a primary key. I was hoping to add a column that will be an identity when the import occurs. Is this possible? Thanks ahead.

有帮助吗?

解决方案

I have done this by creating the database table ahead of time with the identity column and the remaining fields from the text file. Then, in the SSIS package map all text file data fields to the appropriate database fields. The database will handle the identity field when data is inserted into the remaining fields.

CREATE TABLE table_name
(
   Id [int] IDENTITY(1,1) NOT NULL,
   column_name2, -- From text file field 1
   ...
   column_name3 data_type(size), -- text file field n
);

Hope that helps.

其他提示

If you have an identity column in the target table of e. g. an OLE Destination in a data flow, SSIS handles this well by omitting the column from inserts, which is required by teh database engine which wants to manage this column itself, i e. generate new numbers for each record inserted.

If you mean to number all records imported e. g. by row number from 1 onwards, there is no data flow component in SSIS which supports this directly. But you could easily use a script component to the data flow which would add a column say row_no, and then use the following code (assuming C#) as the language setting:

int rowNo;

public override void PreExecute()
{
    base.PreExecute();
    rowNo = 0;
}

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    Row.rowno = ++rowNo;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top