Home Forums SQL Server 2005 T-SQL (SS2K5) Insert data into single table from multiple tables RE: Insert data into single table from multiple tables

  • 1.If your new column is static then

    declare @newColID int, @PQID int

    Set @newColID = 1

    set @PQID =1234

    Insert into resultTB( [PQID] , [col1] , [col2], [newColID])

    select PQID,col1,col2, @newColID

    from TB1 where TB1.[PQID] = @PQID

    2.If your new column resides in another table(TB2)

    If your new column is in a different table (related to TB1 through PQID)

    Insert into resultTB( [PQID] , [col1] , [col2], [newColID])

    select TB1.PQID,TB1.col1,TB1.col2, TB2.newColID

    from TB1 INNER JOIN TB2 ON TB1.PQID = TB2.PQID

    where TB1.[PQID] = @PQID

    Or

    Insert into resultTB( [PQID] , [col1] , [col2], [newColID])

    select TB1.PQID,TB1.col1,TB1.col2, TB2.newColID

    from TB1 , TB2

    where TB1.[PQID] = @PQID

    and TB1.PQID = TB2.PQID

    Hope this is what u are looking for.