Append a table with the result of a select query - sql

I've created a SELECT query to return all of the records from my table WHERE FieldCount = 3.
I'd now like to append the result of this query to the same table with a new value of FieldCount.
How would you go about appending the results of a query to a table and altering the value of a field?

WHERE and SELECT clause are independent of each other. You can do it like this:
INSERT INTO myTable (foo, bar, FieldCount)
SELECT foo, bar, 77 As FieldCount
FROM myTable
WHERE FieldCount = 3

Create a new Query and base it off the query you just created off that table.
Make sure that the new query is an append query
Add all the fields to the query in design mode
For the new "FieldCount" number, Just place whatever number you want them all to be in the field combobox.
Notice the number 3 is the value I chose to add to the table.
Edit: Andre beat me to it, but his method is easier unless you prefer the GUI way of doing it.

Related

Insert a column from output of a query into already populated postgresql database without adding new rows

I have a table named comments_live which has 4 fields and some 24000 entries. I want to add another field which is the output of a select query. The query is like this:
INSERT INTO comments_live SELECT SUBSTR(record,1, POSITION(' ' IN record)) AS project_id FROM comments_live;
What this query is doing is it's appending the result of the SELECT query to the table. I want the 'project_id' field to be appended to the existing 24000 rows. i.e. The above query should not add those extra rows to the table.
Any help would be appreciated.
PS: I tried adding a empty column 'project_id' to the table first and then executing the query, still I'm getting the same result.
I conclude from your code example that you want to add a column whose content is the substring from the column RECORD. If you succeeded in adding the empty column, then you just need code like the following:
update comments_live
set project_id=substr(record,1,[...])

How to insert generated id into a results table

I have the following query
SELECT q.pol_id
FROM quot q
,fgn_clm_hist fch
WHERE q.quot_id = fch.quot_id
UNION
SELECT q.pol_id
FROM tdb2wccu.quot q
WHERE q.nr_prr_ls_yr_cov IS NOT NULL
For every row in that result set, I want to create a new row in another table (call it table1) and update pol_id in the quot table (from the above result set) with the generated primary key from the inserted row in table1.
table1 has two columns. id and timestamp.
I'm using db2 10.1.
I've tried numerous things and have been unsuccessful for quite a while. Thanks!
Simple solution: create a new table for the result set of your query, which has an identity column in it. Then, after running your query, update the pol_id field with the newly generated ID in your result table.
Alteratively, you can do it more manually by using the the ROW_NUMBER() OLAP function, which I often found convenient for creating IDs. For this it is convenient to use a stored procedure which does the following:
get the maximum old id from Table1 and write it into a variable old_max_id.
after generating the result set, write the row-numbers into the table1, maybe by something like
INSERT INTO TABLE1
SELECT ROW_NUMBER() OVER (PARTITION BY <primary-key> ORDER BY <whatever-you-want>)
+ OLD_MAX_ID
, CURRENT TIMESTAMP
FROM (<here comes your SQL query>)
Either write the result set into a table or return a cursor to it. Here you should either use the same ROW_NUMBER statement as above or directly use the ID from Table1.

Common methods for doing select with computation by need?

I would like to be able to add columns to a table with cells who's values are computed by need at 'querytime' when (possibly) selecting over them.
Are there some established ways of doing this?
EDIT: Okay I can do without the 'add columns'. What I want is to make a select query which searches some (if they exist) rows with all needed values computed (some function) and also fills in some of the rows which does not have all needed values computed. So each query would do it's part in extending the data a bit.
(Some columns would start out as null values or similar)
I guess I'll do the extending part first and the query after
You use select expression, especially if you don't plan to store the calculation results, or they are dependant on more than one table. An example, as simple as it could be:
SELECT id, (id+1) as next_id FROM table;
What type of database are you asking for? If it is SQL Server then you can use the computed columns by using the AS syntax.
Eg:
create table Test
(
Id int identity(1,1),
col1 varchar(2) default 'NO',
col2 as col1 + ' - Why?'
)
go
insert into Test
default values
go
select * from Test
drop table Test
In the SQL world it's usually expensive to add a column to an existing table so I'd advise against it. Maybe you can manage with something like this:
SELECT OrderID,
ProductID,
UnitPrice*Quantity AS "Regular Price",
UnitPrice*Quantity-UnitPrice*Quantity*Discount AS "Price After Discount"
FROM order_details;
If you really insist on adding a new column, you could go for something like (not tested):
ALTER TABLE order_details ADD column_name datatype
UPDATE order_details SET column_name = UnitPrice+1
You basically ALTER TABLE to add the new column, then perform an UPDATE operation on all the table to set the value of the newly added column.

SQL insert row with one change

I have this table:
Table1:
id text
1 lala
And i want take first row and copy it, but the id 1 change to 2.
Can you help me with this problem?
A SQL table has no concept of "first" row. You can however select a row based on its characteristics. So, the following would work:
insert into Table1(id, text)
select 2, text
from Table1
where id = 1;
As another note, when creating the table, you can have the id column be auto-incremented. The syntax varies from database to database. If id were auto-incremented, then you could just do:
insert into Table1(text)
select text
from Table1
where id = 1;
And you would be confident that the new row would have a unique id.
Kate - Gordon's answer is technically correct. However, I would like to know more about why you want to do this.
If you're intent is to have the field increment with the insertion of each new row, manually setting the id column value isn't a great idea - it becomes very easy for there to be a conflict with two rows attempting to use the same id at the same time.
I would recommend using an IDENTITY field for this (MS SQL Server -- use an AUTO_INCREMENT field in MySQL). You could then do the insert as follows:
INSERT INTO Table1 (text)
SELECT text
FROM Table1
WHERE id = 1
SQL Server would automatically assign a new, unique value to the id field.

get auto increment in query as a variable to use in another query

I am inserting into two tables. Into the first table goes a new 'item'. There are a lot of fields, but for simplicity I'll only show you what I need. This is my coldfusion page:
<cfquery>
INSERT INTO my_item_table (itemid, a bunch of other stuff)
VALUES (itemid_sequence.nextval, a bunch of other stuff)
</cfquery>
My issue is I want to insert that auto-incremented itemid as a value into another table, a file-attachment table. The same coldfusion page:
<cfquery>
INSERT INTO my_attachments_table (attachno, itemid, filename)
VALUES (attachment_sequence.nextval, **AUTO-INCREMENT VALUE HERE**, '#url.fileName#')
</cfquery>
I know I could query the item table for the values that were just entered into the first table, but even though it's not likely, there is a possibility that the user input isn't unique, meaning a query of the same fields could return more than one row. In which case, I couldn't get the itemid. The itemid created in the query is the only unique identifier.
My question is: is there a way to set the auto=increment to a value inside the query so I can use it outside? If not, how would you suggest getting my itemid? Thanks.
If you do an insert via in ColdFusion 8/9/10, it returns the ID of the inserted row (or rows) in the result attribute of the tag. For example, if you're using SQL Server, result_name.IDENTITYCOL is the reference to the ID of the inserted row. If you're using MySQL, it's result_name.GENERATED_KEY.
You could start by retrieving the next sequence value into a variable with a query like this:
SELECT itemid_sequence.nextval FROM DUAL
(DUAL is a dummy table in Oracle). You could then proceed to use this variable in both INSERT queries.
Another option: create a stored procedure in the database that will fire both queries for you.