Click here to Skip to main content
11,935,053 members (51,177 online)
Rate this:
Please Sign up or sign in to vote.
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
Posted 19-Feb-13 21:21pm
sayedfarhan 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
sayedfarhan 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try This

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

Hera 'ABC' and 'PQR' are Default value.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100