brohjoe
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.
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.
"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
Insert into desinationTable(Col_Result
Select Col_Result
From Source_Table
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.
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
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
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
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
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
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.
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?
ASKER
Add a new column (ThisWeek1) into the existing Demand Table.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, that did the trick.
insert into destinationtable(field1,fi
select field1,field2,...
from ...
where ...