Take complete backup of table with index and keys in SQL Server - sql-server

I have a table (i.e. tbl_test) with 46 index and 42 keys.
I have taken backup via this command:
select *
into tbl_test_bkp_18112019
from tbl_test
But it is unable to copy index and keys in the backup table. I want to take a complete backup of the table with index and keys. Please suggest how.

Select * into - Does not copy indexes, constraints, etc.
You should generate a script from SQL server management studio by right-clicking on the desired table -> Script table as -> create to -> New query windows.
Change the table name as needed and run the script.
This will create a new table with the same structure, indexes, constraints, etc.
If you need data as well then you can insert from the orignal table to this new table using insert command
Insert newtablename select columns from oldtablename

You can Try Task > Generate Script and select only table with data and check in SQL Management Studio

Related

SELECT INTO TARGET_TABLE on specific FILEGROUP

I am trying to clone a table's data structure to another table programmatically. As a start I tried to copy the schema of the table using the following query
SELECT * INTO TARGET_TABLE FROM SOURCE_TABLE WHERE 1<>1
But for my situation I specifically need the newly created table to be in a specific FILEGROUP only. something like the below
SELECT * INTO TARGET_TABLE ON [NEW_FILEGROUP] FROM SOURCE_TABLE WHERE 1<>1
I know the above line will work starting with SQL Server 2017 version. However whatsoever app I'm trying to make, should support from SQL Server 2016. What is the closest/easiest thing I can make use of in this case?
The real struggle was to get it done programmatically.
Till sql-server 2016 SP1, mentioning a filegroup in select into statement is not possible. As a work around solution, what I did to sort this out is that I've added an Unique clustered index to the newly created table. The advantage is that I can mention the filegroup while creating an index
SELECT * INTO TARGET_TABLE FROM SOURCE_TABLE WHERE 1<>1
CREATE UNIQUE CLUSTERED INDEX [target_table_clustered_idx] ON TARGET_TABLE
(
unique_column ASC
) ON [NEW_FILEGROUP]
Since this is a clustered index, the db engine will move the entire table and the index to the [NEW_FILEGROUP]. Hence solved
#Beingnin . Right click on the table then click on "Script Table as " then Click on " CREATE To " then "New Query Editor Window" Like Below Picture. then you can get
Complete script of table.

Cloning a table without importing row data

I need to make a provisionary table (TAB_PROV). This table will have its origin in a main table (TAB_MAIN). I need everything from the main table, except the data (rows).
I searched for some examples and none of them worked for me
CREATE TABLE TAB_PROV LIKE TAB_MAIN
CREATE TABLE TAB_PROV AS TAB_MAIN
You can just simply do:
SELECT *
FROM TAB_MAIN
INTO TAB_PROV
WHERE 1 = 2
Since the WHERE condition will never be true, no data is ever copied - but the table structure is replicated - TAB_PROV is created and has the same column as TAB_MAIN. This does NOT however copy any constraints (check or default constraints) or triggers over - it only recreates the columns (and their datatypes).
If you want a real and complete "copy" of your table, then you should use the "Script Table" function in SSMS to get the SQL needed for TAB_MAIN and then adapt it to create TAB_PROV from it.
In SQL Server Management Studio, you can right-click the table and select
Script Table as -> Create To -> New Query Editor Window
This will create just the table creation script for you.
You can also try the code below but it will copy everything.
SELECT *
INTO NewTable
FROM OldTable
TRUNCATE TABLE NewTable

view created table in microsoft sql server

I am running part of a query in Microsoft SQL server management studio
Select Table1.Column1
into #Table2
from Table1
now it has created the table but I actually want to view this table with my eyes but I cannot seem to find where the table is stored. Please could someone help me find it?
That is a temporary table. It will be created in the tempdb system database and you can see it by going to tempdb -> Temporary Tables.
Any tables where its name start with # is a Temporary Table. Exactly as the name suggests, it's temporary, and only exists for the same time the connection that created it does (or it is dropped).
If you want to view the data from a temporary table, you would do so like any other table SELECT * FROM #Table2;. .
I imagine what your really after is to not use a temporary table, so drop the # from the name, and the new table will be created in the database you are connected to.

SQL Server - Create Copy of Multiple Tables in Same Database

I need to duplicate several tables in a single database. Basically we have an old dot net nuke portal that needs to be split and our host limits us to a single database. So we just want to take the existing tables but copy them with an exact duplicate but different table prefix name (e.g. EEPTL_Users to JBPTL_Users).
I can use 'script to - create' to make the proper table structure but how do I do the proper inserts when I need to specify the primary keys but want the column to be an auto-incrementing identiy?
Is there a tool for doing what I need?
SET IDENTITY_INSERT [table name] ON
INSERTS
SET IDENTITY_INSERT [table name] OFf

What is the equivalent of 'CREATE TABLE ... LIKE ..." in SQL Server

I am working with SQL Server (I am a SQL Server noob) and trying to alter a table. I want to CREATE TABLE LIKE to safely store data while I drop keys and constraints and all the other rigamorole that SQL Server seems to require when altering on the original table but I have not been able to find a match to that command...
you want to recreate the same structure?
how about this
SELECT *
into test
FROM myRealTable
where 0=1
no data will be inserted into the new table
You can do
SELECT * INTO #MyTable_tmp FROM MyTable
Then modify your MyTable, and copy your data back in. Other approaches I've seen is to create a new table call it Mytable_Tmp (Not a temp table), which will be your new table.
Then copy your data doing any migrations you need. Then you will drop the original table and do a rename on Mytable.
Or you can get one of the many excellant tools that compare databases and generate difference scripts or VSTS DB Edition (Comes with developer) and you can do a diff script from a project file to a DB.
Edit
When you run SELECT * INTO #MyTable FROM MyTable, SQL Server creates a new temporary table called #MyTable that matches each column and data type from your select clause. In this case we are selecting * so it will match MyTable. This only creates the columns it doesn't copy defaults, constraints indexes or anything else.

Resources