SSIS error : [Execute SQL Task] Error: Executing the query - sql-server

I am creating a SSIS package and trying to extract data by calling stored procedures from one database and inserting the result set values into another table of different database. I have created a Execute SQL task to extract the data, a for each loop container to loop through the result set and Execute SQL task within the for loop container to insert the result set data into another database table. I am getting an the following error while inserting the records. I guess its the issue with the mapping.
[Execute SQL Task] Error: Executing the query "insert into EmployeeCount (companyId..." failed with the following error: "Parameter name is unrecognized.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Following the screenshot of the template design
Following is the edit window of execute sql task which in inside the foreach container
The insert statement
insert into EmployeeCount (companyId,dataItemName,dataItemvalue,fiscalYear,fiscalQuarter,PeriodTypeId) values(companyId,dataItemName,dataItemvalue,fiscalYear,fiscalQuarter,PeriodTypeId)

You will have to set "Parameter Name" in chronological order.
i.e companyID parameter must be 0, dataItemvalue to 1 ....PeriodTypeId to 5
Sample :

In the 'Parameter Mapping' for Parameter Name, instead of '?' give the values in a 0 based index i.e. 0,1,2,... till the end.

As an option:
You may be trying to insert too long values into shorter columns
For example if you create Source DB you need to be sure that each column have sufficient maximum length and you don't put largest values (too long values) into this column.
So unfortunately SSIS don't specify the problematic column, and you need to find it by yourself or enlarge maximum length of every column.

Simple Workaround
You can achieve this without using parameters, you can use expressions:
Double click on the Execute SQL Task, Go To Expressions.
add the following expression to SqlStatementSource:
"insert into EmployeeCount (companyId,dataItemName,dataItemvalue,fiscalYear,fiscalQuarter,PeriodTypeId)
values(" + (DT_WSTR,50)#[User::companyId] + ",'" + #[User::dataItemName] + "'," + (DT_WSTR,50)#[User::dataItemvalue] + "," + (DT_WSTR,50)#[User::fiscalYear] + "," + (DT_WSTR,50)#[User::fiscalQuarter] + "," + (DT_WSTR,50)#[User::PeriodTypeId] + ")"
Be aware! This approach makes your solution vulnerable to SQL injections

Related

Python3 pypyodbc SQL Server call stored procedure fails

The Python 3.8.1 code that I am working with makes several calls to stored procedures, gets results, does a simple select statement passing a string -- all successfully, using pypyodbc. My last call has me stumped as it simply fails to produce any results, or fails miserably. The stored procedure updates 1 column in 3 tables based on an OrderID, and inserts 1 row in another table. Using SSMS, the call would look like this:
exec GK_set_order '123456'
where 123456 is the OrderID. OrderID is a varchar(). I have logged into SSMS as the user in my Python connect statement and executed that stored procedure and it works every time.
In Python, the order number is used throughout as ADCOrderID, a string. My suspicion is that the single quotes are what is biting me. My first attempt was to create a separate string and execute that string.
exec_SP = "exec GK_set_order '" + ADCOrderID + "'"
cur.execute(exec_SP)
A print(exec_SP) statement shows that it is exactly what I want. But I get no errors, no exceptions, and nothing is updated in the database. I've tried 20 different variations, including double and triple quotes. Nothing was effective.
Changed to:
exec_SP = "exec GK_set_Order(?)"
cur.execute(exec_SP, ADCOrderID)
Now I get an error:
TypeError: Params must be in a list, tuple, or Row
What am I fundamentally missing?

Dynamically Create Views from parameters/variables SQL SSIS

