How to execute SQL script in directory from another script? - sql

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.

Use xp_cmdshell to run sqlcmd utility to execute your script:
exec xp_cmdshell 'SQLCMD -S <Server> -E -i "C:\path\script.sql"

Related

SQL Server Agent unable to run SQLCMD

An SQL Server instance was set up without access to the Internet and failed to install SQLCMD during the initial SQL Server installation process. Upon discovering this SQLCMD was installed through the MsSqlCmdLnUtils.msi installer. Can now run SQLCMD through command line, as user account, but when running SQLCMD as part of xp_cmdshell the error is
'sqlcmd' is not recognized as an internal or external command,operable program or batch file.
I've manually given read/write access to all the SQLCmd files, i.e. .exe, .rll, etc, to the SQL Agent NT account. I can manually run the sqlcmd through command line as user account. Xp_cmdshell can run successfully when given other command line tasks, i.e. moving files, mapping drives, etc. Restarted SQL Server Agent.
SET #sqlCommand =
'sqlcmd -S hqconnectsql1 -d M -Q "SET NOCOUNT ON; select * from temp_travelSurvey" -o"' +
#filePath + #fileName +
'" -W -s"|" -w 1024'
EXEC master..xp_cmdshell #sqlCommand
GO
The above code is what is failing when run as a query through SSMS, left out are the variables that are declared and set prior to the command.
EXEC master..xp_cmdshell 'net use Y: \\HQPCISQL\e$ troperM12 /USER:aaa177\Reportsrv'
go
This code works successfully when run as a query through SSMS, showing that xp_cmdshell is not the issue.
The expected result, which I have been able to have succeed when run through the command prompt, is for a file containing the content of temp_travelSurvey to be created. Current error message is the sqlcmd is not a recognized internal or external command.
on SSMS you can execute your sql directly, you need not to use SQLCMD there. SQLCMD is command line interface to execute SQL from command prompt. I hope this would help you.
Like in above case directly execute sql scripts as below.
select * from temp_travelSurvey

How to execute the SQL Script through Batch Script Command from SQL?

I am trying to execute the .sql file from batch script, but not able to execute.
Script has multiple scripts such as Create Table , Stored Procedure separated by GO statement.
I tried with the below script, however script was not executed.
DB Servername : localhost\SQL2017
Authentication : Windows
sqlcmd -E -S localhost\SQL2017\MyDatabase -i C:\SQLScripts\TestScript.sql
Try this instead:
sqlcmd -E -S localhost\SQL2017 -d MyDatabase -i C:\SQLScripts\TestScript.sql
You need to use the -d parameter to specify the database name. The -S parameter should contain only the server name and the instance.

How to execute generated script(.sql file) with schema and data in SQL Server 2008

Using SSMS 2008 I am able to generate a script for a database with huge amounts of data in file ABC.sql on my desktop.
The database has approx. 9 GB of data so I'm unable to open the file. Is there any way to execute the script?
When I try to open it in SSMS I get an error:
The operation could not be completed. not enough storage is available to complete this operation
The template specified cannot be found. Please check that the full path is correct
SQL Server offers 2 command prompt features that can se used for executing large queries - osql (will be removed in future), and sqlcmd
osql is located in the Tools\Binn subfolder. To execute a SQL script:
Start the Command Prompt
Navigate to the folder where the osql utility is located
Run the command in the following format:
osql –H <workstation name> -S <server_name[\instance_name]> -U <user login ID> -P <login password> –i <full path to script>
To execute the large.sql file located in the D:\test, against the Central database on the SQL Server instance Dell\SQL2012, as an sa with the 'sqladmin' password, run the following command:
osql -H Dell -S Dell\SQL2012 -i D:\test\large.sql -U sa -P sqladmin
The sqlcmd command line utility is also located in the SQL Server’s Tools\Binn sub-directory. To execute a SQL script:
Start the Command Prompt
Navigate to the folder where the sqlcmd utility is located
Run a command in the following format:
sqlcmd –S <server name> -d <database name> -i <full path to script> -U <user login ID> –P <login password>
To execute the same as above, run the following command:
sqlcmd -S Dell\SQL2012 -d Central -i D:\test\large.sql -U sa –P sqladmin
Start the sqlcmd Utility
Run Transact-SQL Script Files Using sqlcmd
I use sqlcmd to execute large SQL files.
You can generate script of your database by RightClick on your database Tasks>GenerateScripts> click next on Generate and Script window Check on select specific table choose tables you want Press next Click on Advance option on end of General Category select Type of data to script now choose which kind you want your database to.
Scheme Only: Means this script will create your database.
DataOnly:If you have created database and table this will insert data into it.
Press ok then Next.
Your file is by default save in
C:\Users[UserName]\Documents\ .

How to figure out all the instances on sql server?

I am connected to one instance of the database server. How will i come to know if there are any other instances on the same server and their names?
First go to Start >> Run >> CMD (Open command prompt). Once in command prompt run following command based on SQL Server version installed on local machine.
For SQL Server 2000:
C:\> isql -L
For SQL Server 2005 / SQL Server 2008:
C:\> osql -L
OR
C:\> sqlcmd -L
As SQL script below is the snippet - Source
This script requires execute permissions on XP_CMDShell.
CREATE TABLE #servers(sname VARCHAR(255))
INSERT #servers (sname)
EXEC master..xp_CMDShell 'ISQL -L'
DELETE
FROM #servers
WHERE sname='Servers:'
OR sname IS NULL
SELECT LTRIM(sname)
FROM #servers
DROP TABLE #servers

How to save query result using T-SQL in a csv file?

How to save query result using T-SQL in a csv file in SSMS 2008? Needs to be done programmatically.
You could use System.Diagnostics.Process to run the SQLCMD or OSQL or BCP programs to dump out csv files.
Here is an example I found from google. Play around with the parameters.
EXEC master..xp_cmdshell 'sqlcmd -X -S server -q "SET NOCOUNT ON Select 'department','FundCode','Month' SELECT * FROM pubs.dbo.test" -s , -w 255 -u -o "\\server\output.csv" -h-1'