Script that will make a SQL Server script from a database - sql-server

I have a SQL Server database that contains tables and other objects that I would like to script out on a regular basis. The idea is that I am going to create an autobuild that will create a container that is a scaled down version of this database (for testing).
I could easily just go script the database manually, but then I have to keep the resulting script up-to-date.
I am wondering if there a way to programmatically connect to a running SQL Server instance and (based off some input) generate scripts of specific tables, stored procedures, user defined types and data?

If you're coding with .NET other other languages that can call .NET objects, you can just use the scripting classes in SMO (SQL Management Objects).
There are worked examples in the SQL Server documentation here:
https://learn.microsoft.com/en-us/sql/relational-databases/server-management-objects-smo/tasks/scripting?view=sql-server-ver15
They are designed to do exactly what you're requesting.

Well, I would think a backup would be the easiest way to go.
Or, setup several jobs, wrap everything in a SProc, and schedule that to run on whatever frequency you desire.
To create and attach a schedule to a job
In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
Expand SQL Server Agent, expand Jobs, right-click the job you want to schedule, and click Properties.
Select the Schedules page, and then click New.
In the Name box, type a name for the new schedule.
Clear the Enabled check box if you do not want the schedule to take effect immediately following its creation.
For Schedule Type, select one of the following:
Click Start automatically when SQL Server Agent starts to start the job when the SQL Server Agent service is started.
Click Start whenever the CPUs become idle to start the job when the CPUs reach an idle condition.
Click Recurring if you want a schedule to run repeatedly. To set the recurring schedule, complete the Frequency, Daily Frequency, and Duration groups on the dialog.
Click One time if you want the schedule to run only once. To set the One time schedule, complete the One-time occurrence group on the dialog.

Related

How can I run a SQL statement in the background so the user don't have to wait, in SQL Server?

I want SQL Server to create a pdf every time that a quotation is created in SAP Business One database. I already have a program that create the file and can run it from SQL Server (using xp_cmdshell), the problem is that it takes a long time to do it and SAP Business One stays frozen until it's done.
I need to execute the Pdf maker program in the background

how to send data from one database to another using linked server

I have created a linked server to copy data from one database to another and this worked well when I manually write the query and execute it. But I want this to be scheduled to happen automatically. Should I use linked server for this or is there a better way to implement it?
If you have SQL scripts to transfer data, you can use SQL Script job step in a new SQL Server job.
I've created a step by step short tutorial showing how to create SQL Server job which executes a SQL stored procedure periodically according to the assigned execution schedule.
You should try using the scheduled jobs, with SQL Server Agent.
In short, you will need to created a stored procedure that executes the moving code you need.
Then, from Databases > SQL Server Agent > Jobs, you can create a new scheduled job to make it run when you need it to.

Create a copy of production databases, without any data?

I need to create a copy of all of our production databases (SQLServer), without any data.
I need to do this on a regular basis, preferably scheduled and not manually.
Do I have to write code that extracts from systables and builds whe SQL-statements itself or is there a good way to do this?
As there is a method to do that in SQL Management Studio:
Select a database
right click
tasks
generate script
etc ...
You can then save the script generated as an sql file.
Once the script set, and if it is T-SQL, you can just add it to the jobs of your server. .And if you have only a SQL EXPRESS server (with no job schedule), I remember it was possible, a few years ago, to find some free products on the net that would do the job.

How to sync schema continuously in two databases in Sql Server (for unit testing)

