How to get count on ths query - sql

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

Related

TSQL INSERT INTO SELECT - display rows

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

SQL Server: Select from two tables and insert into one

I have a stored procedure with two table variables (#temp and #temp2).
How can I select the values from both temp tables (Both table variables contain one row) and insert them all in one table ?
I tried the following but this didn't work and I got the error that the number of SELECT and INSERT statements does not match.
DECLARE #temp AS TABLE
(
colA datetime,
colB nvarchar(1000),
colC varchar(50)
)
DECLARE #temp2 AS TABLE
(
colD int
)
...
INSERT INTO MyTable
(
col1,
col2,
col3,
col4
)
SELECT colD FROM #temp2,
colA FROM #temp,
colB FROM #temp,
colC FROM #temp
Many thanks for any help with this, Tim.
As both table variables have a single row you can cross join them.
INSERT INTO MyTable
(col1,
col2,
col3,
col4)
SELECT t.colA,
t.colB,
t.colC,
t2.colD
FROM #temp t
CROSS JOIN #temp2 t2
you should use this if you have only single row in both table #temp and #temp2, because this is a cartesian product.
INSERT INTO MyTable(col1,col2,col3,col4)
SELECT t.colA,
t.colB,
t.colC,
t2.colD
FROM #temp t,#temp2 t2

How to get the count of occurrence from comma separated string

I have following two tables in Oracle 8i
For each col1 in T1 table, we need to find out the number of occurrences in T2 table (for matching col3).
QUESTION
What is the best way to achieve this in Oracle using a single query (without creating temporary table)?
Fiddle
EXPECTED RESULT
‘C1’ -- 10 -- 1
‘C1’ -- 8 -- 1
‘C2’ -- 10 -- 1
‘C3’ -- 10 -- 0
‘C4’ -- 10 -- 2
SQL
--Table 1
CREATE TABLE T1 (col1 varchar2(2), col2 varchar(8), col3 NUMBER);
INSERT INTO T1 (col1, col2, col3) VALUES ('C1', 'john',10);
INSERT INTO T1 (col1, col2, col3) VALUES ('C1', 'nishal',8);
INSERT INTO T1 (col1, col2, col3) VALUES ('C2', 'piers',10);
INSERT INTO T1 (col1, col2, col3) VALUES ('C3', 'sara',10);
INSERT INTO T1 (col1, col2, col3) VALUES ('C4', 'lijo',10);
--Table 2
CREATE TABLE T2 (col1 varchar2(2), col2 varchar(8), col3 NUMBER);
INSERT INTO T2 (col1, col2 , col3) VALUES ('R0', 'C1,C4',10);
INSERT INTO T2 (col1, col2 , col3) VALUES ('R1', 'C1',8);
INSERT INTO T2 (col1, col2 , col3) VALUES ('R2', 'C2,C4',10);
Fiddle
select T1.col1
, T1.col3
, count(T2.col1)
from T1
left join
T2
on T1.col3 = T2.col3
and ',' || T2.col2 || ',' like '%,' || T1.col1 || ',%'
group by
T1.col1
, T1.col3
Example at SQL Fiddle.

Sql insert select from – multiple rows with unique column id

I am trying to copy multiple records using one query using insert select from.
Insert into tab_A(colId, col1, col2, col3)
Select colId, col1, col2, col3 form tab_A
Where colId in ( 2,4,6)
Would it be possible to assign different colId for new entries? For example colid 2 should be replaced with 23, 4 with 24 and 6 with 25. How could I achieve it in a single query?
this would work
Insert into tab_A(colId, col1, col2, col3)
Select 23 , col1, col2, col3 form tab_A Where colId = 2 UNION ALL
Select 24 , col1, col2, col3 form tab_A Where colId = 4 UNION ALL
Select 25 , col1, col2, col3 form tab_A Where colId = 6
If you give some more info I could provide somthing more reusable. Should/is colId (be) an identity column?
EDIT
This would work in this very specialised case
Insert into tab_A(colId, col1, col2, col3)
Select ((colId - 4) * (-1)) + colId + 20 , col1, col2, col3
form tab_A Where colId IN (2, 4, 6)
The function newId = ((oldId - 4) * (-1)) + oldId + 20 is obviously specific to the stated problem.
EDIT2
I suspect somthing like this is more generic approach is appropriate.
DECLARE #MaxColID INT
BEGIN TRANSACTION
SELECT #MaxColID = MAX(ColID) FROM tab_A
INSERT tab_A(colId, col1, col2, col3)
SELECT row + #MaxColID, col1, col2, col3
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY ColID) row, col1, col2, col3
FROM tab_A WHERE colID IN (2, 4, 6)
)
COMMIT
EDIT 3
If you think EDIT 2 is actually what you want then you really want to make ColID an IDENTITY column, then you could do this.
INSERT tab_A (col1, col2, col3)
SELECT col1, col2, col3 FROM tab_A WHERE colId IN (2, 4, 6)
I dont see col4 or col6 in your query, but is this what you want:
Insert into tab_A(colId, col1, col2, col3)
Select colId, col1, 23, col3 form tab_A
Where colId in ( 2,4,6)
have you just tried adding the disired difference to colId -
In your case, since you need to replace 2 by 23, difference is 21.
Insert into tab_A(colId, col1, col2, col3)
Select colId+21, col1, col2, col3
form tab_A Where colId in ( 2,4,6)
Note: I missed the part, that the differnce is not consistent in your case.
The proposed solution will work only if difference is same
There are a few options:
Add the new ID column to the original table and populate it with the new values before you do this insert, selecting the new ID column instead of the old. This would be the tidiest solution I think.
Alternative - Modify the ID value on the insert based on a rule e.g.
INSERT INTO tab_A(colID, col1, col2, col3)
SELECT colId + 20, col1, col2, col3
FROM tab_A
WHERE colID IN(2,4,6)
Last resort - Process the insert sequentially with a cursor, modifying the ID value each time.
You could also write case in the select. when 2 then 23 or whatever value.

Stored procedure for inserting a constant plus values from another table

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