Combine multiple stored procedures into one script - sql

I have multiple stored procedures in my application database. I need an easy way for someone else to integrate all my stored procedures into his/her database. Is there a way to combine all my stored procedures into one script so that someone can run the single script to recreate all my stored procedures in his/her database?

Use the sql server "Generate Script" Wizard.
Right Click on the database from which you want to Generate the scripts
Choose Tasks --> Generate Scripts
Click Next on the "Introduction" window and in the 2nd screen select the option button "Specific Database objects" and click the combo box near "Stored Procedure" (If you are only taking the scripts of stored procedures.
On the Next screen give the path and file name to where you want to save the script.
Click the advanced button and change the following
a) Check for Object Existence to "True"
b) Script Use database - false
c) Script Drop and Create
d) Script Object-level Permissions - True
items c & d are optional.
Once you have all these set Click Next till you reach the Final screen and now hit Finish. You will get all the procedures in a single .sql file

Related

How do I transfer a schema and all of its tables to a new database?

I have database a with schema foo which contains 20 tables. I want to move all of the contents of schema foo into database b without overriding the current content in database b.
Is there also a way to do it in pgadmin?
I found this link and perhaps it will be quite similar. But this particular link is for transferring a table.
Copy a table from one database to another in Postgres
You can script the first database with all its data once scripted you can run the th script within the other database it should work as long as you dont have tables in the second database with the same name
so in pg admin follow these steps to script the
-Right click on the database and click Backup.
-Selece a filepath and filename on where you want to save your script
-Select Plain as the format in the format dropdown.
-Go to Options and check "schema and data" in tab # 1.
-Then click on Backup.
-Then click Done.
-Then right click on your 2nd database and create a new query.
-Find where you saved the script and copy the script to the query
-run the query and should be all good
if you are unsure about this just create 2 practice databases and practice on those before you do it on the main one

Check the names of Tables/Views/Procs and compare with those from a different database

I am currently in the process of moving tables, views, and stored procedures from one database in SQL Server to another database on the same server. A couple weeks ago, I had already started manually moving some of these procdures, and I am unsure of which ones I have moved so far. I want to write a query that compares the tables I have already moved to database 2 to the ones in database 1, and lists the stuff in database 1 I have not moved yet. I guess I am confused as to where to start, as I don't have much experience in using the system tables.
The easiest way would be to disregard which you already did, script them all out using the SSMS Generate Scripts task, and do a DROP/CREATE or check for existence just to be sure.
Open SSMS
Open the object explorer, connect to your instance,
and open the Databases folder
Right click on the database that
has the object you want to move over and click Tasks, then click
Generate Scripts
Click the Select specific database objects radio button and select all the tables/procs/etc you want to move over. Click Next
Click Advanced, change Script Drop and CREATE to SCRIPT DROP AND CREATE (or change Check for object existence to TRUE), then change Types of Data to Script to Schema and Data
Click Save to File radio button and script it all out
Here's something that should work.
select
name
,type_desc
from
CurrentDatabase.sys.objects
where
is_ms_shipped <> 1
and type_desc in ('VIEW','SQL_STORED_PROCEDURE','USER_TABLE') --add in trigers, TVF, etc that you may want...
except
select
name
,type_desc
from
YourNewDatabase.sys.objects
where
is_ms_shipped <> 1
and type_desc in ('VIEW','SQL_STORED_PROCEDURE','USER_TABLE') --add in trigers, TVF, etc that you may want...

Backup a single table with data in SQL Server [duplicate]

