Click here to Skip to main content
15,174,684 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two table say table1 and table2 all the columns in both table are same but table2 has some extra column in which i want to insert a some default value and name of table is generated runtime
sayed farhan 20-Feb-13 3:35am
thanx susheel for answering it.
but this is not working my both table is present in database and this is creating a new table
sayed farhan 20-Feb-13 3:40am
table is present in database and table name is genrating runtime i.e if firt time table name is table1 and table2 next time table can be table3 and table4 which has different fields
gvprabu 20-Feb-13 7:00am
You need to frame Dynamic SQL Query and Execute as per your requirements.

Try This

select *,'ABC' as NewColumn1,'PQR' as NewColumn2 into Table2 from Table1

Hera 'ABC' and 'PQR' are Default value.
Hi Sayed,

Check the following Script
-- Variable Declaration 
DECLARE @TabName VARCHAR(100)='TestTable2', @ExtColumnDtls VARCHAR(1000)='M1 INT, M2 INT, M3 INT, Total INT',
    @SqlString VARCHAR(2000)
-- Source Table Creations
INSERT INTO TestTable1 (SNO, Name) VALUES(1,'A'),(2,'B'),(3,'C')

-- Generate SQL Statement
SELECT @SqlString = 'SELECT * INTO '+ @TabName +' FROM TestTable1 GO ALTER TABLE '+@TabName +' ADD '+@ExtColumnDtls
PRINT @SqlString
EXEC (@SqlString)

SELECT @SqlString='', @SqlString='SELECT * FROM '+@TabName
EXEC (@SqlString)


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900