Windows Vista Forums

New User Q: using contents of a file as sql command

  1. #1


    sylan Guest

    New User Q: using contents of a file as sql command

    Hi,
    I'm trying to read in the contents of a .sql file and use that as the query
    for my script but not having much luck in a ps1 script. Can anyone give me
    pointers?

    $cmdtext = Get-Content (".\" + "sqlquery.sql")
    cmd.commandtext = $cmdtext

    Also, any pointers to beginner level PS scripting references would be great.



      My System SpecsSystem Spec

  2. #2


    Leo Tohill Guest

    RE: New User Q: using contents of a file as sql command

    Are you using SqlConnection and SqlCommand objects, etc, to execute sql? Or
    do you want to execute sqlcmd.exe with that sql text? I'm not sure of the
    specifics of your question.

    regarding
    $cmdtext = Get-Content (".\" + "sqlquery.sql")
    if your sql is on multiple lines, you'd need to combine the lines (which are
    represented in the $cmdtext array) before passing it to the sql processor.
    Or, you could use:

    $cmdtext = [System.IO.File];:ReadAllText(filename)

    in which case $cmdText will be a scalar, not an array.






    "sylan" wrote:

    > Hi,
    > I'm trying to read in the contents of a .sql file and use that as the query
    > for my script but not having much luck in a ps1 script. Can anyone give me
    > pointers?
    >
    > $cmdtext = Get-Content (".\" + "sqlquery.sql")
    > cmd.commandtext = $cmdtext
    >
    > Also, any pointers to beginner level PS scripting references would be great.

      My System SpecsSystem Spec

  3. #3


    RichS [MVP] Guest

    RE: New User Q: using contents of a file as sql command

    Can you post the sql that is in the file
    --
    Richard Siddaway
    All scripts are supplied "as is" and with no warranty
    PowerShell MVP
    Blog: http://richardsiddaway.spaces.live.com/
    PowerShell User Group: http://www.get-psuguk.org.uk


    "Leo Tohill" wrote:

    > Are you using SqlConnection and SqlCommand objects, etc, to execute sql? Or
    > do you want to execute sqlcmd.exe with that sql text? I'm not sure of the
    > specifics of your question.
    >
    > regarding
    > $cmdtext = Get-Content (".\" + "sqlquery.sql")
    > if your sql is on multiple lines, you'd need to combine the lines (which are
    > represented in the $cmdtext array) before passing it to the sql processor.
    > Or, you could use:
    >
    > $cmdtext = [System.IO.File];:ReadAllText(filename)
    >
    > in which case $cmdText will be a scalar, not an array.
    >
    >
    >
    >
    >
    >
    > "sylan" wrote:
    >

    > > Hi,
    > > I'm trying to read in the contents of a .sql file and use that as the query
    > > for my script but not having much luck in a ps1 script. Can anyone give me
    > > pointers?
    > >
    > > $cmdtext = Get-Content (".\" + "sqlquery.sql")
    > > cmd.commandtext = $cmdtext
    > >
    > > Also, any pointers to beginner level PS scripting references would be great.

      My System SpecsSystem Spec

New User Q: using contents of a file as sql command

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using contents of an object as input to a command Gomez Addams PowerShell 0 13 Oct 2009
file icons dont match contents-file names hidden Fosbury Vista file management 0 07 May 2008
Contents of ZIP file blocked? Gerry Hickman Vista security 16 31 Oct 2007
Fast User Switching via Command Line or Batch File? Woody45 Vista General 1 17 Mar 2007
Fast User Switching via Command Line or Batch File? Woody45 Vista account administration 0 27 Feb 2007