I want to get a backup of a single table with its data from a database in SQL Server using a script.
How can I do that?
SELECT * INTO mytable_backup FROM mytable
This makes a copy of table mytable, and every row in it, called mytable_backup. It will not copy any indices, constraints, etc., just the structure and data.
Note that this will not work if you have an existing table named mytable_backup, so if you want to use this code regularly (for example, to backup daily or monthly), you'll need to run drop mytable_backup first.
You can use the "Generate script for database objects" feature on SSMS.
Right click on the target database
Select Tasks > Generate Scripts
Choose desired table or specific object
Hit the Advanced button
Under General, choose value on the Types of data to script. You can select Data only, Schema only, and Schema and data. Schema and data includes both table creation and actual data on the generated script.
Click Next until wizard is done
There are many ways you can take back of table.
BCP (BULK COPY PROGRAM)
Generate Table Script with data
Make a copy of table using SELECT INTO, example here
SAVE Table Data Directly in a Flat file
Export Data using SSIS to any destination
You can create table script along with its data using following steps:
Right click on the database.
Select Tasks > Generate scripts ...
Click next.
Click next.
In Table/View Options, set Script Data to True; then click next.
Select the Tables checkbox and click next.
Select your table name and click next.
Click next until the wizard is done.
For more information, see Eric Johnson's blog.
Put the table in its own filegroup. You can then use regular SQL Server built in backup to backup the filegroup in which in effect backs up the table.
To backup a filegroup see:
https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/back-up-files-and-filegroups-sql-server
To create a table on a non-default filegroup (its easy) see:
Create a table on a filegroup other than the default
Another approach you can take if you need to back up a single table out of multiple tables in a database is:
Generate script of specific table(s) from a database (Right-click database, click Task > Generate Scripts...
Run the script in the query editor. You must change/add the first line (USE DatabaseName) in the script to a new database, to avoid getting the "Database already exists" error.
Right-click on the newly created database, and click on Task > Back Up...
The backup will contain the selected table(s) from the original database.
To get a copy in a file on the local file-system, this rickety utility from the Windows start button menu worked:
"C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\DTSWizard.exe"

Extended event session on SQL Server 2014 to capture stored procedure calls and arguments passed

I have an application which generates certain reports while values are entered and generate button is clicked on an application.
I know to which database this application is connecting, but don’t know which stored procedure is getting called. The database is having 100s of stored procedures.
So I need to track which stored procedure is called and what arguments are passed while clicking each button on the application.
Guys, please could you help me to create an extended event session which captures every stored procedure call with the arguments passed - in a specific database?
Thanks
JJ
below is the way to capture stored proc details using extended events
1.Go to management ->Extended Events --> sessions-->Right click and say new session wizard and give a new session name
2.In choose template field,dont choose default template and click next..
3.In next screen you will be presented with below screen,search for batch and select sql batch started and completed events as shown in screenshot.. and click next
Note: Extended events so much info ,if you change the channel to debug,you will be presented with many more events like spill to tempdb,cpu..
4.Next screen presents you with options to choose fields of interest,here I choose text,connection id,client name..
5.in the next screen ,select flters based on your choice,i choose databasename ,you can choose batch text as well for a single proc
6.In this screen,you have the options to choose option of storing data,i choose opion1 since ,I may need data sets collected for long period of time.
7.final screen shows you summary and gives you an option to script out what you have done so far .And also start the event session check box in finalscreen as shown in screen below
Now I ran my stored proc in ssms and when finally when I want to see data,i stopped event session .
through File ->new >merge extended events option ,I have the option to choose multiple data sets and it will be shown llike below
I can see my stored proc and fields of interest as highlighted..

How to backup Sql Server to sql file?

In "Back UP" I only get a bak file, but I would like to create .sql file
Use SQL Server's Generate Scripts commend
right click on the database; Tasks -> Generate Scripts
select your tables, click Next
click the Advanced button
find Types of data to script - choose Schema and Data.
you can then choose to save to file, or put in new query window.
results in CREATE and INSERT statements for all table data selected in bullet 2.
This is a possible duplicate of: SQL script to get table content as "SELECT * FROM tblname"
To do a full database backup to File/Query you can use the 'Generate Scripts...' option on the Database.
Open SQL Server Management studio, right click on the database and choose 'Tasks->Generate Scripts...'
Then use the wizard to backup the database. You can script the whole database or parts of it. Two important options: In the 'Advanced' section, you will probably want to ensure 'Type of backup = 'Schema and Data' and the 'Script Statistics' is on.
This will produce a *.sql file that you can use as a backup that includes the schema and table data.
Ok, I read through most of these, but I had no "advanced button". But, there is still a way to do it, it's just a little hard to find, so here you go:
You can generate a script from a database, see http://msdn.microsoft.com/en-us/library/ms178078.aspx
If you want to create a script of your database you right-click on the databases and Generate Scripts (it's in different sub-menus depending on what version of SQL and Enterprise Manager / SQL Server Management studio you're using).
That will, however, only get you the database objects. It will not generate scripts for data. Backing up a database will give you all of the database objects as well as the data, depending on what recovery model your database is set to.
This fellow may have achieved what you are trying to do by creating the backup, and then restoring it and giving it a new name.
This approach copies the data along with all of the database objects.
If you want a file with insert statements for your data have a look here:
This procedure generates INSERT statements using existing data from the given tables and views. Later, you can use these INSERT statements to generate the data. It's very useful when you have to ship or package a database application. This procedure also comes in handy when you have to send sample data to your vendor or technical support provider for troubleshooting purposes.
http://vyaskn.tripod.com/code.htm#inserts