SSIS execute sql commands stored in a column of a table - sql

Error: error occurred while extracting the result into a variable of type (DBTYPE_WSTR). Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Using SSIS I have placed in a sql column commands that will convert a pdf to a blob they have been tested and work individually. So I would like to query the table column pass it to a exec sql task and execute each command. I have tried many different things currently I have a execute sql task resultset being set to "full result set" in the ResultSet the name is set to 0 and variable is User::sqlcmds this goes into a foreach loop container with collection tab setting of ADO object source variable = User::sqlcmds and Enumeration mode as Rows in the first table. Variable Mappings tab has Variable = User::sqlcmdenu and Index set to 0. Within the foreach container I have a execute sql task and the sqlstatement set to:
declare #sql nvarchar(max)
set #sql= CONVERT(NVARCHAR(4000),' ' + ?)
exec sp_executesql #sql
Parameter mapping: Variable name=user::sqlcmds, Direction: Input, Datatype:NVARCHAR, parameter name inputsqlcmd.
what am I doing wrong?
Tried using data flow, now trying execute sql and foreach. neither works.
Error: error occurred while extracting the result into a variable of type (DBTYPE_WSTR). Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

Got it to work by going back to dataflow task:
oledb source has-> select convert(varchar(4000),cmd) cmd from attcmd
then used derived column to convert -> (DT_WSTR,4000)cmd (this solves the non Unicode to Unicode problems).
this is input into oledb command with component properties tab set to ->
declare #sql nvarchar(max)
set #sql= CONVERT(NVARCHAR(4000),?)
exec sp_executesql #sql WITH RESULT SETS NONE
be sure to set column mapping of the input column to Param_0!
Data type of input column is DT_WSTR and Param_0 data type is Unicode string [DT_WSTR].

Related

Using foreach loop container to create new columns in a table- can't solve "incorrect syntax near '#P1'" error

