Append one column table to another existing, larger table SQL Server - sql

I have a table name KHA_ID with a column name of KHA_ID. I went to append that to another table named Visits with a column name KHA_ID, and I want to see if I have my SQL right before I run the query
INSERT INTO [databaseName].[dbo].[KHA_ID]
(KHA_ID)
VALUES (KHA_ID,[dbo].Visits)
GO
Thanks!

If KHA_ID is the primary key in KHA_ID table then
INSERT INTO [databaseName].[dbo].[KHA_ID]
(KHA_ID)
SELECT distinct KHA_ID
FROM [dbo].Visits V left outer join
KHA_ID from [databaseName].[dbo].[KHA_ID] K on K.KHA_ID=V.KHA_ID
where K.KHA_ID is null
GO

You need to do it this way:
INSERT INTO [databaseName].[dbo].[KHA_ID]
(KHA_ID)
SELECT KHA_ID
FROM [dbo].Visits
GO

Related

Copy data from one table to another - Ignore duplicates Postgresql

I am using Postgresql db. I have data in two tables. Table A has 10 records and Table B 5 records.
I would like to copy Table A data to Table B but only copy the new entries (5 records) and ignore the duplicates/already existing data
I would like to copy data from Table A to Table B where Table B will have 10 records (5 old records + 5 new records from Table A)
Can you please help me as to how can this be done?
Assuming id is your primary key, and table structures are identical(both table has common columns as number of columns and data type respectively), use not exists :
insert into TableB
select *
from TableA a
where not exists ( select 0 from TableB b where b.id = a.id )
If you are looking to copy rows unique to A that are not in B then you can use INSERT...SELECT. The SELECT statement should use the union operator EXCEPT:
INSERT INTO B (column)
SELECT column FROM A
EXCEPT
SELECT column FROM B;
EXCEPT (https://www.postgresql.org/docs/current/queries-union.html) compares the two result sets and will return the distinct rows present in result A but not in B, then supply these values to INSERT. For this to work both the columns and respective datatypes must match in both SELECT queries and your INSERT.
INSERT INTO Table_A
SELECT *
FROM Table_B
ON CONFLICT DO NOTHING
Here, the conflict will be taken based on your primary key.

Postgresql, Copy data to a new table from a foreign table

I'm trying to do a job that will copy data from a foreign table called "m_aduana" of the schema "nathalia" to my schema "publico" and my table "mae_aduana".
I need to do a query that copies all the values from the table "m_aduana" avoiding duplicates.
I got something like this for now but the result sends me an Insert 0 0, which means nothing is inserted.
insert into publico.mae_aduana(cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana)
select cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana
from nathalia.m_aduana
where not exists (
select * from publico.mae_aduana ma_ad, nathalia.m_aduana m_ad
where ma_ad.cod_aduana = m_ad.cod_aduana)
I think you have an error in the inner select. You don't need to use again the table nathalia.m_aduana. If should be something like:
insert into publico.mae_aduana(cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana)
select cod_aduana,nom_aduana,des_aduana,cod_aduana1,cod_aduana2,cod_aduana3,est_aduana
from nathalia.m_aduana
where not exists (
select * from publico.mae_aduana ma_ad
where ma_ad.cod_aduana = nathalia.m_aduana.cod_aduana)
You might want to change the where exists part like below
from nathalia.m_aduana m
where not exists (
select 1 from publico.mae_aduana
where cod_aduana = m.cod_aduana)

SQL Query to match if data is not present in another table

I have two tables named tblStockManagement and tblFolding in my database. i have column Name in tblFolding table and column FoldingID as Foreign Key in tblStockManagement table. now i have comboBox in my Winform and i want Names of Items in combobox from tblFolding Table but only those items that are not in tblStockManagement Table.
(because i dont want to select data again if it is already in tblStockManagement table . instead i will update the quantity later).
these are the screenshots of both of tables. please tell me how can i do that
NOT EXISTS version:
select *
from tblFolding f
where not exists (select * from tblStockManagement SM
where sm.FoldingID = f.FoldingID)
NOT EXISTS is "NULL safe", which NOT IN isn't.
This is you need.Basically a sub query which gets all folding id and using not in operator I exclude those matching sets.
SELECT Name
FROM tblFolding
WHERE FoldingID NOT IN (
SELECT FoldingID
FROM tblStockManagement
)
;
You can use SQL NOT condition
Select Name
From tblFolding
Where FoldingId Not In (Select FoldingId From tblStockManagement)
Order By Name

SQL Query to return rows even if it is not present in the table

This is a specific problem .
I have an excel sheet containing data. Similar data is present in a relational database table. Some rows may be absent or some additional rows may be present. The goal is to verify the data in the excel sheet with the data in the table.
I have the following query
Select e_no, start_dt,end_dt
From MY_TABLE
Where e_no In
(20231, 457)
In this case, e_no 457 is not present in the database (and hence not returned). But I want my query to return a row even if it not present (457 , null , null). How do I do that ?
For Sql-Server: Use a temporary table or table type variable and left join MY_TABLE with it
Sql-Server fiddle demo
Declare #Temp Table (e_no int)
Insert into #Temp
Values (20231), (457)
Select t.e_no, m.start_dt, m.end_dt
From #temp t left join MY_TABLE m on t.e_no = m.e_no
If your passing values are a csv list, then use a split function to get the values inserted to #Temp.
Why not simply populate a temporary table in the database from your spreadsheet and join against that? Any other solution is probably going to be both more work and more difficult to maintain.
You can also do it this way with a UNION
Select
e_no, start_dt ,end_dt
From MY_TABLE
Where e_no In (20231, 457)
UNION
Select 457, null, null

Insert multiple records from joining temporary tables

I need to insert multiple return records from a joining select command to a temporary table. I used the below command . The select statement return two value but when i insert using the below command then the (#TempTableValue) temporary table have nothing.All other field declaration is ok and the joining select command is returning value properly.
INSERT into #TempTableValue DEFAULT values
SELECT #temp1.id,#temp1.DestFieldName,#temp2.FieldValues
FROM #temp2
INNER JOIN #temp1
ON #temp2.FieldName=#temp1.SourceFieldName
select * from #TempTableValue
But the last select command return all Null value. What is the problem here?
Try to remove DEFAULT value as below:
INSERT into #TempTableValue
SELECT #temp1.id,#temp1.DestFieldName,#temp2.FieldValues
FROM #temp2
INNER JOIN #temp1 ON #temp2.FieldName=#temp1.SourceFieldName
from documentation:
DEFAULT VALUES
Forces the new row to contain the default values defined for each
column.
Alright I think I know what's wrong here.
If you use Insert Into #TempTableValue You have to First Create the temp table.
To directly insert your Selection you could use this
Select #temp1.id,#temp1.DestFieldName,#temp2.FieldValues Into #TempTableValue
FROM #temp2
INNER JOIN #temp1
ON #temp2.FieldName=#temp1.SourceFieldName