Solved

How to run sql statements from SQLCMD or command line

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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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.

760 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

20 Experts available now in Live!

Get 1:1 Help Now