I'm trying to use a foreach loop container to take row values and make them into fields. But for some reason I can't get it to work without running into this error:
alter table /databasename/.dbo.cp_hh_foo..." failed with the following error: "Incorrect syntax near '#P1'.
The problem appears to be in the final execute SQL statement. The first two execute SQL statements work fine. I think I've made a mistake in my variable/parameter mappings, but I'm not sure.
My data flow looks like this:
Basically what's going on is this:
First Execute SQL Task creates the new table
Second Execute SQL Task selects a table with full result set going into an object-type variable "AllocItems"
Foreach Loop container (configured as an ADO enumerator) maps specific row from "AllocItems" onto variable "AllocItemsSQL1". These are the strings which should become field names in the table I'm creating
Execute SQL Task within foreach loop container alters the table. The SQL query: alter table MIT_Client_Profitability.dbo.cp_hh_footprint add ? varchar(255)
Things I've tried:
within the final execute sql task, adding parentheses around the parameter: "(?)" instead of "?"
within final execute sql task, changing parameter name to "Param1"
within final execute sql task, changing parameter size
within second execute sql task, changing "result name"
within final execute sql task, changing query to "declare #SQL varchar(255) set #SQL = 'alter table MIT_Client_Profitability.dbo.cp_hh_footprint add ? varchar(255)' exec(#SQL)"
Thanks in advance for any suggestions!
To build on David's answer
Create a new SSIS variable, #[User::Sql] of type String and in the Expression box, add the following syntax
"alter table MIT_Client_Profitability.dbo.cp_hh_footprint add " + #[User::AllocItemSQL1] + " varchar(255);"
The nice thing about this approach is that you can put a breakpoint on the Execute SQL Task and see what the statement looks like prior to the task attempting to execute it. And then modify the Execute SQL Task to use the new variable and remove the parameter.
Otherwise, the dynamic tsql approach ought to have worked, just you needed to modify the syntax. The token replacement won't work inside the string. Something more like this should work according to my mental model
declare #newcolumn varchar(255) = ?;
declare #SQL varchar(255) = 'alter table MIT_Client_Profitability.dbo.cp_hh_footprint add ' + #newcolumn + ' varchar(255)';
exec(#SQL);
This
alter table MIT_Client_Profitability.dbo.cp_hh_footprint add ? varchar(255)
is a Data Definition Language (DDL) statement. DDL cannot be paramterized. You'll have to create the statement with string concatenation.

SSIS 2017 "Execute SQL Task" fails Could not find stored procedure

I'am using SSIS with an ADO.NET connection to Azure. Within SSIS I want to execute a procedure with one input and 2 output parameters. The input parameter is a static value. The Procedure works with T-SQL within SSMS.
I setup the "Execute SQL Task" as followed
General
SQLStatement: METRICE_VAULT.GP_1001_GENERIC_PRE_PROCESS 2, #INSTANCE, #PROCESS_STATUS
IsQueryStoredProcedure: True
ConnectionType: ADO.NET
ResultSet: None
SQLSourceType: Direct input
Parameter Mapping
When I execute the "Execute SQL Task" I received the following error
Must declare the scalar variable "#". Possible failure reason: Problem with the query, "ResultSet" property not set correctly..
Update
After adding parameters name i am receiving the following error:
Execute SQL Task] Error: Executing the query "METRICE_VAULT.GP_1001_GENERIC_PRE_PROCESS 2, #INS..." failed with the following error: "Could not find stored procedure 'METRICE_VAULT.GP_1001_GENERIC_PRE_PROCESS 2, #INSTANCE ,#PROCESS_STATUS'.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
Below is the way to configure your parameters and properties. Set IsQueryStoredProcedure property to True and instead of writing exec ProcName you just have to give ProcName in sqlstatment if you are using ADO.NET Connection.
Change the direction of your parameters to INPUT.
Since you are using ADO.NET You must specify the parameter name. As example:
METRICE_VAULT.GP_1001_GENERIC_PRE_PROCESS 2,#param1, #param2
And don't use parameter index in parameter mapping tab as shown in the screenshot.
Additional Information
You can refer to the following official documentation for more details and examples:
Map Query Parameters to Variables in an Execute SQL Task
Update 1
Try using a fully qualified name:
<database name>.<schema name>.<stored procedure name>
Or add a USE <database> command before the stored procedure execution.

Using a variable to create a variable

I have an execute SQL task that finds the path of the latest backup of a database and populates a variable with it (User::BackupFilePath)
I want to pass that into another task that will generate a restore database script and populate another variable to be used to restore the database.
Select (
'ALTER DATABASE [Database] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE [Database]
FROM DISK = ''' + **BackupFilePath** + ''' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5
ALTER DATABASE [Database] SET MULTI_USER
GO'
) as RestoreScript
The second part that would generate the string is however returning this error message
[Execute SQL Task] Error: Executing the query "Select 'ALTER DATABASE [xxxx..." failed with the following error: "An error occurred while extracting the result into a variable of type (DBTYPE_I4)". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
I'm using Visual Studio 2008 Professional Edition
So far, it looks like you're having a simple problem: the variable you're setting, to your command string, needs to be a string datatype.
Your error message mentions DBTYPE_I4, which is a long integer:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms711251%28v=vs.85%29.aspx
Whereas what you'd save a command string into, would be a string type such as DBTYPE_STR or DBTYPE_WSTR (see the link above), which in SSIS would commonly be called DT_STR (for ASCII strings) or DT_WSTR (for Unicode strings) -- see the link below:
http://technet.microsoft.com/en-us/library/ms141036.aspx
Hope that helps...
One possible cause is the property 'ResulSet' of your second SQL Task (the restore)/
Make sure it's set to 'None'

Execute SQL Task on SSIS 2005. ADO.NET connection type and Stored Procedure Input Parameter. Not working. Syntax error

