SQL Powershell Querie that can be executed by windows schueduler

I am trying to create a PowerShell SQL query for example below :

USE users_db
Update users set name = 2 where category = '5'
Go

I then somehow would like be able to use Microsoft scheduler to execute this command / '.bat file' ?
John-S PretoriusTechnical Service Manager, Mid-AtlanticAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DcpKingCommented:
So you're using SQL Server 2008.

* With that comes a tool called SSIS.
* The interface for designing jobs like the one you want to create and then run them using SSIS is called BIDS, and you should be able to find it in your menu structure, somewhere under the SQL Server entries.
* You can use BIDS to create a new SSIS package and include in that an "Execute SQL" job.
* For the Execute SQL task you'll need to create the definition of a connection to the database you want to use: there's a wizard that will help you do that.
* Then, in the interface wizard for the Execute SQL task, you can type in your SQL:

Update users set name = 2 where category = '5'

(You'll already be using User_db, etc., and there'll be no need for the "Go".

* Finally you can run your new package in BIDS to test it.

* If all that (which isn't really very much, actually - it takes more to write about it than to do it!), you'll be able to create a SQL Server Agent Job that will execute your package and schedule that in the SQL Server Agent. Look here for a step-by-step instruction for deployment.here - they're much better at that than I am!

hope this helps

Mike
0
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
I am using SQL 2008, which is locked down to limited functionality in a parking environment.

Thank you for your input Mike, I am fully aware of using this approach but with the closed system I'm using in a parking environment that feature is not available (locked-down) indefinably. I do however have available Powershell and scheduler which is what I have been looking into but need a little assistance putting together a complete .bat script that will make the connection, process the sql query and close up again.

Also, I would be scheduling this on the same PC running SQL.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Just save the code as a SQL file and the use the SQLCMD utility to run it inside a schedule task.
Example of the SQLCMD utility (run form command prompt) to run a script in a SQL Server instance using trusted connection (-E):
sqlcmd -S ServerName\InstanceName -E -i C:\Path\SQLScript.sql

Open in new window

0
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

Marten RuneSQL Expert/Infrastructure ArchitectCommented:
Save the file somwhere as <Filenamewhatever>.PS1
The in Windows scheduler, create a new basic task
copy and past this code into the execute part of windows scheduled task (don't have a English computer nearby, so do an educated guess), the code comes here:

Powershell -ExecutionPolicy bypass -file <FullPath>\<Filenamewhatever>.PS1

This will launch it, and bypass executionpolicy restricted bye the way. So its safer than fiddeling with Set-Executionpolicy unrestricted.

To make it a lot safer, let windows scheduled task run as a domain user (with low level rights) and the powershell file should be encrypted with this users EFS key. This means nobody else can alter the code that windows scheduled task executes. This would be considered a secure solution in most Companys.

Regards Marten
0
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
Thank you for your input, I need direction/assistance creating this .PS1 file please.
0
Marten RuneSQL Expert/Infrastructure ArchitectCommented:
Save the stuff between the *** in a PS1 file, and execute it
(remember to fill in $SQLServer = '<EnterSQLServerNameHere>'
and check that the DB is named users_db)

***********************************************************************
$SQLServer = '<EnterSQLServerNameHere>'
$SQLDB = 'users_db'

function QueryDB($DBServer, $Database, $Query, $BatchName) {
#write-host $DBServer, $BatchName
      $connection = new-object system.data.sqlclient.sqlconnection($connectionString)
      $connection.connectionString = $("Data Source=" + $DBServer + ";Initial Catalog=" + $Database + " ;Trusted_Connection=True; Connect Timeout=0; Application Name=" + $BatchName + ";" )
      $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
      $SqlCmd.CommandText = $query
      $SqlCmd.Connection = $connection
      $SqlCmd.CommandTimeout = 0
      $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
      $SqlAdapter.SelectCommand = $SqlCmd
      $DataSet = New-Object System.Data.DataSet
      $script:Supress = $SqlAdapter.Fill($DataSet)
      $connection.close()
      return ,$DataSet.Tables[0]
}

QueryDB $SQLServer $SQLDB  "Update users set name = 2 where category = '5'" "ApplicationName_GoodAsProfilerFilter"
***********************************************************************

Regards Marten
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
John-S PretoriusTechnical Service Manager, Mid-AtlanticAuthor Commented:
Thank you much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.