I have a table that contains just under a million rows. I'm building a form using SSIS that asks for user input and uses the values as parameters to build a view from source data. I'm having trouble getting SSIS to create the view from a variable.
The purpose of this 'tool' is to provide a dialogue that programmatically builds a view and later an update statement based upon parameters defined via a form that will execute an SSIS package. A number of the ppl on my team know 0 SQL. Therefore this circumvents any SQL knowledge. Creating an entirely standalone app is not ideal as it would require too much additional overhead on my side and would deviate from a number of our existing processes that currently use SSIS/SQL to achieve similar results.
With that here is what I've tried/trying.
I have an SSIS package that contains 'Execute SQL Task'
This task brings up a form with 5 inputs (variables)
var1,var2,var3,var4,var5.
some vars are strings others are doubles, ints etc... (they all vary)
You populate the fields and hit okay.
These variables are passed to an 'Execute Package Task'.
Inside this package (Package B)
the vars are used in an 'Execute SQL Task'.
This task is attempting to take the users input and create a view with a where clause containing 4 other variables.
example:
Create View ? AS Select col1,col2,col3,col4 WHERE
col1 = ?
AND col2 =?
AND col3 =?.........
First it appears that using ? in the create view is invalid.
The error being:
Error: 0x0 at Build_Query: Incorrect syntax near '#P1'.
Error: 0xC002F210 at Build_Query, Execute SQL Task: Executing the query "CREATE VIEW ? as Select * from S_t_equip_template
..." failed with the following error: "The batch could not be analyzed because of compile errors.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established
correctly.
Task failed: Build_Query
If I use the create view variable as an expression and remove the variables/paramerters for the where statements, I can create the view no problem.
However the where statements throw errors once I add them back in. I've tried evaluating these as an expression in the 'Execute SQL Task' but as these are of various types I get the error:
[Execute SQL Task] Error: Executing the query "
CREATE VIEW testing AS SELECT
P.label,P.uniq..." failed with the following error: "The metadata could not
be determined because statement 'CREATE VIEW testingagain AS SELECT
P.label,P.uniqueid,C.label as Child_Label,C.uniqueid as Child_uni' does not
support metadata discovery.". Possible failure reasons: Problems with the
query, "ResultSet" property not set correctly, parameters not set correctly,
or connection not established correctly.
No idea what is going on. Any help would be appreciated
I've googled the error and found some info but the other use-cases are so different that it's difficult to understand the actual cause, or another work around.
AS requested (simplified example):
I've created a package variable (datatype string) called: View_Name
Execute SQL Task:
CREATE VIEW #[User::View_Name] AS SELECT
* from table1
where col1 = 100;
Specifically it does not like that I use a variable here.
If I set the View name everything works until: I move on to my Where clauses that contain variables.
Create a variable called type (datatype int)
I map the variable/parameter in my sql task
Example:
CREATE VIEW tempTable AS SELECT
* from table1
where col1 = ?;
This won't work, same error.
If i attempt to do the above via an expression or expressions I get the following error
[Execute SQL Task] Error: Executing the query "CREATE VIEW test_45678 AS SELECT P.label,P.uniquei..." failed with the following error: "Must declare the scalar variable "#".". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Generally having to due with the variables cannot be evaluated this way. I'm guessing I'd need to evaluate each piece individually and build the expression piece by piece. That's fine but very inefficient and not maintainable.
I had some fundamental misunderstanding in my attempt to evaluate my expressions. Specifically syntax issues and having to cast each variable to a string.
My SQLstatement variable
ON C.pid = P.ID
where
C.width >="+(DT_WSTR, 8)#[User::Width] +"-"+ (DT_WSTR, 8)#[User::Range]+........
The final expression looks like so:
"Create View "+#[User::View_Name] + " AS SELECT " + #[User::SQLStatement]

Issue loading-data-from-multiple-db-to-another-server-using-ssis

