Solved

How to run sql statements from SQLCMD or command line

Posted on 2016-09-15
2
71 Views
Last Modified: 2016-09-15
I want to run the following statements from a command line and schedule it. How would I accomplish this?

DROP TABLE DBO.WOYTRN01

SELECT * into WOYTRN01 FROM OPENQUERY(ATQAV, 'SELECT * FROM WOYTRN01 where 1=0')

INSERT INTO WOYTRN01
SELECT * FROM OPENQUERY(ATQAV, 'SELECT * FROM WOYTRN01')

Open in new window

0
Comment
Question by:maximus1974
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 41800650
How about saving the above T-SQL as a stored procedure, and then creating a job in SQL Agent that executes that Stored Procedure?
0
 
LVL 40

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 250 total points
ID: 41800684
Agreed that SqlServer Agent is the way to go.

If not you can use sqlcmd:
https://msdn.microsoft.com/en-us/library/ms180944(v=sql.110).aspx

Save it to a .sql file and then:
sqlcmd -S <ComputerName>\<InstanceName> -i <MyScript.sql>

You can use windows scheduler at that point . . . but sqlServerAgent to me is always the better place as it's centralized.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question