locked
Powershell SQL Query - Read variable name RRS feed

  • Question

  • Hi,

    I have a script that connects to SQL and populates variables based on the values in SQL.  The issue I am having is that I would like to store a variable name as a value in SQL, so when it is processed by PowerShell it utilizes the variable.

    For example,

    In the powershell script there is a variable defined $test = "Variable"

    I have a variable that stores a SQL query called $Subject

    I have a column in SQL called Subject, the value for the line that I am processing is "This is a test $test"

    When I run my powershell query that queries SQL, and then run write-host $subject, the result I see is:

    This is a test $test

    What I am expecting to see is "This is a test Variable"

    What can I do to get the result I require?


    Thanks in Advance.



    Thursday, October 24, 2019 12:05 AM

All replies

  • You cannot store variables in SQLServer. Yo can only store values in a table. You can use variables in a query an d you can pass variables to a query to use while executing the query.

    The place to start is by learning SQL; what it is and how it works.

    https://docs.microsoft.com/en-us/sql/t-sql/language-reference?view=sql-server-ver15

    https://www.tsql.info/

    help Invoke-SqlCmd  -ShowWindow


    \_(ツ)_/

    Thursday, October 24, 2019 12:11 AM
  • Thank you, I know how to query the SQL and return values and store them in variables.

    I was hoping to build a powershell variable, that references another Powershell variable, which was pulled from the database.  

    In powershell if I have a variable defined as $test = "variable", I can then specify a variable $new = "this is a $test"  

    if I then write-host $new, it will display "this is a variable"


    Essentially I was trying to do the same, with the value from the SQL table (the value being "This is a $test").

    I get it to return the line, though when powershell processes the line, it sees $test as part of the string, rather than the variable.  I was hoping there was something I could do in powershell to indicate the the $test is a variable and not part of the string.

    Thursday, October 24, 2019 1:47 AM
  • So far your question is ambiguous because you have described to dissimilar requests. Also the concept of variables within variables in lines.

    If a string contains a some text that looks like $text then it is just text and not a variable.  If you are asking how to get the text that looks like a variable to be replaced by the value of a local variable - you can't by any normal methods.  You can use string operations too replace text in a string.  You can also use a SQL variable that can be executed in your query that can return the value you passed to the query.  That is why I posted the learning material for T-SQL.

    Without some idea of the code you are trying to use there is really no good way to answer your question or to even understand the question.


    \_(ツ)_/

    Thursday, October 24, 2019 2:08 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

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, November 8, 2019 1:17 PM