SQL Insert Into Exists - Adding a foreign key to existing table - sql

I am trying to add a key from one table to another so that I can create a one to many relationship. The table I would like to add a foreign key to currently has the employee's first name and last name, but not ID. I would like to query the employee table to find the ID based on their first name and last name, and then add it to the injury table.
I Keep getting the following errors:
The multi-part identifier "InjuryOLD.FirstName" could not be bound.
The multi-part identifier "dbo.InjuryOLD.LastName" could not be bound.
Below is the SQL Statement:
Insert into dbo.InjuryOLD(dbo.InjuryOLD.EmpID)
Select dbo.EmployeeInformation.EmpID
From EmployeeInformation
Where exists(select dbo.EmployeeInformation.EmpID from dbo.EmployeeInformation
where dbo.EmployeeInformation.FirstName = dbo.InjuryOLD.FirstName
and dbo.EmployeeInformation.LastName = dbo.InjuryOLD.LastName)
I have ensured that the tables and columns that I am referencing have no typo's, and I know that there is a chance that two employee could have the same name. However that is not the case in this scenario.

The SELECT part an INSERT statement should always work on its own
so...
Select dbo.EmployeeInformation.EmpID
From EmployeeInformation
Where exists(select dbo.EmployeeInformation.EmpID from dbo.EmployeeInformation
where dbo.EmployeeInformation.FirstName = dbo.InjuryOLD.FirstName
and dbo.EmployeeInformation.LastName = dbo.InjuryOLD.LastName)
...doesn't work because dbo.InjuryOLD isn't referenced in either FROM clause. You probably meant to put the dbo.InjuryOLD in the from clause of the Exists sub query instead of repeating EmployeeInformation
Select dbo.EmployeeInformation.EmpID
From EmployeeInformation
Where exists(select 1 from dbo.InjuryOLD
where
dbo.EmployeeInformation.FirstName = dbo.InjuryOLD.FirstName
and dbo.EmployeeInformation.LastName = dbo.InjuryOLD.LastName)

Related

Inserting an ID that increments (but is not an AUTOINCREMENT)

I have a table that I am trying to insert multiple records into using a select statement.
The ID field is an INT and not autoincremented but I do need to increment in in the INSERT.
The table belongs to a third party product we use for our ERP so I cannot change the property of the ID.
The insert is supposed to create a record in the EXT01100 table for each line item on a particular sales order.
Here is the code I am using:
INSERT INTO EXT01100 (Extender_Record_ID, Extender_Window_ID, Extender_Key_Values_1 , Extender_Key_Values_2, Extender_Key_Values_3)
SELECT (SELECT MAX(EXTENDER_RECORD_ID) + 1 FROM EXT01100), 'ECO_FEE_DIGIT', SOL.LNITMSEQ, SOL.SOPNUMBE, SOL.SOPTYPE
FROM SOP10200 SOL WITH(NOLOCK)
WHERE SOL.SOPTYPE = #InTYPE AND SOL.SOPNUMBE = #INNUMBE AND SOL.LNITMSEQ <> 0
This works on a single line order, but multiple line orders will produce a Primary Key duplicate error so I don't think I can use (SELECT MAX(EXTENDER_RECORD_ID) + 1 FROM EXT01100) in this case.
This is in SQL server.
Any help is greatly appreciated!
You can use row_number to ensure each row has a unique ID, and you need to take an exclusive lock on your main sequence table, and you need to remove your nolock.
INSERT INTO EXT01100 (Extender_Record_ID, Extender_Window_ID, Extender_Key_Values_1 , Extender_Key_Values_2, Extender_Key_Values_3)
SELECT (SELECT MAX(EXTENDER_RECORD_ID) FROM EXT01100 WITH (TABLOCKX)) + ROW_NUMBER() OVER (ORDER BY SOL.LNITMSEQ)
, 'ECO_FEE_DIGIT', SOL.LNITMSEQ, SOL.SOPNUMBE, SOL.SOPTYPE
FROM SOP10200 SOL
WHERE SOL.SOPTYPE = #InTYPE AND SOL.SOPNUMBE = #INNUMBE AND SOL.LNITMSEQ <> 0;
Seconding a recommendation from the comments above, we use Sequences in our production system with no problem. Here's how it looks:
create sequence SQ_Extender_Record_ID
minvalue 1
start with 1
cache 100;
INSERT INTO EXT01100 (Extender_Record_ID, Extender_Window_ID, Extender_Key_Values_1 , Extender_Key_Values_2, Extender_Key_Values_3)
SELECT (next value for SQ_Extender_Record_ID), 'ECO_FEE_DIGIT', SOL.LNITMSEQ, SOL.SOPNUMBE, SOL.SOPTYPE
FROM SOP10200 SOL
WHERE SOL.SOPTYPE = #InTYPE AND SOL.SOPNUMBE = #INNUMBE AND SOL.LNITMSEQ <> 0
Obviously, adjust the min/start values as appropriate for your situation.
If you want, you could add a default constraint to the table/column with this:
alter table EXT01100 add constraint DF_EXT01100__Extender_Record_ID
default (next value for SQ_Extender_Record_ID)
for Extender_Record_ID
You mention that this is in a database whose schema you don't control, so that may not be an option; I mention it for the sake of completeness.

