Solved

How to run sql statements from SQLCMD or command line

Posted on 2016-09-15
2
62 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

825 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