Solved

How to run sql statements from SQLCMD or command line

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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.​
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now