[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQLCMD Batch File - Syntax Question

Posted on 2009-02-21
4
Medium Priority
?
958 Views
Last Modified: 2012-06-27
I'm trying to accomplish the following in a cmd batch file but don't know the syntax...I tried just putting it in a batch file but it fails terribly..

SQLCMD -S server/sqlexpress
use master
go
sp_detach_db 'dbasename.mdf'
go
sp_attach_db 'attachdbname.mdf', 'c:\mdflocation\mdffile.mdf', 'c:\ldflocation\ldffile.ldf'
go
0
Comment
Question by:ChiBella
  • 2
4 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 23701214
the problem is that you need 2 files:
1 .sql file with the script, and the second calling sqlcmd with specifying that .sql file as parameter:
http://msdn.microsoft.com/en-us/library/ms162773.aspx

SQLCMD -S server/sqlexpress -i yourfile.sql

Open in new window

0
 
LVL 51

Assisted Solution

by:Mark Wills
Mark Wills earned 1000 total points
ID: 23701507
You will also need to tell it how to login...  use -E for windows / trusted login otherwise use -Uusername  -Ppassword

sqlcmd -S servername -d databasename -E -i scriptfilename      

the T-SQL is what goes into a normal text file (notepad, or save your query from a query window) so in AngelIII example, yourfile.sql will contain :

use master
go
sp_detach_db 'dbasename.mdf'
go
sp_attach_db 'attachdbname.mdf', 'c:\mdflocation\mdffile.mdf', 'c:\ldflocation\ldffile.ldf'
go
0
 

Author Comment

by:ChiBella
ID: 23701591
Great Info! Thanks...
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 23701655
You also have OSQL available, so if you are more used to that, then you can continue up until 2008...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

872 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