I am Execute SQL Task on SSIS 2005. ADO.NET connection type and Stored Procedure Input Parameter. Not working. Syntax error
I read some documentation and blogs but its not working for me. This is what I have on SQL Statement of the Exeute sql task (ADO.NET)
EXEC StoredProcedureName #ParameterName =
It gives different errors on different syntax I tried. All failture
Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
I tried all combination like EXEC StoredProcedureName #ParameterName = ?
StoredProcedureName ?
StoredProcedureName #ParameterName = ?
etc etc in vain
I checked the parameter type and its fine
The same setup works if I change the connection type to OLE DB. But I don't want OLE DB and want to change it to ADO.NET
I tried as per Microsoft specification but that also didn't work
per Micrtosoft
If IsQueryStoredProcedure is set to False, EXEC uspGetBillOfMaterials #StartProductID, #CheckDate
I tried changing all combinations on IsQueryStoredProcedure property also in vain
I mapped parameter name,order and variable on Parameter mappings properly still no luck
as i said it works with OLEDB but does not work with ADO.NET even after changing syntax.
Please help
I have another task on ADO.NET and it works fine because it has no input parameter
Old question but in case it helps someone :
Set IsQueryStoredProcedure to True
Write ONLY the name of the stored procedure in the SQLStatement : sp_MyStoredProcedure
In the "Mapping" tab, add your parameter :
Direction = input ; Name = #parameterName
Try setting the IsQueryStoredProcedure to False and then setting the SqlStatement to
Execute MyStoredProcedure #MyParameter
Where #MyParameter is the Parameter Name in the Parameter Mapping section of the task, which references a variable in your package. This variable holds the value that you want to pass to the stored procedure.
When using an ADO.NET connection parameters should use the '#' notation, when using OLE DB you should use the '?' notation.
ADO.NET. Execute MyStoredProcedure #MyParameter
OLE DB. Execute MyStoredProcedure ?
Remember though that when using OLE DB, you should reference the Parameter Names (in Parameter Mapping) using 0,1,2,3 etc

SSIS: Passing parameter in OLE DB Source Editor to a table-valued function

How can I pass parameters in an OLE DB Source to call a table-valued function like:
select * from [dbo].[udf_test](?, ?)
When doing so, I am getting the error:
Parameters cannot be extracted from the SQL command. The provider might not help to parse parameter information from the command. In that case, use the "SQL command form variable" access mode, in which the entire SQL command is stored in a variable.
Syntax error, permission violation, or other nonspecific error (Microsoft SQL Native Client)
Is there another solution than "SQL command from variable"?
Sven
You can use the SQL Command from Variable, it is relatively straightforward. You could build the SQL statement to pass to the Source using a Script transform to edit the variable which holds the SQL. So for example you'd set up a variable called strSQL and make the value
select * from [dbo].[udf_test](?1, ?2)
And then in the script component, replace the ?1 and ?2 values with the parameters you want to pass using string manipulation methods.
It's a little more work in the package but works well in my experience.
This is a little bit of a workaround but it works pretty well. What we do is create a stored procedure called SetVar that will take an input parameter and assign it to a declared out parameter.
Create a procedure called SetVar in the OLE DB source database. It assigns #inputVar to #outVar
CREATE PROC SetVarInt
#inputVar INT ,
#outVar INT OUTPUT
AS
BEGIN
SELECT #outVar = #inputVar;
END
In the SSIS OLE DB source use this SQL:
DECLARE #param1 INT;
EXEC [dbo].[SetVarInt] #inputVar = ?, -- int
#outVar = #param1 OUTPUT -- int
SELECT * FROM [dbo].[GetByKey](#param1)
If nothing happens, the optimizer may have optimized the task away (check package execution log). In this case I will add the following to the end of the OLE DB source SQL, and connect this output to a RowCount task into a dummy variable.
SELECT 1 Done;
If you want to keep it simple, and just have the query in the OLDDB source without using other tricks, you can achieve that by writing the entire T-SQL code needed to get it done e.g.
declare #var1 as int = ?;
declare #var2 as datetime = ?;
select *
from [dbo].[udf_test](#var1, #var2);
Doing it like that you do not need to have a separate task in SSIS to build the query and assign the parameteres, it all stays in the same place, and therefore easier to write, and even more important easier to understand by the person who will need to support this after you.