EDIT ........ Dynamic SQL Generation added
This code will generate INSERT statements to INSERT from #TEMP into #TEMP. You can tweak it to suit your purpose if you are going from #temp to regular tables.
SET NOCOUNT ON
DROP Table #TempTable1
DROP Table #TempTable2
GO
DROP Function GenerateInserts
GO
Create Function GenerateInserts
(
@SourceTable VarChar (100),
@DestinationTable VarChar (100)
)
Returns VarChar (MAX)
AS
BEGIN
DECLARE @SelectString VarChar (MAX)
DECLARE @InsertString VarChar (MAX)
DECLARE @SQLString VarChar (MAX)
DECLARE @SourceColumnCount INTEGER
DECLARE @DestinationColumnCount INTEGER
DECLARE @ColumnCount INTEGER
DECLARE @Counter INTEGER
SELECT @SourceColumnCount = COUNT (*)
FROM tempdb..syscolumns
WHERE id=object_id(@SourceTable)
SELECT @DestinationColumnCount = COUNT (*)
FROM tempdb..syscolumns
WHERE id=object_id(@DestinationTable)
SET @ColumnCount = @SourceColumnCount
IF @DestinationColumnCount < @ColumnCount
SET @ColumnCount = @DestinationColumnCount
SET @Counter = 0
SET @SelectString = ' INSERT INTO ' + @DestinationTable + ' '
SET @InsertString = ' INSERT INTO ' + @DestinationTable + ' '
SET @SelectString = ''
SET @InsertString = ''
WHILE @Counter <= @ColumnCount
BEGIN
SELECT @SelectString = @SelectString + ', ' + Name
FROM TempDB..SysColumns
WHERE Id = Object_Id (@SourceTable)
AND ColOrder = @Counter
SELECT @InsertString = @InsertString + ', ' + Name
FROM TempDB..SysColumns
WHERE Id = Object_Id (@DestinationTable)
AND ColOrder = @Counter
SET @Counter = @Counter + 1
END
SET @InsertString = 'INSERT INTO ' + @DestinationTable + ' (' + STUFF ( @InsertString, 1, 2, '') + ') '
SET @SelectString = 'SELECT ' + STUFF ( @SelectString, 1, 2, '') + ' FROM ' + @SourceTable
SET @SQLString = @InsertString + '
'+ @SelectString
RETURN @SQLString
END
GO
Create Table #TempTable1
(
Col1 VarChar (10),
Col2 VarChar (10),
Col3 VarChar (10),
Col4 VarChar (10),
Col5 VarChar (10)
)
Create Table #TempTable2
(
MyCol1 VarChar (10),
MyCol2 VarChar (10),
MyCol3 VarChar (10),
MyCol4 VarChar (10),
MyCol5 VarChar (10),
MyCol6 VarChar (10)
)
SELECT dbo.GenerateInserts ('tempdb..#TempTable1', 'tempdb..#TempTable2')
OLD ANSWER
Yes you can do this but you have to write different statements for each type of INSERT
. You do have to specify column names in both places - the INSERT INTO
and the SELECT
If you have the same number of columns in your Source and Destination tables, do this
INSERT INTO Table1 (Column1, Column2, Column3)
SELECT MyColumn01, MyColumn02, MyColumn03
FROM MyTable
What this will do is map as follows:
MyTable.MyColumn01 -> Table1.Column1
MyTable.MyColumn02 -> Table1.Column2
MyTable.MyColumn03 -> Table1.Column3
If the Source has less columns, you can use a NULL value in place of the column name
INSERT INTO Table1 (Column1, Column2, Column3)
SELECT MyColumn01, MyColumn02, NULL AS MyColumn03
FROM MyTable
OR you can just use two column names
INSERT INTO Table1 (Column1, Column2)
SELECT MyColumn01, MyColumn02
FROM MyTable
If the destination table has less columns than the source, then you have to ignore columns from the source
INSERT INTO Table1 (Column1, Column2, Column3)
SELECT MyColumn01, MyColumn02, NULL AS MyColumn03 /* MyColumn04, MyColumn05 are ignored */
FROM MyTable