SQL query to create a clone of existing table without using Create Command.

Amatya
Posted by Amatya under Sql Server category on | Points: 40 | Views : 292
SQL query to create a clone of existing table without using Create Command.

Select Top 8 * INTO tblDNFRec_Temp From tblDNFRec


Thanks
Amatya

Comments or Responses

Posted by: Aruntuteja on: 9/27/2017 Level:Starter | Status: [Member] | Points: 10
Hi Amatya,

what is use of "Top 8" here in above query?

I think it will return top 8 records of table. But you are creating clone of the table. And, clone should have all records of source table.
Posted by: Amatya on: 9/27/2017 Level:Silver | Status: [Member] | Points: 10
Sure Aruntuteja "I think it will return top 8 records of table." and the clone will be created(structure).
Data will not be same here. If you want same data we have copy functionality.
if any doubt, please ask .

Thanks & Regards,
Amatya
Posted by: Aruntuteja on: 9/28/2017 Level:Starter | Status: [Member] | Points: 10
Hi Amatya,

Your query is creating new table "tblDNFRec_Temp". And that new table have 8 records of old table "tblDNFRec". This is happening because select query is returning top 8 records.

If you want to create new table only, then your select query should return 0 records. And, that you can achieve this by just putting where clause in select query like "where 1 = 2". This where clause will always return false and hence no record will be returned. Thus only clone table will be created without any records.

Thanks...
Posted by: Amatya on: 9/28/2017 Level:Silver | Status: [Member] | Points: 10
Yea Aruntuteja.. Now you got it.

Thanks
Amatya
Posted by: Aruntuteja on: 9/28/2017 Level:Starter | Status: [Member] | Points: 10
Hi,

So, please change and update the query used in this post so that other new readers get the correct query for achieving the mentioned clone table only (without any record).

Login to post response