INSERT INTO subtract 2 values - sql

Is it possible for an INSERT query to subtract 2 values you have entered to create a 3rd value that can then be inserted into a table - if that makes sense...
e.g.
INSERT INTO table1 (column1, column2, column3)
VALUES ('50', '25', column1 - column2)

INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', column1 - column2) from table1 where conditions)
This is a sample query! hope it helps!

Convoluted:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
(select 50 as column1,
25 as column2
) t
Since you can't reference other columns from the same SELECT clause, you have to do it as a subquery. I've also switched to using int literals rather than strings, because I can't make subtraction make sense in my head otherwise.
You could also do it using a Table Value Constructor:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
( VALUES (50, 25)
) AS t (column1, column2);
As indicated in my comment though, if the relationship should always hold, I'd build table1 as:
CREATE TABLE table1 (
column1 int not null,
column2 int not null,
column3 as column1 - column2
--More columns
)
Because that way, the column3 value is always correct.

You can create function that subtracts values and use this function in insert. This is the right way to do such things:
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', your_function() ) from table1 where conditions)
/

Using the "INSERT INTO" would do this:
INSERT INTO Table1Name (column1, column2, column3,)
(select 'X', 'Y', X - Y as Z)
Here is a link to SQL Authority with more examples of INSERT INTO

Another method would to be add a trigger to the table, where on insert of data, the third column would be updated with the difference of the first two columns.

Related

postgresql conditionnal insert values

I need a SQL which do following work.
INSERT INTO table_1 (column1, column2, column3) VALUES (:1,:2, :3)
This insert will only happen when another condition is TRUE, the
condition is:
SELECT 1 FROM table_2 WHERE column_time = 'time_stamp'
Table_1 and table_2 dont have any relation. the INSERT values to table_1 should only happen if the query on Table_2 return TRUE(1)
So i try to write my SQL as following
INSERT INTO table_1 (column1, column2, column3) VALUES (:1,:2, :3)
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING
However, this SQL not work as Postgresql looks dont like the INSERT combine with WHERE condition.
BTW: the values (:1, :2:, :3) are bind array values. the final SQL looks like this:
INSERT INTO table_1 (column1, column2, column3) VALUES ('ca_1','cb_1', 'cc_1') ('ca_2','cb_2', 'cc_2') ... ('ca_n','cb_n', 'cc_n') WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'my_time_stamp') ON CONFLICT DO NOTHING
Really need help thanks.
You need to select those values.
This would work in Postgres:
INSERT INTO table_1 (column1, column2, column3)
SELECT :1, :2, :3
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING
Alternatively:
INSERT INTO table_1 (column1, column2, column3)
SELECT x.*
FROM (VALUES (:1, :2, :3)) AS x(column1, column2, column3)
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING

MS SQL output inserted with insert from select

I'm trying to figure out how to combine INSERT FROM SELECT and returning id value of inserted record.
INSERT INTO [someDB].[dbo].[OBJ] ( column1, column2, column3 )
OUTPUT inserted.ID (SELECT TOP 1 590675, column2, column3
FROM [someDB].[dbo].[OBJ] WHERE ID = 317817)
If you are trying to get the new ID after your INSERT statement you can use SCOPE_IDENTITY, IDENT_CURRENT or ##IDENTITY. For example:
INSERT INTO [someDB].[dbo].[OBJ] ( column1, column2, column3 )
SELECT TOP 1
590675,
column2,
column3
FROM [someDB].[dbo].[OBJ] WHERE ID = 317817
ORDER BY ...
SELECT SCOPE_IDENTITY(); -- Last identity generated in current session and current scope
SELECT ##IDENTITY; -- Last identity generated in current session across all scopes
SELECT IDENT_CURRENT([someDB].[dbo].[OBJ]) -- Last identity generated for the given table in any session and any scope
Because you are inserting just one row, SCOPE_IDENTIY() would be the best approach.
Hope it helps.
Your syntax is a little wrong here.
You want:
INSERT INTO [someDB].[dbo].[OBJ] ( column1, column2, column3 )
OUTPUT inserted.ID
SELECT TOP 1
590675,
column2,
column3
FROM [someDB].[dbo].[OBJ] WHERE ID = 317817
ORDER BY ...?; --You have a TOP 1, thus you really need an ORDER BY as well.

SQL - Concat in where clause with IN

