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 comm开发者_如何学Goand. 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.
精彩评论