How to avoid compile time syntax error

  • I am preparing a common script to execute in many versions of sql.

    I am checking the versions and executing based on that.

    For example 'decrypt' function is not working in 2008.

    If version is 2008, I am not executing the decrypt function. But I am getting the below error.

    Msg 102, Level 15, State 1, Line 5

    Incorrect syntax near 'decrypt'.

    The sample script is :

    --------------------------------------------------------------

    if @version != 2008

    begin

    exec(decrypt(0x0D000A006300720065006100740065002000700072006F0063002000700031000D000A00610073000D000A00730065006C0065006300740020002A002000660072006F006D00200065006D0070006C006F007900650065000D000A00

    ))

    end

    ---------------------------------------------------------------

    I tried dynamic sql. That also not working.

    If anybody have solution for that please help me...

    Thank you in Advance.

  • I'm not familiar with the keyword "decrypt". I'm guessing it's something that works in

    much older versions of SQL Server.

    Even if that fragment of code is not being executed, it still has to be parsed and compiled by SQL Server.

    So if SQL Server 2008 doesn't support that keyword, then it will generate a syntax error.

    May I suggest that using cast(0x#### as nvarchar) will do what you need.

    select cast ( 0x0D000A006300720065006100740065002000700072006F0063002000700031000D000A00610073000D000A00730065006C0065006300740020002A002000660072006F006D00200065006D0070006C006F007900650065000D000A00

    as nvarchar)

    If that's not supported in the older version of SQL Server you are using,

    and you need something that will run under both versions, then dynamic SQL would be your only choice.

    Can you give us more information about which version of SQL Server you were using before?

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply