Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign String variable in SSIS

Tags:

sql

ssis

I have a execute sql task in SSIS that I try to store a string value

this is the query

Declare @IdList nvarchar(max)
select @IdList = coalesce( @IdList + ''',''', '') + ID from table
Select @IdList = '''' + @IdList 
Select @IdList

The result looks like

'abc','bcd','def','fds'

and I try to store this value into String variable in SSIS

result set: single row
result name: 0 
Variable Name: String_Contact

this is the error I got

[Execute SQL Task] Error: The value type (__ComObject) can only be converted to variables of type Object.
[Execute SQL Task] Error: An error occurred while assigning a value to variable "String_Contact": "The type of the value (DBNull) being assigned to variable "User::String_Contact" differs from the current variable type (String). Variables may not change type during execution. Variable types are strict, except for variables of type Object.

".

Anyone know what's the issue?

thanks

like image 237
user3399111 Avatar asked Jan 11 '23 08:01

user3399111


1 Answers

Try to change the data type of the variable from String to Object.

If you assign varchar(max) to a string [SSIS] variable. there might be chance of overflow error since strings in SSIS are limited to 8000 chars in length.

But you can assign nvarchar(max) to Object.

You can process object by using For Each Loop Container setting Enumerator as "For Each ADO Enumerator" or by Script Task as

 DataTable dt = new DataTable();
OleDbDataAdapter oleDa = new OleDbDataAdapter();
oleDa.Fill(dt, Dts.Variables["User::variable"].Value);

use dt as regular DataTable.

Resources:

How To Read Object Type Variable In Script Task

Using the SSIS Object Variable as a Result Set Enumerator

Execute SQL Task into an object variable - Shred it with a Foreach loop

Mapping Full Resultset with Execute SQL Task to Object Variable

To use variable in SQLStatement property follow this thread.

like image 122
Vikramsinh Shinde Avatar answered Jan 12 '23 22:01

Vikramsinh Shinde