We have a database against which we run unit tests for components that require a database (for several reasons we are not mocking the DAL everywhere).
We are using Sql Server 2008 R2 and in the development db server we have our development database (ApplicationName_Dev) and our testing db (ApplicationName_UT).
The unit tests create the test data they need and delete it afterwards so the tables could/should be empty when no tests are running.
The problem is keeping the schema of the unit test database up to date.
The best solution for me (to my limited knowledge) would be to have a Sql Server Agent Job that would run once a night (or when manually started) that would drops all the tables in the UT database, generate a create script for all tables, indexes and relationships in the Dev-database, and run the create scripts on the UT-database. Note that we don't need to insert any data.
Is there any way of programmatically (T-Sql, SMO etc) generating Create scripts for all tables including indexes and relationships?
In Management Studio I can right click the database->Tasks->Generate scripts...->Choose Objects->Tables and I get just the scripts that I want (except for the "Use [ApplicationName_Dev]" on the first line.
Please help.
Regards,
Mathias
I'd create an SSIS package - there's a task called "Transfer SQL Server Objects Task". Specify your Source and Destination Connections & Databases, set DropObjectsFirst to True, and CopyAllObjects (or just CopyAllTables and CopyAllViews) also, and you should be set. (And obviously, don't set CopyData to true).
You also need to set the CopyIndexes and other such table options, for those table structures you want.
Setting up a job to run an SSIS package is also quite easy.
You could use a tool like SQL Delta. You create a "script" (SQL Delta specific script) using SQL Delta and essentially , what you can do is get it to sync the source database with the destination database. It can also pump in data into some or all tables if needed.
The whole process can be automated using a scheduled job using the Scheduler (part of Windows).

Copy table to a different database on a different SQL Server

I would like to copy a table from one database to another. I know you can easily do the following if the databases are on the same SQL Server.
SELECT * INTO NewTable FROM existingdb.dbo.existingtable;
Is there any easy way to do this if the databases are on two different SQL Servers, without having to loop through every record in the original table and insert it into the new table?
Also, this needs to be done in code, outside of SQL Server Management Studio.
Yes. add a linked server entry, and use select into using the four part db object naming convention.
Example:
SELECT * INTO targetTable
FROM [sourceserver].[sourcedatabase].[dbo].[sourceTable]
If it’s only copying tables then linked servers will work fine or creating scripts but if secondary table already contains some data then I’d suggest using some third party comparison tool.
I’m using Apex Diff but there are also a lot of other tools out there such as those from Red Gate or Dev Art...
Third party tools are not necessary of course and you can do everything natively it’s just more convenient. Even if you’re on a tight budget you can use these in trial mode to get things done….
Here is a good thread on similar topic with a lot more examples on how to do this in pure sql.
SQL Server(2012) provides another way to generate script for the SQL Server databases with its objects and data. This script can be used to copy the tables’ schema and data from the source database to the destination one in our case.
Using the SQL Server Management Studio, right-click on the source database from the object explorer, then from Tasks choose Generate Scripts.
In the Choose objects window, choose Select Specific Database Objects to specify the tables that you will generate script for, then choose the tables by ticking beside each one of it. Click Next.
In the Set Scripting Options window, specify the path where you will save the generated script file, and click Advanced.
From the appeared Advanced Scripting Options window, specify Schema and Data as Types of Data to Script. You can decide from here if you want to script the indexes and keys in your tables. Click OK.
Getting back to the Advanced Scripting Options window, click Next.
Review the Summary window and click Next.
You can monitor the progress from the Save or Publish Scripts window. If there is no error click Finish and you will find the script file in the specified path.
SQL Scripting method is useful to generate one single script for the tables’ schema and data, including the indexes and keys. But again this method doesn’t generate the tables’ creation script in the correct order if there are relations between the tables.
Microsoft SQL Server Database Publishing Wizard will generate all the necessary insert statements, and optionally schema information as well if you need that:
http://www.microsoft.com/downloads/details.aspx?familyid=56E5B1C5-BF17-42E0-A410-371A838E570A
Generate the scripts?
Generate a script to create the table then generate a script to insert the data.
check-out SP_ Genereate_Inserts for generating the data insert script.
Create the database, with Script Database as... CREATE To
Within SSMS on the source server, use the export wizard with the destination server database as the destination.
Source instance > YourDatabase > Tasks > Export data
Data Soure = SQL Server Native Client
Validate/enter Server & Database
Destination = SQL Server Native Client
Validate/enter Server & Database
Follow through wizard

Resources