Link to home
Start Free TrialLog in
Avatar of brohjoe
brohjoeFlag for United States of America

asked on

Appending a Query Result into existing Table

I'd like to know how to place the resultset of a query into an existing table in SQL Server.
Avatar of Reza Rad
Reza Rad
Flag of New Zealand image

use this syntax:
insert into destinationtable(field1,field2,...)
select field1,field2,...
from ...
where ...
Avatar of brohjoe

ASKER

Ok, the result column is 'Col_Result.'  So my syntax is,

"Insert into desinationTable(Col_Result)
Select Col_Result
From ???

I don't understand the select statement.  'Col_Result' is in SQL Server's 'Result' window.  How do I address that with the select statement?  Or do you mean something else?  Please clarify.

let me know what is your select statement exactly? and what is your destination table structure?
Do you have non-NULL columns in your destinationTable? If yes, you need to pass a non-NULL value to those columns in the SELECT clasue and you need to include those columns in the columns list in the INSERT statement.

Insert into desinationTable(Col_Result)
Select Col_Result
From Source_Table
Avatar of brohjoe

ASKER

How do you reference the source table when it is a query result?   I guess what I'm trying to create is a derived column that is added to an existing table.  

The exact query is listed here.  The query resultset is the result of a combination of other columns in the queried table.  I want to add the results of the query into the table as a new table.
USE [master]

GO

Select Case 
	When  
		Bklog_Qty + Week_1 + midpoint - Avail_Inv_Qty <= 0 Then 0

	Else
		Bklog_Qty + Week_1 + midpoint - Avail_Inv_Qty

	End As 
		ThisWeek1

	From
		Demand

Open in new window

INSERT INTO DESTINATION TABLE( Col_Result )
Select Case
      When  
            Bklog_Qty + Week_1 + midpoint - Avail_Inv_Qty <= 0 Then 0

      Else
            Bklog_Qty + Week_1 + midpoint - Avail_Inv_Qty

      End As
            ThisWeek1

      From
            Demand
try like this
USE [master]

GO

Select Case 
	When  
		Bklog_Qty + Week_1 + midpoint - Avail_Inv_Qty <= 0 Then 0

	Else
		Bklog_Qty + Week_1 + midpoint - Avail_Inv_Qty

	End As 
		ThisWeek1
    Into DestinationTable
	From
		Demand

select * from DestinationTable

Open in new window

Avatar of brohjoe

ASKER

Correction, I want to add the query resultset into the existing table, not a new table.
previous sql statement , will insert into an existing table,
if you want to put them in a NEW TABLE , do this:
Select Case
      When  
            Bklog_Qty + Week_1 + midpoint - Avail_Inv_Qty <= 0 Then 0

      Else
            Bklog_Qty + Week_1 + midpoint - Avail_Inv_Qty

      End As
            ThisWeek1
INTO NewTable
      From
            Demand
Avatar of brohjoe

ASKER

This code only provides a resultset in the bottom window of SQL Server.  It does not add the column to the 'Demand' table:

Select Case
      When  
            Bklog_Qty + Week_1 + midpoint - Avail_Inv_Qty <= 0 Then 0

      Else
            Bklog_Qty + Week_1 + midpoint - Avail_Inv_Qty

      End As
            ThisWeek1

      From
            Demand

If I replace, 'From Demand' with 'INTO Demand', I get invalid column errors.
Do you want to add new column to the existing Demand table or do you want to insert the result of your query to an existing table?
Avatar of brohjoe

ASKER

Add a new column (ThisWeek1) into the existing Demand Table.
ASKER CERTIFIED SOLUTION
Avatar of Reza Rad
Reza Rad
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brohjoe

ASKER

Thanks, that did the trick.