Can I create a random number in an SSIS derived column
-
01-10-2020 - |
Frage
I have a task where I am trying to assign a random PIN to an account. I have SQL code that will generate a random integer between 1000 and 9999. I was wondering if I could put that in a Derived Column task in SSIS and not do it in the source data?
Also, I can probably write a Script Task to accomplish this. At this point it's more an exercise to see if it's possible since I have alternatives.
Lösung
You don't actually have to alter your source data, but you can use a customised SQL command in your OLE DB Datasource
. Basically change the Data access mode from Table or view
to SQL command
or even SQL command from variable
and create your statement to include your PIN code, something like this:
SELECT *, 1 + ABS(CHECKSUM(NEWID())) % 9999 PIN
FROM dbo.ssisTest
No need for Derived Column, Script Task etc. Make sense?
Another approach would be to assign some kind of arbitrary row id to your source table and a similar id to your unique account ids table and join on that, something like this:
SELECT *
FROM
(
SELECT
ROW_NUMBER() OVER( ORDER BY ( SELECT NULL ) ) rowId,
*
FROM dbo.ssisTest
) x
INNER JOIN dbo.uniqueAccounts a ON x.rowId = a.rowId
Similar to the above approach, you can hold this SQL in your OLE DB Source without affecting your actual source data. In order for this to work you must a) persist your unique accounts and give them an arbitrary row id and b) there must be more unique accounts than there are original records, eg generate a few million up front.