locked
SQL Query- No characters are allowed after a here-string header but before the end of the line. RRS feed

  • Question

  • I was testing out this powershell script and i cannot understand why is there a @ before the SQL query ?

    At line:4 char:15
    + $SqlQuery = @'Select top 10 * from database.dbo.table'@;
    +               ~
    No characters are allowed after a here-string header but before the end of the line.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedCharactersAfterHereStringHeader

    $SQLServer = 'server';
    $Database = 'database';
    $SqlQuery = @'Select top 10 * from database.dbo.table'@;

    Monday, November 19, 2018 6:22 AM

All replies

  • Remove the @.  They are not useful here.

    I suggest taking a bit of time to learn PowerShell before you try copying bad code you see on the Internet.

    HELP about_Quoting_Rules

    Microsoft Virtual Academy - Getting Started with Microsoft PowerShell


    \_(ツ)_/

    Monday, November 19, 2018 6:28 AM

  • Surrounding quotations with @ makes them a here-string.

    The @" "@ parts have to be in their own lines.

    $text = @"
    here strings
    can span multiple
    lines.
    "@

    You don't need a here-string if you are putting your query in a single line.

    Monday, November 19, 2018 6:40 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    LEE


    Just do it.

    Tuesday, November 27, 2018 2:46 AM