Why do i receive 'Duplicate column name' error when innerjoin-ing 2 tables with similar column name?

CREATE TABLE student_activestudent AS
(
SELECT *
FROM
student
INNER JOIN
activestudent ON activestudent.studentnumber=student.studentnumber
);
I am expecting a table with 2 columns of studentnumber but I received Duplicate error instead --> Duplicate column name 'studentnumber'
A database table must have unique column names.
When you do select * you will get all columns from all tables and studentnumber exists on both student table and activestudent table. So to solve you problem specify the columns you want instead of *
CREATE TABLE student_activestudent AS
(
SELECT
student.studentnumber,
..Other columns..
FROM
student
INNER JOIN
activestudent ON activestudent.studentnumber=student.studentnumber
);
At the very least, studentnumber is duplicated. In general, I strongly recommend that a view list all the columns explicitly. This protects the view if underlying columns change.
That said, if studentnumber is the only column, then you can do:
CREATE TABLE student_activestudent AS
SELECT *
FROM student s JOIN
activestudent ast
USING (studentnumber);
With using, the * does not repeat the join keys.
You cannot select two tables that have same column's name.
The best way is not to select *
Select by column and if the column is same you can put [as]
Example
SELECT student.studentnumber as stuNumber, activestudent.studentnumber as actstuNumber

How do I insert data from one table to another when there is unequal number of rows in one another?

I have a table named People that has 19370 rows with playerID being the primary column. There is another table named Batting, which has playerID as a foreign key and has 104324 rows.
I was told to add a new column in the People table called Total_HR, which is included in the Batting table. So, I have to insert that column data from the Batting table into the People table.
However, I get the error:
Msg 515, Level 16, State 2, Line 183 Cannot insert the value NULL into
column 'playerID', table 'Spring_2019_BaseBall.dbo.People'; column
does not allow nulls. INSERT fails. The statement has been terminated.
I have tried UPDATE and INSERT INTO SELECT, however got the same error
insert into People (Total_HR)
select sum(HR) from Batting group by playerID
I expect the output to populate the column Total_HR in the People table using the HR column from the Batting table.
You could use a join
BEGIN TRAN
Update People
Set Total_HR = B.HR_SUM
from PEOPLE A
left outer join
(Select playerID, sum(HR) HR_SUM
from Batting
group by playerID) B on A.playerID = B.playerID
Select * from People
ROLLBACK
Notice that I've put this code in a transaction block so you can test the changes before you commit
From the error message, it seems that playerID is a required field in table People.
You need to specify all required fields of table People in the INSERT INTO clause and provide corresponding values in the SELECT clause.
I added field playerID below, but you might need to add additional required fields as well.
insert into People (playerID, Total_HR)
select playerID, sum(HR) from Batting group by playerID
It is strange, however, that you want to insert rows in a table that should already be there. Otherwise, you could not have a valid foreign key on field playerID in table Batting... If you try to insert such rows from table Batting into table People, you might get another error (violation of PRIMARY KEY constraint)... Unless... you are creating the database just now and you want to populate empty table People from filled/imported table Batting before adding the actual foreign key constraint to table People. Sorry, I will not question your intentions. I personally would consider to update the query somewhat so that it will not attempt to insert any rows that already exist in table People:
insert into People (playerID, Total_HR)
select Batting.playerID, sum(Batting.HR)
from Batting
left join People on People.playerID = Batting.playerID
where People.playerID is null and Batting.playerID is not null
group by playerID
You need to calculate the SUM and then join this result to the People table to bring into the same rows both Total_HR column from People and the corresponding SUM calculated from Batting.
Here is one way to write it. I used CTE to make is more readable.
WITH
CTE_Sum
AS
(
SELECT
Batting.playerID
,SUM(Batting.HR) AS TotalHR_Src
FROM
Batting
GROUP BY
Batting.playerID
)
,CTE_Update
AS
(
SELECT
People.playerID
,People.Total_HR
,CTE_Sum.TotalHR_Src
FROM
CTE_Sum
INNER JOIN People ON People.playerID = CTE_Sum.playerID
)
UPDATE CTE_Update
SET
Total_HR = TotalHR_Src
;

