I have INSERT statement where I have to use one of my argument values and values from my temporary table. I'm using SQL Server 2008. I haven't worked with temp tables before so I'm not sure if and how I can combine argument values and temp table since arguments have to be placed inside of the VALUES() and temp table values are pulled using SELECT. Here is my code:
INSERT INTO myTbl (col1, col2, col3, col4, col5, col6)
//col1 should use #arguments.myVal#
SELECT #col2,#col3,#col4,#col5,#col6
FROM #myTemp
Is there any way to combine values from two different sources into insert statement?
hope this what you are looking for.
Declare #Param1 int = 20
INSERT INTO myTbl (col1, col2, col3, col4, col5, col6)
SELECT #Param1, col2, col3, col4, col5, col6
FROM #myTemp
Related
I have a table which has 3 columns and 6 records, I want to insert the values in another table which has an additional column of sysid, when I use insert into clause it inserts the records but the sysid value is same , I want different values everytime a record insert but I want to insert in bulk as well
Use a sequence:
CREATE SEQUENCE table_name__sysid__seq;
Then:
INSERT INTO table_name (sysid, col1, col2, col3, col4, col5, col6)
VALUES (table_name__sysid__seq.NEXTVAL, 'value1', 'value2', 'value3', 'value4', 'value5', 'value6');
or, for multiple rows:
INSERT INTO table_name (sysid, col1, col2, col3, col4, col5, col6)
SELECT table_name__sysid__seq.NEXTVAL, col1, col2, col3, col4, col5, col6
FROM first_table;
Or else, if you are using Oracle 12 or later, define sysid as an IDENTITY column:
CREATE TABLE table_name (
sysid NUMBER
GENERATED ALWAYS AS IDENTITY
PRIMARY KEY,
col1 VARCHAR2(20),
col2 VARCHAR2(20),
col3 VARCHAR2(20),
col4 VARCHAR2(20),
col5 VARCHAR2(20),
col6 VARCHAR2(20)
);
db<>fiddle here
I have 5 columns out of which I have known values for 2 columns.
I have to insert values for 3 columsn using INSERT INTO...SELECT statement and the code is working.
I only want to know how do I combine both INSERT INTO SELECT with known values in ORACLE.
insert into destination (col1, col2, col3, col4, col5)
select 'static_value1', 'static_value2', col6, col7, col8
from source_table
Is it possible to display which rows I did inserted via this query:
INSERT INTO dbo.Table (Col1, Col2, Col2)
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
It is important to obtain whole new row with it's PrimaryKey value, etc., not only Col1, Col2, Col3
The OUTPUT clause is your friend now:
INSERT INTO steam.DocumentGeographies (Col1, Col2, Col3)
OUTPUT inserted.*
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
You can insert the result of the OUTPUT by specifying the destination table with the INTO clause or by wrapping the query as a subquery:
INTO clause
This is useful, when you want to insert the same data into two tables. You can always list the required fileds in the OUTPUT clause (inserted.Col1, inserted.Col2)
INSERT INTO steam.DocumentGeographies (Col1, Col2, Col3)
OUTPUT inserted.* INTO DestinationTableName(Col1, Col2, Col3)
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
SUBQUERY
This is useful, when you want to join the OUTPUT to another tables or you want to make calculations (like summing or counting values) and insert those results into another table.
INSERT INTO DestinationTableName
(Col1, Col2, Col3)
SELECT Col1, Col2, Col3 FROM (
INSERT INTO steam.DocumentGeographies (Col1, Col2, Col3)
OUTPUT inserted.*
(SELECT
Col1,
Col2,
'Something modified',
FROM dbo.Table
WHERE Col10 = 66)
) TMP
I'm doing an
INSERT INTO table1...
SELECT...
FROM table2
However, I need to retrieve the identity from a table3 and do an insert into it just before inserting into table1. These two inserts need to occur together, with table3 insert going first. I've tried something like this:
INSERT INTO table1 (col1, col2, col3)
SELECT (
col1=(insert into table3(col1, col2)values(1,1) select SCOPE_IDENTITY(),
col2, col3)
FROM table2
But that doesn't work. table1.col1 does need the identity value from the new insert into table3. Amount of data to insert probably no more than a few 100 rows. Any suggestions?
It looks like you might be able to use the Output Clause.
BEGIN TRANSACTION
DECLARE #MyResults table(col1 int, col2 int, col3 int);
INSERT INTO table3 (col1, col2)
OUTPUT SCOPE_IDENTITY(), table2.col2, table2.col3 INTO #MyResults
SELECT 1, 1 FROM table2
INSERT INTO table1 (col1, col2, col3)
SELECT col1, col2, col3 FROM #MyResults
COMMIT
How to modify the following stored procedure if we want in the inserted records the Col3 contain a constant string 'Foo'?
INSERT Table2
(Col1,
Col2,
Col3)
SELECT T1Col1,
T1Col2
FROM Table1
INSERT Table2
(Col1,
Col2,
Col3)
SELECT T1Col1,
T1Col2,
'Something'
FROM Table1
INSERT Table2
(Col1,
Col2,
Col3)
SELECT T1Col1,
T1Col2,
'Foo'
FROM Table1