Backup SQL Schema Only? - sql

I need to create a backup of a SQL Server 2005 Database that's only the structure...no records, just the schema. Is there any way to do this?
EDIT: I'm trying to create a backup file to use with old processes, so a script wouldn't work for my purposes, sorry

Use a 3 step process:
Generate a script from the working database
Create a new database from that script
Create a backup of the new database

Why not just use SQL Management Studio to create a complete script of your database and the objects?

Toad for SQL Server does this nicely, if you're considering a commercial product.

I make heavy use of this tool:
SQLBalance for MySQL
Unfortunately; its windows only... but works like a charm to move databases around, data or no data, merge or compare.

As of SQL Server 2012 (patched), you can make a schema only clone of your database using DBCC CLONEDATABASE. Then simply backup the clone.
dbcc clonedatabase(Demo, Demo_Clone) with verify_clonedb;
alter database [Demo_Clone] set read_write;
backup database [Demo_Clone] to disk = N'C:\temp\Demo_SchemaOnly_20220821.bak';
drop database [Demo_Clone];
Read more here: Schema Only Database Backup

Related

How to restore a SQL Backup .bak file on a different (new) server? SQL 2012

I remember myself restoring a db on linux side. I used mysql dump and before I could restore this backup on the other server I had to create a DB with the same name.
Now I am going to switch the server on windows side using SQL 2012. I am backupping many SQL DB's and call them for now db1.bak , db2.bak...
When I now want to restore them on the new server, do I need to create a "structure" first with the same DB names or can I simply restore my DB's with the restore command one by one.
Is there anything else I should prepare? Thanks
A SQL Server database backup contains the structure and the data, so if you have run a full backup on one SQL Server 2012 server, you can restore this onto another SQL Server 2012 + server instance without having to create an empty database first.

create sql server database as other sql database structure (design)

I need to create sql database just like other sql database (without the data) via sql script
some one told me that Oracle has this ability by some code like
create database <your new DB name> as <the old DB name>
so is there a similar statement or workaround in SQL
I use MS-SQL Server 2008/2008R2
I don't want the 'generate scrip' solution as this provide a very long script, I just need the Oracle statement (mentioned above) but in SQL
to be more clear I need the tables structure only (no data) and need all other objects such as functions, views etc...
Please advice,
In SQL Server, you can right click on the database then select "Tasks" then "Generate Scripts" and you can build one script for all your object sans data.
If you are asking about SQL Server (at least in 2005 or newer, not sure about previous versions) if you right click on a Database in SQL Server Management Studio Go To Tasks and then Generate Scripts you have the option to Generate a script to create all the object ins the Database w/o Data.
Update:
You can also right click on a User Database and select Script Database as -> Create To just to get the TSQL to create the DB itself without any of the tables, stored procedure etc.

SQL Server export, back-up without to stop the server

Can anyone tell me how can I make back-up to a database to to export it to a different database (SQL Server or mysql) without to stop the server ?
Sql Server to Sql Server
Get a copy of your database: Backup Database
Restore a copy of your database: Restore Database
--when connected to source db instance
BACKUP DATABASE AdventureWorks2008R2
TO DISK = 'Z:\SQLServerBackups\AdvWorksData.bak'
--when connected to target db instance
RESTORE DATABASE AdventureWorks2008R2
FROM DISK = 'Z:\SQLServerBackups\AdvWorksData.bak'
Sql Server to MySQL
As far as I know, there aren't any standard methods for this. It's a by hand, or by hand written program. If I were tasked with this, I would try : SSMS, select a database, export data- source = sql server instance. If there is a my sql destination available, maybe that will create the table structure for you automajically. Else, I'm betting
- you'll have to create the mysql schema, then pump the data over yourself
- or there is some 3rd party tool someone is happy to sell you to assist you in the task.

Efficiently moving large sets of data between SQL Server tables?

I have a rather large (many gigabytes) table of data in SQL Server that I wish to move to a table in another database on the same server.
The tables are the same layout.
What would be the most effecient way of going about doing this?
This is a one off operation so no automation is required.
Many thanks.
If it is a one-off operation, why care about top efficiency so much?
SELECT * INTO OtherDatabase..NewTable FROM ThisDatabase..OldTable
or
INSERT OtherDatabase..NewTable
SELECT * FROM ThisDatabase..OldTable
...and let it run over night. I would dare to say that using SELECT/INSERT INTO on the same server is not far from the best efficiency you can get anyway.
Or you could use the "SQL Import and Export Wizard" found under "Management" in Microsoft SQL Server Management Studio.
I'd go with Tomalak's answer.
You might want to temporarily put your target database into bulk-logged recovery mode before executing a 'select into' to stop the log file exploding...
If it's SQL Server 7 or 2000 look at Data Transformation Services (DTS). For SQL 2005 and 2008 look at SQL Server Integration Services (SSIS)
Definitely put the target DB into bulk-logged mode. This will minimally log the operation and speed it up.

Cross-server SQL

I want to port data from one server's database to another server's database.
The databases are both on a different mssql 2005 server.
Replication is probably not an option since the destination database is generated from scratch on a [time interval] basis.
Preferebly I would do something like
insert *
from db1/table1
into db2/table2
where rule1 = true
It's obvious that connection credentials would go in somehwere in this script.
I think what you want to do is create a linked server as per this webarchive snapshot of msdn article from 2015 or this article from learn.microsoft.com. You would then select using a 4 part object name eg:
Select * From ServerName.DbName.SchemaName.TableName
You can use Open Data Source Like this :
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO
SELECT *
FROM OPENDATASOURCE('SQLOLEDB',
'Data Source=<Ip Of Your Server>;
User ID=<SQL User Name>;Password=<SQL password>').<DataBase name>.<SchemaName>.<Table Or View Name>
Go
Are SQL Server Integration Services (SSIS) an option? If so, I'd use that.
Would you be transferring the whole content of the database from one server to another or just some data from a couple of tables?
For both options SSIS would do the job especially if you are planning to to the transfer on a regular basis.
If you simply want to copy some data from 1 or 2 tables and prefer to do it using TSQL in SQL Management Studio then you can use linked server as suggested by pelser
Set up the source database server as a linked server
Use the following syntax to access data
select columnName1, columnName2, etc from serverName.databaseName.schemaName.tableName
Well I don't agree with your comment on replication. You can start a replication by creating a database from scratch, and you can control either the updates will be done by updating the available client database or simply recreating the database.
Automated replication will ease your work by automatically managing keys and relations.
I think the easiest thing to do is to start a snapshot replication through MSSQL Server Studio, get the T-SQL corresponding scripts (ie the corresponding T-SQL instructions for both publication and subscriptions), and record these scripts as part of a job in the Jobs list of the SQL Agent or as a replication job in the replications folder.
You could go the linked server route.
you just can't use the select * into you have to do an insert into select.
I would avoid replication if you don't have experience with it as it can be difficult to fix if it breaks and can be prone to other problems if not properly managed.
Keep it simple especially if the databases are small.
Can you use the Data Transformation Services to do the job? This provides all sorts of bolt-together tools for doing this kind of thing.
You can download the SQL Server 2005 feature pack from Microsoft's website
here
CREATE VIEW newR1
AS
SELECT * from OPENQUERY ([INSTANCE_NAME], 'select * from DbName.SchemaName.TableName')