INSERT INTO a created table throws an error - sql-server

The following code shows an error on the SELECT statement in line 2.
INSERT INTO (SELECT A.Column1, B.Column2
FROM Table1 A
LEFT JOIN Table2 B ON A.Id = B.Id) AB
SELECT Column1, Column2
FROM ExtraData C
What I'm trying to do here is - I have 2 tables, A and B, that need to be joined (to make the table AB) and then to this joined table I have to add a few extra rows which are present in C
If I create a temp table for the table AB before the INSERT INTO it seems to work fine.
Is there such a restriction on INSERT?

If you want to create a table called AB, using select into:
SELECT Column1, Column2
INTO AB
FROM ExtraData C;
It is not clear exactly what you are trying to do, though.
If that is the case, then use union all:
SELECT A.Column1, B.Column2
INTO AB
FROM Table1 A LEFT JOIN
Table2 B
ON A.Id = B.Id
UNION ALL
SELECT Column1, Column2
FROM ExtraData C;
If you already have the table defined, then do:
INSERT INTO AB(column1, column2)
SELECT A.Column1, B.Column2
FROM Table1 A LEFT JOIN
Table2 B
ON A.Id = B.Id
UNION ALL
SELECT Column1, Column2
FROM ExtraData C;

You cannot do that in SQL Server. I don't know what RDBMS supports this syntax, or even what you expect as output.
There are two ways to insert data into a temp table. You already discovered the first which is CREATE TABLE #AB (...) then INSERT INTO #AB.
The second way is to use SELECT ... INTO:
SELECT A.Column1, B.Column2
INTO #AB
FROM Table1 A
LEFT JOIN Table2 B
ON A.Id = B.Id
-- Now that #AB is defined, you can use INSERT INTO to add additional data
INSERT INTO #AB (Column1, Column2)
SELECT Column1, Column2
FROM ExtraData C

INSERT INTO SomeTable
SELECT A.Column1, B.Column2
FROM Table1 A
LEFT JOIN Table2 B
ON A.Id = B.Id
UNION
SELECT Column1, Column2
FROM ExtraData C

Related

Selecting Columns A values if no like a value in column B in SQL Server

I have Table1
And I am trying to remove every identical Column A value if one of it's row has "ZX" anywhere in Column B. So if I did it right, it will look like Table2
I did the following:
Select
Column A,
Column B
From
Table1
Where
Column B not like '%ZX%'
However, it only removes rows with ZX and not every identical Column A values and returns this Table instead
I will really appreciate any help on this! Thank you in advance :)
You can use NOT IN
SELECT
ColumnA
, ColumnB
FROM table1
WHERE ColumnA NOT IN (SELECT ColumnA FROM table1 WHERE ColumnB like '%ZX%')
I like not exists for this purpose:
Select t1.*
From Table1 T1
where not exists (select 1 from table1 tt1 where tt1.a = t1.a and tt1.b like '%ZX%');
This can take advantage of an index on table1(a, b).
Use :NOT EXISTS and that should do it:
Select
[Column A],
[Column B]
From Table1 T1
where
NOT EXISTS (
SELECT 1 FROM TABLE1 T2
WHERE T2.[Column B] like '%ZX%'
AND T2.[column a] = t1.[column a]
)

How can I find different elements between two columns (generated by two subqueries)?

My subqueries produce a result like this:
coulmn1 column2
a d
b z1000
c c
d
1
2
z1000 k
I want to know the different elements in both sets. column1 ={ a,b,c, 1,2,d, z1000,.....} column 2 ={ d,c,z1000,k......} The result I want is ={ a,k,1,2,....} hope I made it clear ..please let me know how could I do that..?
One method is full outer join:
select coalesce(t1.col1, t2.col2)
from t t1 full join
t t2
on t1.col1 = t2.col2
where t1.col1 is null or t2.col2 is null;
Another method doesn't require running the subquery twice;
select v.col
from t cross apply
(values (t.col1, 1), (t.col2, 2)) v(col, which)
group by v.col
having min(v.which) = max(v.which);
--Test Data
with temp_table as (
select 'a' coulmn1,'b' column2 union all
select 'b' coulmn1,'z1000' column2 union all
select 'c' coulmn1,'c' column2 union all
select 'd' coulmn1,'' column2 union all
select 'z1000' coulmn1,'k' column2
)
--use cross join and union to distinct data
--you have to change temp_table to your own table
select * into #temp_table from (
select T.coulmn1,T2.column2 as column2
from temp_table T,temp_table T2
where T.coulmn1 <> T2.column2
) T;
select coulmn1 from #temp_table
union
select column2 from #temp_table;
Test Link
Use EXCEPT for this.
SELECT column1
FROM your_subquery
EXCEPT
SELECT column2
FROM your_subquery
UNION
SELECT column2
FROM your_subquery
EXCEPT
SELECT column1
FROM your_subquery

Acquiring Row index with identity column in Join Query

