0

Say I have a .sql script stored in c:\scripts\some_script.sql.

What command do I use to execute this script from within another script or stored procedure.

For example

create procedure dbo.sp_execute_script
    @script_location varchar(100)   
as  
    execute(@script_location)

 -- does not work  
go

I am running the scripts in SSMS.

2
  • You want to run c:\scripts\some_script.sql, and that in turn should call another script - is that the case? Where is the create procedure code supposed to be? Commented May 7, 2014 at 4:02
  • sp_execute_script is a stored procedure. Within this Stored Procedure, i will like to run the script located in "c:\scripts\some_script.sql" Commented May 7, 2014 at 13:23

1 Answer 1

3

Use xp_cmdshell to run sqlcmd utility to execute your script:

exec xp_cmdshell 'SQLCMD -S <Server> -E -i "C:\path\script.sql" 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Just what I needed. Used this link to be able to execute xp_cmdshell: mangalpardeshi.blogspot.com/2008/12/…

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.