Re: Inserting new rows that use divided values
From: Mary Bray (no_at_spam.com)
Date: 10/20/04
- Next message: AQ Mahomed: "View Data Differently"
- Previous message: Anuradha: "CHAR INDEX ON text data type"
- In reply to: bfwcom: "Inserting new rows that use divided values"
- Next in thread: bfwcom: "Re: Inserting new rows that use divided values"
- Reply: bfwcom: "Re: Inserting new rows that use divided values"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 20 Oct 2004 20:13:33 +1000
do it with an insert ... select eg:
INSERT INTO dbo.order_cost (cost_each, cost_tax, cost_ship)
select cost_each / total_cost, cost_tax / total_cost, cost_ship / total_cost
from dbo.order_cost
where ship_code = 1
With an insert statement you use the Values key word if you have variables
or exact values, but use Select to insert rows from a query.
-- ------ Mary Bray [SQL Server MVP] Please only reply to newsgroups "bfwcom" <bfwcom@discussions.microsoft.com> wrote in message news:35650B1C-D11D-48FD-96ED-7E7FB5722DB6@microsoft.com... >I am having a problem with syntax on an INSERT INTO statement. I may be >using > the wrong statement to perform the task, but am not an expert and am still > trying to learn the right way to write SQL. I have created a table and > added > it to an existing database. Now I need to perform some work. > > First my table: > > CREATE TABLE dbo.order_cost > (ship_code integer IDENTITY(1,1) NOT NULL, > cost_each numeric(2, 2) NOT NULL, > cost_tax numeric(2, 2) NOT NULL , > cost_ship numeric(2, 2) NOT NULL, > total_cost numeric(3, 2) NULL, > PRIMARY KEY (ship_code)) > GO > > Next I create a relationship with another table (order_lines) in the > database and insert data into the table: > > ALTER TABLE order_lines ADD ship_code integer, > CONSTRAINT orderlines2ordercost > FOREIGN KEY (ship_code) > REFERENCES order_cost; > GO > > INSERT INTO dbo.order_cost (ship_code, cost_each, cost_tax, cost_ship) > VALUES (1, 15.95, 1.89, 12.75); > GO > > Then I put a value in the last column based on the sum of 3 other fields: > > UPDATE order_cost > SET total_cost = (cost_each + cost_tax + cost_ship) > WHERE ship_code = 1; > > Here's where I am having a problem- I need to insert another row that > divides the values in each of the fields from row one by the value of the > last field in row one: > > INSERT INTO dbo.order_cost (cost_each, cost_tax, cost_ship) > VALUES (cost_each / total_cost, cost_tax / total_cost, cost_ship / > total_cost) > GO > > I get this error: > > Server: Msg 128, Level 15, State 1, Line 4 > The name 'cost_each' is not permitted in this context. Only constants, > expressions, or variables allowed here. Column names are not permitted. > > I know it can be done, but cannot find any reference to help me with the > correct syntax. this should be easy, but it has me banging my head.
- Next message: AQ Mahomed: "View Data Differently"
- Previous message: Anuradha: "CHAR INDEX ON text data type"
- In reply to: bfwcom: "Inserting new rows that use divided values"
- Next in thread: bfwcom: "Re: Inserting new rows that use divided values"
- Reply: bfwcom: "Re: Inserting new rows that use divided values"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|