简体   繁体   中英

SSIS Execute SQL Task Editor query failed to parse

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 

参数映射的图像

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

在此处输入图片说明

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

在此处输入图片说明

Runs successfully

在此处输入图片说明

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM