Interviews6 answers
How to insert ONLY HEADER of one table into another table?
6 Answers
u complete structure of table we can use copy script
Shashidhar RPosted Oct 26, 2024, 5:31 AM EDT
select *into new_table_name from tablename
where 2=0
Shashidhar RPosted Oct 26, 2024, 5:31 AM EDT
I think question is interesting but not framed correctly. Please find below my query I used for this question and as per your soluction, question should be “How to create table with same column without using create table command”.
Please take it positvely and your question is really interesting, it drew my attention :).
Regards,
Ekta Pandey Pareta
Ekta PandeyParetaPosted Feb 27, 2020, 11:41 PM EST
SELECT C.NAME INTO #TEST FROM SYS.COLUMNS C JOIN SYS.OBJECTS O ON C.OBJECT_ID = O.OBJECT_ID WHERE O.NAME = ‘YOURTABLENAME’
SELECT * FROM #TEST
Ekta PandeyParetaPosted Feb 27, 2020, 11:37 PM EST
First we create a table with only header , no data should be inserted. After that by using insert into command we can move the header of first table to second table.
create table table1 ( Id int, name varchar(10))
Go
Select * into table2 from table1 where 1=2
Mamta RajpootPosted Oct 27, 2018, 1:04 AM EDT
We can do so by adding a condition at the end of the T-SQL
CREATE TABLE TEST_WF (ID INT, NAME VARCHAR(5))
INSERT INTO TEST_WF VALUES
(1,'A'),(2,'B'),(3,'B'),(4,'C'),(5,'C'),(6,'C')
SELECT * INTO TEST
FROM TEST_WF WHERE 1=2
By adding the false condition or the condition which can not be true, we can insert Headers only from one table to another table.
Rohit KainthPosted Oct 9, 2018, 2:59 AM EDT
