Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL-server-2005
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
Comments
sayedfarhan at 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 at 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 at 20-Feb-13 7:00am
   
You need to frame Dynamic SQL Query and Execute as per your requirements.
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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
CREATE TABLE TestTable1 (SNO INT, Name VARCHAR(50))
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)
 
 
Regards,
GVPrabu
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 485
1 OriginalGriff 310
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 200
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2014
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