HOW TO CREATE A NEW TABLE FROM AN EXISTING TABLE IN SQL | SQL Interview Question | IQBees

Опубликовано: 02 Июнь 2021
на канале: IQBees
915
125

Hello Friends,

This video talks about :- HOW TO CREATE A NEW TABLE FROM AN EXISTING TABLE IN SQL using SQL Query. This question is mostly asked by the interviewer.

HOW TO COPY JUST TABLE STRUCTURE OR CREATE A TABLE FROM ANOTHER TABLE WITHOUT COPYING ANY VALUES FROM THE OLD TABLE USING SELECT QUERY?

Syntax :
Select *
into NewTableName
From ExistingTableName
Where 1=0

This would create a new table that includes all columns from the existing table, but no data from the existing table.

HOW TO CREATE A TABLE FROM ANOTHER TABLE WITH VALUES FROM THE OLD TABLE USING SELECT QUERY?

Syntax :
Select *
into NewTableName
From ExistingTableName

Note : that this will not copy indexes, keys, etc.

For Identity column, we can easily skip that by giving the column name & add 0 to identity column.

SELECT ID+0 AS ID, RESTCOLUMNNAME INTO NEWTABLENAME FROM OLDTABLENAME WHERE 1=2

OR
WE CAN USE CAST

SELECT CAST(ID AS INT) AS ID, RESTCOLUMNNAME INTO NEWTABLENAME FROM OLDTABLENAME WHERE 1=2


If you need personal help in SQL, Mock interviews, You can contact me

Regards,
IQBees

#sqlInterviewQuestions #SelectInto #sql #sqlserver

1. Copy table data using Select Into in sql server
2. Select into in sql
3. copy table from One Database to Another using sql server management studio
4. Inserting data Into a table from another table in Sql server
5. copy data from one table to another table in sql