3

I have the following script in a Execute SQL Task Editor. I have two parameters that I have mapped to the question marks. The query parses when I set @ClientCode and @PEK to something else. Why does my query not parse with the ? parameters? The full error is "The query failed to parse. Syntax error, permission violation, or other nonspecific error"

declare @ClientCode varchar(100)
declare @PEK int
set @ClientCode = ?
set @PEK = ?


if((@ClientCode != '') and (@ClientCode is not null))
begin
    exec        portal.GetClients @ClientCode
end

else if((@PEK != 0) and (@PEK != '' ))
begin

    select      distinct c.Id, c.Name, c.Code
    from        Features.map.ProfileAreasManagementAreas pama INNER JOIN
                ClientDW.dbo.ManagementAreas ma ON pama.ManagementAreaKey = ma.ManagementAreaKey INNER JOIN
                ClientDW.dbo.Clients c ON ma.ClientKey = c.ClientKey
    where       pama.PublishEventKey = @PEK
end

else 
begin  
    select      top 1 PublishEventKey
    from        Features.publish.PublishEvents
    order by    PublishDate  desc

end 

image of Parameter Mapping

2
  • I've found the parser and parameterisation of this task to be pretty lame. Perhaps start by setting the length if User::ClientCode to 100 rather than -1 and see if that helps. Also what kind of driver (OLE DB, ADO.Net) are you using? because they use different tokens and syntaxes
    – Nick.Mc
    Jun 20, 2016 at 23:22
  • I tried 100 for @ClientCode. I didn't work. It is an OLE DB connection Type. Jun 20, 2016 at 23:36

2 Answers 2

3

Something must be awry with how you have things configured or the code has been sanitized.

I built a reproduction package and have it configured thusly

enter image description here

Parameters make no difference whether I specify -1 lengths or 100 and 0 as one might expect for varchar(100) and int.

enter image description here

Runs successfully

enter image description here

The SQL I used was simplified to

declare @ClientCode varchar(100)
declare @PEK int
set @ClientCode = ?
set @PEK = ?

I find it helpful to distill problems down to their essentials. If this logic and parameter assignment work, then there's something wrong with the rest of the TSQL.

Since that all worked, I then modified your TSQL to be

declare @ClientCode varchar(100)
declare @PEK int
set @ClientCode = ?
set @PEK = ?


if((@ClientCode != '') and (@ClientCode is not null))
begin
    PRINT @ClientCode;
end
else if((@PEK != 0) and (@PEK != '' ))
begin
    PRINT @PEK;
end
else 
begin  
    PRINT CURRENT_TIMESTAMP;
end 

I tested with '' and 0 which printed current date and time. I then gave PEK a non-zero value and it echoed the non-zero value back. Finally, I gave the client code a non-empty string and it too was shown so the logic all seems to be in order.

Biml

I used the following Biml to generate a prototype package. You can use either the free tool BIDSHelper or Biml Express to take Biml files and make SSIS packages - it's very cool.

After installing either tool, right click on the SSIS project and select Add new Biml file. Copy and paste the following code into the BimlScript.biml file.

Edit the third line (OleDbConnection ) to point the ConnectionString's DataSource to a valid database server in your world.

Save.

Right click on the BimlScript.biml file and select Generate SSIS Packages.

Magic, you now have a replica of what works. Try using that to patch in your pieces and test against that.

<Biml xmlns="http://schemas.varigence.com/biml.xsd">
    <Connections>
        <OleDbConnection Name="tempdb" ConnectionString="Data Source=localhost\dev2014;Initial Catalog=tempdb;Provider=SQLNCLI11.0;Integrated Security=SSPI;"/>
    </Connections>
    <Packages>
        <Package Name="so_37932933">
            <Variables>
                <Variable DataType="Int32" Name="PublishEventKey">0</Variable>
                <Variable DataType="String" Name="ClientCode">mixed</Variable>
            </Variables>
            <Tasks>
                <ExecuteSQL ConnectionName="tempdb" Name="SQL Parameter test">
                    <DirectInput><![CDATA[declare @ClientCode varchar(100)
declare @PEK int
set @ClientCode = ?
set @PEK = ?]]></DirectInput>
                    <Parameters>
                        <Parameter DataType="AnsiString" VariableName="User.ClientCode" Name="0" Length="100" />
                        <Parameter DataType="Int32" VariableName="User.PublishEventKey" Name="1" />
                    </Parameters>
                </ExecuteSQL>
            </Tasks>
        </Package>
    </Packages>
</Biml>
2

The solution as I see it is as follows. When I was testing real figures I tries empty string for @ClientCode and it bypassed the exec portal.GetClients stored proc. It turns out I did not have execute pemissions on this. Secondly the BypassPrepare was set to false and for some reason when the ? parameters are in the query does not prepare the query with the parameters added. So when I set BypassPrepare to true again and did not worry with Parsing the query and just executed the Task it worked( with new execute permissions on the query). Thank you to those who tried to answer it. I think that this is the correct solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.