I am facing issue in loading-data-from-multiple-db-to-another-server-using-ssis
I have referred the below link
Loading data from multiple db to another server using SSIS
SSIS Package FLow :
Unfortunately i am getting the error in "Execute SQL Task" as below:
[Execute SQL Task] Error: There is an invalid number of result
bindings returned for the ResultSetType: "ResultSetType_Rowset".
Appreciate if you could help me with the solutions.
Thanks
Based on the comments, to solve your issue
1. Evaluate your SQL Statement. For instance :
2. Once your variable query is evaluating, move to Execute SQL Task. It should look like this:
3. Next the resultSet should look like this (object_variable is of object type)
Why are we not using anything in parameter mapping ?
Answer: If we had a SQL query like Select col1, col2 from table1 where col3 = ?, Then we would be replacing ? with either a parameter or a variable.
In your case, delete everything inside parameter mapping.
Updated : Also, since you're query is Select * into tbl2 from tbl1, ResultSet property should be None instead of any other thing.
You have 2 Execute SQL Tasks
1, First Execute SQL Task, get list of tables and schema, it expects a Full Result set and map it to a object type variable.
2, Foreach loop container, ADO Enumerator, ADO source is the object type variable. Variable Mappings to 2 string type variables, 1 is for table name and 1 is for schema name.
Second Execute SQL Task, it has no Full Result set, no parameter mapping. Because table name/schema name changes are taken care of by Foreach loop container.

SSIS - SQL Command - Parameters

I edited the question based on the solution that Hadi gave.
I am using SSIS in VS 2013.
I have a user variable called MyVariableList and Query.
This is Expression in user variable Query: "SELECT cola, colB FROM myTable WHERE myID IN (" + #[User::MyVariableList] + ")"
I have a Script Task that set the value of #[User::MyVariableList].
Dts.Variables["User::MyVariableList"].Value = sList;
After that, I have A Data Flow Task with OLE DB Source (from 1 database) to another OLE DB Destination (another database on another server).
In the OLE DB Source Editor, I set
Data access mode: SQL Command from variable
Variable name: User:: Query
In the OLE DB Source connection, I have set the DelayValidation to True
Before I even can run the package, I am getting this error
How can I fix this issue ? Thank you
First of all, you are working with a project parameter not a project variable
You cannot achieve this using a parameterized sql command, you have to build the whole query inside a variable, and use it as source (SQL Command from variable)
Parameters are used to specify a single value, not concatenating the query string
Create a SSIS variable (User::Query), change the variable Evaluate As Expression property to True and write the expression in the variable Expression property. Like the following
"SELECT cola, colB FROM myTable WHERE myID IN (" + #[$Project::MyProjectVariable] + ")"
Note: to use a project parameter inside an expression use the following syntax : #[$Project::MyProjectVariable]

SSIS Execute SQL Task Stored Procedure returning empty result sets

I have a simple stored procedure that returns 1 row with 3 columns. I am trying to create an SSIS package that reuses the values in these columns later. If I run a simple select query everything is happy but when I run the stored procedure I get the following error:
[Execute SQL Task] Error: Executing the query "rs_UpdateMemberExtract"
failed with the following error: "Unable to populate result columns
for single row result type. The query returned an empty result set.".
Possible failure reasons: Problems with the query, "ResultSet"
property not set correctly, parameters not set correctly, or
connection not established correctly.
I've set up an ado.net connection and the proc runs correctly in SSMS, I have the task set up like follows:
ConnectionType: ADO.NET
Connection: ServerName.connectionName
SQLSourceType: Direct Input
SQLstatment: Name of Proc (rs_UpdateMemberExtract)
IsQueryStoredProcedure: True
I have each parameter mapped to a variable and the direction set to Output
I have the name of each column that should be returned in the result set and mapped to the associated variable.
The task works if I set the ResultSet to 'None',
Any ideas What I've missed?
Thanks
If an SQL Task Editor expects results returned in a row … but there are no records to return then the following SQL fills a null return with a space. Note : can also be used to return zero if a numeric “return row” is expected
SELECT '' + ISNULL ((SELECT x FROM y WHERE (z = ?)), '') AS myfield

Resources