I'm trying use IN to query multiple columns. If I use "=", I return rows (see example) but I would like to query multiple.
need query table to return rows A12345 and B98765 but not C00000
column1 | column2
A 12345
B 98765
C 00000
This works
SELECT *
FROM TABLE
WHERE (column1,column2) = ('A',12345)
This does not work.
SELECT *
FROM TABLE
WHERE (column1,column2) IN (('A',12345),('B',98765))
Here is error:
Error: SQL0104N An unexpected token "," was found following ",". Expected tokens may include: "AT MICROSECONDS MICROSECOND SECONDS SECOND MINUTES MINUTE HOURS". SQLSTATE=42601
(State:42601, Native Code: FFFFFF98)
I've tried several variations of parenthesis, commas's, etc and can't get it to work. Is it possible to do this and if so can you provide the syntax.
thanks
DB2 9.7 onwards:
SELECT *
FROM TABLE
WHERE (column1, column2) IN (VALUES ('A', 12345), ('B', 98765))
One solution is to use a temp table:
create table #tmp
(
col1 varchar(2),
col2 int
);
insert into #tmp (col1, col2)
values ('A', 12345), ('B', 98765)
SELECT t.* FROM TABLE t
JOIN #tmp ON #tmp.col1 = t.column1 and #tmp.col2 = t.column2
You haven't stated your RDBMS, but have you tried:
SELECT * FROM TABLE
WHERE (column1, column2) IN ('A', 12345)
OR (column1, column2) IN ('B', 98765)
Depending on your data, this might be workable:
SELECT *
FROM TABLE T
JOIN (
SELECT 'A' COL1, 12345 COL2
UNION ALL
SELECT 'B', 98765
UNION ALL
SELECT 'C', 44365) AS Matches
ON T.column1 = Matches.COL1
AND T.column2 = Matches.COL2
This turned out to be easy and should have known this.
SELECT *
FROM TABLE
WHERE CONCAT(column1, column2) IN ('A12345','B98765')

insert into with select + new information

I'm trying to write a sql query that can copy specific columns from a table and insert it in the same table + extra information for the other columns
Simply copying certain information would be something like this:
INSERT INTO table (column1, column2)
SELECT column1, column2
FROM table
WHERE columnx = 'some value'
But I need to also insert some new information in column3. How can I do that?
I have the information that will go in "column3", I don't have to get it from an other table or source.
This is for a repeat appointment where basically all the information is the same except for date, planner and appointment_id.
If you know what the values are . . .
INSERT INTO table (column1, column2, column3)
SELECT column1, column2, <value for column3>
FROM table
WHERE columnx = 'some value'

inserting multiple rows with one insert command

Is it possible to insert more than one row in a table with one insert statement?
I know this will happen if I do:
insert into table ( fields ) select values from another_table
But what if I want to insert:
row 1 - ( a1, b1, c1 )
row 2 - ( a2, b2, c2 )
...
row n - ( an, bn, cn )
with just one insert command?
Two solutions (source : http://appsfr.free.fr/spip.php?article21 ):
INSERT ALL
INTO table (column1, column2)
VALUES (value1, value2)
INTO table (column1, column2)
VALUES (value1, value2)
...etc...
SELECT * FROM DUAL ;
or
INSERT INTO table (column1, column2)
SELECT value1, value2 FROM DUAL UNION ALL
SELECT value1, value2 FROM DUAL UNION ALL
...etc...
SELECT value1, value2 FROM DUAL ;
Insert All
INSERT ALL
INTO mytable (column1, column2, column3) VALUES ('val1.1', 'val1.2', 'val1.3')
INTO mytable (column1, column2, column3) VALUES ('val2.1', 'val2.2', 'val2.3')
INTO mytable (column1, column2, column3) VALUES ('val3.1', 'val3.2', 'val3.3')
SELECT * FROM dual;
INSERT INTO products (product_no, name, price) VALUES
(1, 'Cheese', 9.99),
(2, 'Bread', 1.99),
(3, 'Milk', 2.99);
INSERT INTO College (CustomerID, FirstName, MiddleName, LastName)
SELECT 7, N'Charles', N'Simmons', N'Burns'
UNION ALL
SELECT 9, N'Dominic', N'Fred', N'Einsten'
UNION ALL
SELECT 12, N'Dave', N'William, N'Bryan';
NOTE:
Letter N before each hard coded string value converts string to an NVARCHAR value to match the datatype of the column.
No, this is not possible. As you already stated yourself, it is only possible with a select clause providing the insert values and rows.