I need to add the results from a Left Join query to a table that does not have its index set to be an identity, but the int must still be unique. My insert query looks like this:
INSERT INTO Table1 (S.SubjectID, S.Subject, S.SubjectDescription, S.Status)
SELECT (Select MAX(SubjectID) FROM Table1) + ???? , N.Code, N.Literal, N.Trans
FROM Table2 N LEFT JOIN Table1 S ON N.Code = S.Subject
WHERE (N.Code IS NULL OR S.Subject IS NULL OR N.Trans = 'D')
Where I have the ???? is where i need to have some incrementing value so that when inserting into the table1 the ID's will be unique.
I am not allowed to change the table's structure, I just need something that can calculate his on the fly.
As always help, tips and references are much appreciated.
In most databases, you can use row_number() for this purpose. Here is an example with SQL Server syntax:
INSERT INTO Table1 (S.SubjectID, S.Subject, S.SubjectDescription, S.Status)
SELECT (Select MAX(SubjectID) FROM Table1) + row_number() over (order by (select NULL)) ,
N.Code, N.Literal, N.Trans
FROM Table2 N LEFT JOIN Table1 S ON N.Code = S.Subject
WHERE (N.Code IS NULL OR S.Subject IS NULL OR N.Trans = 'D')

Need Unique Value from table

I have two tables:
Table A
ID Name
1 abc
2 xyz
Table B
ID Name
1 abc
2 xyz
3 mno
I need the distinct value form above two table, I mean i want only ID 3 Name mno from Table B (as it is unique from two table)
Please let me know how I can get this value.
Thanks,
Ajay
This query will get you the rows from B that don't exist in A:
SELECT b.* FROM TableB b
OUTER JOIN TableA a ON a.ID = b.ID AND a.Name = b.Name
WHERE a.ID IS NULL
you could then do the adverse and use a UNION ALL to get it both ways:
SELECT a.* FROM TableA a
OUTER JOIN TableB b ON b.ID = a.ID AND b.Name = a.Name
WHERE b.ID IS NULL
UNION ALL
SELECT b.* FROM TableB b
OUTER JOIN TableA a ON a.ID = b.ID AND a.Name = b.Name
WHERE a.ID IS NULL
Another way of achieving it would be:
;WITH MatchingRows AS (
SELECT a.ID FROM TableA a
JOIN TableB b ON b.ID = a.ID AND b.Name = a.Name
)
SELECT * FROM TableA
WHERE ID NOT IN (SELECT m.ID FROM MatchingRows m)
UNION ALL
SELECT * FROM TableB
WHERE ID NOT IN (SELECT m.ID FROM MatchingRows m)
I'm not sure if that performs better or not - it's just something I thought of. If I'm not mistaken this will actually run the WITH query twice (see the answer to this question) because it's being used twice - so there may be some performance implications with this approach.
The EXCEPT operator may work for you. Here is an example using your data.
CREATE TABLE TableA (id int, name varchar(50))
INSERT INTO TableA VALUES (1, 'abc'),(2,'xyz')
CREATE TABLE TableB (id int, name varchar(50))
INSERT INTO TableB VALUES (1, 'abc'),(2,'xyz'),(3,'mno')
SELECT * FROM TableB
EXCEPT
SELECT * FROM TableA
Be warned though it acts like UNION. It's only going to exclude rows where there is an exact match on all columns.

SQL server 2000 insert rows when join does not match

I would simply like to insert rows from a table to another if rows does not exist in the target.
How should I code that? with inner join?
Below is the query which returns rows that match between source and target
select * from LOG_S1201_REFERENCE_T1 b
inner join LOG_S1201_REFERENCE_STAGING_WT5 a on b.OU_ID=a.OU_ID and
b.Plant_desc=a.Plant_desc and b.workshop=a.workshop and
b.SerieNum=a.SerieNum and b.Operation_type=a.Operation_type and
b.PC10DBName=a.PC10DBName and b.SimuDBName=a.SimuDBName and
b.ProgramName=a.ProgramName and b.Calibre=a.Calibre
Copying rows can be done through INSERT SELECT
Need an example?
INSERT INTO new_table (col1, col2, col3)
SELECT col4, col5, col6
FROM old_table
HAVING !(SELECT COUNT(*) FROM new_table WHERE col1 = old_table.col4)
Join the table that has all rows with left join with the table that has some of the rows.
Filter one of the left joined table column with IS NULL
INSERT INTO your_table
select b.*
from LOG_S1201_REFERENCE_T1 b
left join LOG_S1201_REFERENCE_STAGING_WT5 a on b.OU_ID=a.OU_ID and b.Plant_desc=a.Plant_desc and b.workshop=a.workshop and b.SerieNum=a.SerieNum and b.Operation_type=a.Operation_type and b.PC10DBName=a.PC10DBName and b.SimuDBName=a.SimuDBName and b.ProgramName=a.ProgramName and b.Calibre=a.Calibre
WHERE a.OU_ID IS NULL
In order to select rows that do NOT match you need to do a left join.
INSERT INTO LOG_S1201_REFERENCE_T1 b2
SELECT a.* FROM LOG_S1201_REFERENCE_STAGING_WT5 a
LEFT JOIN LOG_S1201_REFERENCE_T1 b ON
(b.OU_ID=a.OU_ID
AND b.Plant_desc=a.Plant_desc
AND b.workshop=a.workshop
AND b.SerieNum=a.SerieNum
AND b.Operation_type=a.Operation_type
AND b.PC10DBName=a.PC10DBName
AND b.SimuDBName=a.SimuDBName
AND b.ProgramName=a.ProgramName
AND b.Calibre=a.Calibre)
WHERE b.OU_ID IS NULL
This will select all rows from a that are not in b which will then be inserted into a.
Afterwards a and b should be identical (except for b rows that are not in a).

Resources