Unable to execute the queries in .sql file in SQL Server 2017 - sql-server-2017

On saving a SQL query file in SQL Server 2017 and loading the Microsoft SQL Server query file (.sql) with SSMS, I cannot execute the queries I have written in the .sql file but when I copy and paste the queries onto a new query created on the database, the same query is able to execute. Can anyone tell me what I am doing wrong?
STR:
Open the exiting saved .sql file with SSMS
Execute the query in the .sql file
Unable to run the query
Copy the query and paste into a new query file created on the database
The query is able to execute.
Note: the .sql file was created in SQL Server 2017
This issue keeps occurring when I open previous .sql file and it has become an hassle to copy and paste it into a new query file by right clicking on the database and selecting new query.
On loading the .sql file
I can only connect to the master database but not the database that I'm targeting
After connecting to the database

Related

I copied an SQL folder that contains database info and I would like to add it to Microsoft SQL Server Management Studio. How Do I do this?

Not very familiar with databases, but I have a task where an entire database folder was backed up and copied to a drive on my computer. I have SQL Server installed and Microsoft SQL Server opened. How do I use this folder and its contents as a database?
The folder has the following subfolders
SQL Server -> Backup, binn, DATA, FTData, Install, Jobs, Log, Upgrade and two files: sql_engine_core_inst_keyfile.dll, sql_fulltext_keyfile.dll
For each .ldf+.mdf file sets you have you can restore a database using the following sql. You cannot create the databases in bulk and will have to do them one at a time if you have multiple.
For each .ldf + .mdf combo you have update the dbname and the two filenames
create database dbname
on
(
Filename= 'path where you copied .mdf data file',
Filename ='path where you copied .ldf log file'
)

attach files to SQL operation studio

I have .MDF and .LDF files created by SQL Server. I'm trying to attach them to my SQL operations studio. I have put files in var/opt/mssql/data but the operation studio does not show the files even after refreshing or disconnecting. Is there any other ways to attach?
Databases can be attached using a T-SQL query. Execute CREATE DATABASE...FOR ATTACH from a query window, specifying the desired database name along with the existing file paths with the FILENAME clause. For example:
CREATE DATABASE YourDatabase
ON (FILENAME = '/var/opt/mssql/data/yourdb.mdf')
LOG ON (FILENAME = '/var/opt/mssql/data/yourdb_log.ldf')
FOR ATTACH;
This method can be used with SQL Operations Studio, SQL Server Management Studio, SQLCMD, mssql_cli, or any tool that can run SQL queries.

SQL Server 2012 : How to script all database stored procedures into separate .sql files?

I want to script all the stored procedures from SQL Server 2012 to Visual Studio 2012 as .sql files (in a different project). How do I do that? I want one .sql file for each stored procedure?
I get the scripts using the Generate Scripts in Tasks option after right clicking DB in SQL Server 2012. However, the name of the .sql file is spname.StoredProcedure in each case. I want the name to be spname.sql.
Again I don't want the file name to be database.spname.StoredProcedure, I want it to be just spname.sql
Please check following SQL tutorial showing how to generate a separate script file for each stored procedure in a SQL Server database
The solution uses sp_Helptext stored procedure with SQL BCP command for generating create script for target SP and then create the export .sql file on the file system.
I hope it helps,
Additionally on generate script wizard, if you configure selection options as seen in below picture, you will get the sp name as the output script file name
As #Ivan_Starostin said:
Step one
Step two
Step three

creating batch file to run sql query in sql developer and store result in excel sheet

I am creating a batch file that will be executed daily at a specific time. The batch file will execute a .sql file. The .sql contains a complex select statement. I have created the sql file using Sql Developer. Also the output of the sql must be stored in an excel sheet with name as todays date.
I have read posts related to batch file to run the sql script but have not found clear answers. I am completely new to batch files.
Thanks
try to use bcp command http://sqlfool.com/2008/12/bcp-basics/ with queryout option

How do I import a sql data file into SQL Server?

I have a .sql file and I am trying to import it into SQL Server 2008. What is the proper way to do this?
If your file is a large file, 50MB+, then I recommend you use sqlcmd, the command line utility that comes bundled with SQL Server. It is easy to use and it handles large files well. I tried it yesterday with a 22GB file using the following command:
sqlcmd -S SERVERNAME\INSTANCE_NAME -i C:\path\mysqlfile.sql -o C:\path\output_file.txt
The command above assumes that your server name is SERVERNAME, that you SQL Server installation uses the instance name INSTANCE_NAME, and that windows auth is the default auth method. After execution output.txt will contain something like the following:
...
(1 rows affected)
Processed 100 total records
(1 rows affected)
Processed 200 total records
(1 rows affected)
Processed 300 total records
...
use readfileonline.com if you need to see the contents of huge files.
UPDATE
This link provides more command line options and details such as username and password:
https://dba.stackexchange.com/questions/44101/importing-sql-server-database-from-a-sql-file
If you are talking about an actual database (an mdf file) you would Attach it
.sql files are typically run using SQL Server Management Studio. They are basically saved SQL statements, so could be anything. You don't "import" them. More precisely, you "execute" them. Even though the script may indeed insert data.
Also, to expand on Jamie F's answer, don't run a SQL file against your database unless you know what it is doing. SQL scripts can be as dangerous as unchecked exe's
Start SQL Server Management Studio
Connect to your database
File > Open > File and pick your file
Execute it
Try this process -
Open the Query Analyzer
Start --> Programs --> MS SQL Server --> Query Analyzer
Once opened, connect to the database that you are wish running the script on.
Next, open the SQL file using File --> Open option. Select .sql file.
Once it is open, you can execute the file by pressing F5.
In order to import your .sql try the following steps
Start SQL Server Management Studio
Connect to your Database
Open the Query Editor
Drag and Drop your .sql File into the editor
Execute the import
A .sql file is a set of commands that can be executed against the SQL server.
Sometimes the .sql file will specify the database, other times you may need to specify this.
You should talk to your DBA or whoever is responsible for maintaining your databases. They will probably want to give the file a quick look. .sql files can do a lot of harm, even inadvertantly.
See the other answers if you want to plunge ahead.
Get the names of the server and database in SSMS:
Run the following command in PowerShell or CMD:
sqlcmd -S "[SERVER NAME]" -d [DATABASE NAME] -i .\[SCRIPT].sql
Here is a screenshot of what it might look like:
There is no such thing as importing in MS SQL. I understand what you mean. It is so simple. Whenever you get/have a something.SQL file, you should just double click and it will directly open in your MS SQL Studio.