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

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

Related

Executing SQL File in SSIS

I am using Microsoft Visual Studio 2010 currently. I have a .sql file on my computer which has a lot of sql statements of code in it. Basically has three create table statements, multiple insert into statements, multiple alter table statements,adding foreign keys, etc.
I want to know is there a way that I can load that .sql file into an Execute SQL Task. Or how is it possible, in SSIS, that I can execute this long .sql file? I feel like an Execute SQL Task is involved, but I don't know for sure. This was the Execute SQL Task I tried before to no avail.
Any help would be appreciated.
I have some screenshots basically to show how long of a file I'm talking about...and it goes longer than what is shown.
Execute SQL Task will be what runs commands.
Change the SQL Source type from the default of "Direct Input" to "File Connection"
Then in the FileConnection property, specify a file connection manager that points to MyFile.sql
That said, you can just run the above file(s) in SSMS, or sqlcmd if you prefer a command line
does the sql file contain parameterised insert statements? if so you need to have map the parameters and check that the source to which you are connecting is accessible and the structure of the tables are same as well

How to execute SQL queries from text files

I am using Aqua Data Studio 7.0.39 for my Database Stuff.
I have a 20 SQL files(all contains sql statements, obviously).
I want to execute all rather than copy-paste contains for each.
Is there any way in Aqua to do such things.
Note: I am using Sybase
Thank you !!
I'm also not sure of how to do this in Aqua, but it's very simple to create a batch/powershell script to execute .sql files
You can use the SAP/Sybase isql utility to execute files, and just create a loop to cover all the files you wish to execute.
Check my answer here for more information:
Running bulk of SQL Scripts in Sybase through batch
In the latest versions of ADS there is an integrated shell named FluidShell where you can achieve what you are looking for. See an overview here: https://www.aquaclusters.com/app/home/project/public/aquadatastudio/wikibook/Documentation15/page/246/FluidShell
The command you are looking for is source
source
NAME
source - execute commands or SQL statements from a file
SYNOPSIS
source [OPTION...] FILE [ARGUMENT...]
source [OPTION...]
DESCRIPTION
Read and execute commands or SQL statements from FILE in the current shell environment.
I have not used Aquafold before so I can't tell you exactly. However I have tackled a similar problem once before.
I once created a Powershell script. It opened a ODBC connection to my database and then executed stored procedures in a loop until end of file.
I suggest having a text document with each line being the name of an Stored Proc to run. Then in your powershell script read in a line from the file concatenate it into the call to execute a stored procedure. After each execution is completed you can delete the line from the text file and then read the next line until the EOF (end of file) is reached.
Hope this helps. If I have some time this morning I will try and do a working example for you and post it.

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

SQL Server Management Studio command line?

Is there a command line interface where I can run a script that says, for example:
Connect to server a
Run query 1
Connect to server b
Run query 2
Run query 3
I tried searching online but couldnt find anything built into SQL Server Management Studio.
Thanks!
The sqlcmd Utility
http://msdn.microsoft.com/en-us/library/ms162773.aspx
It usually ships with the SQL Server installer and is located in the following directory by default:
C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE
If you want to export in/out there is a command-line utility called BCP that will let you execute these types of processes.
bcp Utility
The bcp utility bulk copies data between an instance of Microsoft SQL
Server and a data file in a user-specified format. The bcp utility can
be used to import large numbers of new rows into SQL Server tables or
to export data out of tables into data files. Except when used with
the queryout option, the utility requires no knowledge of
Transact-SQL. To import data into a table, you must either use a
format file created for that table or understand the structure of the
table and the types of data that are valid for its columns.

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.