Need to create a new table from existing table

Diboliya

I want to create a new table with existing table, where the table names should to pass from input parameters. I am trying the following code.

DECLARE @oldTableName nvarchar(50)
DECLARE @newStagingTableName nvarchar(50)
SET @oldTableName='OldTableName'
SET @newStagingTableName ='NewTableName';
SELECT * INTO @newStagingTableName FROM @oldTableName WHERE 1 = 0;  

The SQL server is giving error while parsing this query.

Ashish Sapkale

Could you please try below dynamic SQL query?

DECLARE @oldTableName nvarchar(50)
DECLARE @newStagingTableName nvarchar(50)

SET @oldTableName='OldTableName'
SET @newStagingTableName ='NewTableName'

DECLARE @sqlquery nvarchar(100) = 'SELECT * INTO ' + @newStagingTableName + ' FROM ' + @oldTableName
exec(@sqlquery)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create a new table from two existing table

From Dev

Create a new table from two existing table

From Dev

Create new hive table from existing external portioned table

From Dev

Create new hive table from existing external portioned table

From Dev

Create a new table based on existing table

From Dev

Create New MySQL table with structure of existing table

From Dev

laravel - Create a new migration table with data from existing

From Dev

Create a new table (copy of an existing) with date order

From Dev

How to create a new table from existing table with both keys and data in SQL Server 2012 (Management Studio)?

From Dev

How to create new field in view with some offset (field of table) from existing field (another field of table)?

From Dev

Word 2013 - How to create a new table style from an existing table style?

From Dev

insert into new table from existing table with an added new field

From Dev

Adding new data from JSON to existing table

From Dev

Papyrus - Create a Table from an existing configuration

From Dev

Create an HTML table from an existing array

From Dev

Creating a new table in database with description from existing table as an input

From Dev

Create empty SQL table from existing table in Azure

From Dev

Create empty SQL table from existing table in Azure

From Dev

How to create table from existing 2 table with default value in oracle

From Dev

Create new table from old table and add 2 new columns

From Dev

Cassandra - Import from CSV and create new table?

From Dev

SQL: Create new table from old

From Dev

Is it possible to create a new table from an unpivot results?

From Dev

MySQL: create new table with frequencies based on data from original table

From Dev

SAP HANA create table / insert into new table from select

From Dev

Displaying values in row of table from existing item in new row

From Dev

Oracle: Inserting a row using values from existing table and new values

From Dev

Add new column and assign values from existing table

From Dev

A new table in R based on fields and data from existing one

Related Related

  1. 1

    Create a new table from two existing table

  2. 2

    Create a new table from two existing table

  3. 3

    Create new hive table from existing external portioned table

  4. 4

    Create new hive table from existing external portioned table

  5. 5

    Create a new table based on existing table

  6. 6

    Create New MySQL table with structure of existing table

  7. 7

    laravel - Create a new migration table with data from existing

  8. 8

    Create a new table (copy of an existing) with date order

  9. 9

    How to create a new table from existing table with both keys and data in SQL Server 2012 (Management Studio)?

  10. 10

    How to create new field in view with some offset (field of table) from existing field (another field of table)?

  11. 11

    Word 2013 - How to create a new table style from an existing table style?

  12. 12

    insert into new table from existing table with an added new field

  13. 13

    Adding new data from JSON to existing table

  14. 14

    Papyrus - Create a Table from an existing configuration

  15. 15

    Create an HTML table from an existing array

  16. 16

    Creating a new table in database with description from existing table as an input

  17. 17

    Create empty SQL table from existing table in Azure

  18. 18

    Create empty SQL table from existing table in Azure

  19. 19

    How to create table from existing 2 table with default value in oracle

  20. 20

    Create new table from old table and add 2 new columns

  21. 21

    Cassandra - Import from CSV and create new table?

  22. 22

    SQL: Create new table from old

  23. 23

    Is it possible to create a new table from an unpivot results?

  24. 24

    MySQL: create new table with frequencies based on data from original table

  25. 25

    SAP HANA create table / insert into new table from select

  26. 26

    Displaying values in row of table from existing item in new row

  27. 27

    Oracle: Inserting a row using values from existing table and new values

  28. 28

    Add new column and assign values from existing table

  29. 29

    A new table in R based on fields and data from existing one

HotTag

Archive