1

I am trying to run a .sql file in SQL Server Management Studio using this command

EXEC xp_cmdshell  sqlcmd -s  '127.0.0.1' -d  MyDB -i 'C:\Data\ProcessedSQL\ReversalFile1.sql'

but I am getting an error

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '-'

Can someone assist me please?

Update Edit: To be clear, I am only starting to use SQL Server.
I have several sql files in one folder and I was hoping to run a query window in SSMS to run several sql files one after another as follows:
Execute file1
Execute file2
Execute file3
The Files are being generated out of another system by a DBA.

6
  • 1
    try EXEC xp_cmdshell 'sqlcmd -s "127.0.0.1" -d MyDB -i "C:\Data\ProcessedSQL\ReversalFile1.sql"' Commented Feb 9, 2015 at 9:04
  • Wouldn't you have to quote the whole command as a single string? Here's an answer that does that but I can't believe there isn't a better way to do this, or a better option for doing this at all (e.g. put that SQL into a stored procedure?) Commented Feb 9, 2015 at 9:04
  • 1
    why to execute it when you can open it in sqlserver editor ? is there any requirement? Commented Feb 9, 2015 at 9:04
  • 1
    msdn.microsoft.com/en-us/library/ms170572.aspx here is the link for your reference Commented Feb 9, 2015 at 9:08
  • 1
    I used something like this before osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i CoreLibLUT_Insert_VoiceOverride.sql -o CoreLibLUT_Insert_VoiceOverride.out. Generally, I use this in batch script in which I write the names of sql files in a proper sequence then give the whole package to client. they run this batch file to upgrade the DB to newest version. Commented Feb 9, 2015 at 9:08

1 Answer 1

1

Use SSMS in SQLCMD mode to run an external SQL file:

:R Pathtoyourfileinthesqlserver

https://msdn.microsoft.com/en-us/library/ms174187.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

I only just discovered this yesterday on this very site

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.