Insert Into Temp Table from another Temp Table throws error

I've seen a few of the questions and answers for this, but they all seem different than my problem. I am trying to insert into a temp table with a where clause from a real table to the id on a different temp table. Let me explain
Here is my first insert. It creates a temp table based on the parameters
Insert Into #programs (programs_id, state_program_ID, org_no, bldg_no)
Select programs_ID, state_program_ID, org_no, bldg_no
From programs as p
Where p.org_no = #org_no
And p.bldg_no = #bldg_no
And p.school_yr = #school_year
This returns a table that has a flat list of programs. Programs are offered at the school level and are slightly modified from the related state_program.
Then I need a list of all students that have taken the program from the program_student table.
Insert Into #programStudent (programs_id , ss_id, status_id)
Select ps.programs_id, ps.ss_id, ps.status_id
From program_student as ps
Where ps.programs_id = #programs.program_id
--'#programs.program_id' throws error
This would meet my need having all students that have taken any of the programs offered by the school at that school year.
The full error is
The multi-part identifier '#programs.program_id' could not be bound.
You are not addressing the #programs table in your second query. that last line will have to change to something like this:
WHERE EXISTS (SELECT TOP 1 1 FROM #Programs WHERE #Programs.programs_id = ps.program_id)
This is how you must address temp tables - they do not become variables in your current script - they are actual tables which get cleaned up after you disconnect. As such, they need to be introduced as tables - in a from clause, for each query that needs to reference them.
You had a typo when defining the #programs table - you called the column programs_id and not program_id. Just fix it, and you should be fine:
Insert Into #programs (program_id, state_program_ID, org_no, bldg_no)
-- "s" removed Here ---------^
Select programs_ID, state_program_ID, org_no, bldg_no
From programs as p
Where p.org_no = #org_no
And p.bldg_no = #bldg_no
And p.school_yr = #school_year

Trying to delete when not exists is not working. Multiple columns in primary key

I am currently trying to delete from Table A where a corresponding record is not being used in Table B. Table A has Section, SubSection, Code, Text as fields, where the first three are the Primary Key. Table B has ID, Section, SubSection, Code as fields, where all four are the Primary Key. There are more columns, but they are irrelevant to this question...just wanted to point that out before I get questioned on why all columns are part of the Primary Key for Table B. Pretty much Table A is a repository of all possible data that can be assigned to a entity, Table B is where they are assigned. I want to delete all records from table A that are not in use in Table B. I have tried the following with no success:
DELETE FROM Table A
WHERE NOT EXISTS (SELECT * from Table B
WHERE A.section = B.section
AND A.subsection = B.subsection
AND A.code = b.code)
If I do a Select instead of a delete, I get the subset I am looking for, but when I do a delete, I get an error saying that there is a syntax error at Table A. I would use a NOT IN statement, but with multiple columns being part of the Primary Key, I just don't see how that would work. Any help would be greatly appreciated.
In sql server,when using not exists, you need to set an alias for the table to be connected, and in the delete statement, to specify the table to delete rows from.
DELETE a FROM Table_A a
WHERE NOT EXISTS (SELECT * from Table_B b
WHERE a.section = b.section
AND a.subsection = b.subsection
AND a.code = b.code)
Please try :
DELETE FROM Table A
WHERE NOT EXISTS (SELECT 1 from Table B
WHERE A.section = B.section
AND A.subsection = B.subsection
AND A.code = b.code)
1 is just a placeholder, any constant/single non-null column will work.
Try something like this:
delete from Table_A
where (section, subsection, code) not in (select section,
subsection,
code
from Table_B)