• You cannot use the variable like that directly. (e.g. placing a ? instead of a servername is not an option).

    You need to use dynamic SQL. Create a string that contains your SQL statement with a placeholder for the servername.

    Create a variable and map this variable to your parameter (using the question mark).

    Then, use the REPLACE function on the string to replace the placeholder with the variable value. You'll have your final SQL statement stored in that string variable.

    Execute this SQL statement with EXEC or with sp_executesql.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP