I'm trying to insert values from table 2 (in the pacp2 column) into table 1 (in the pacp column) where the facility_id from both tables matches and where table1 pacp column is null. I get the following error:
Msg 4104, Level 16, State 1, Line 5 The multi-part identifier
"table1.FACILITYID" could not be bound. Msg 4104, Level 16, State 1,
Line 5 The multi-part identifier "table1.pacp" could not be bound.
Does anyone know why this isn't working? I've tried using alias for both tables but that won't work.
INSERT INTO database.dbo.table1 (pacp)
SELECT pacp2
FROM table2
WHERE database.dbo.table1.facility_id = database.dbo.table2.facility_id
AND database.dbo.table1.pacp IS NULL
The description of your question suggests that you are looking for an update rather than an insert.
Here is one option using an updatable cte:
with cte as (
select t1.pacp, t2.pacp2
from table1 t1
inner join table2 t2 on t1.facility_id = t2.facility_id
where t1.pacp is null
)
update cte set pacp = pacp2
For rows of table1 where pacp is null, the query attempts to retrieve the value of pacp2 from table2, using facility_id for matching. If it succeeds, then it updates the corresponding row in table1 and sets pacp to the value of pacp2.
Related
This question already has an answer here:
How can I implement this plan in C# with SQL command?
(1 answer)
Closed 7 years ago.
I'm write this query:
INSERT INTO Customer_Care_Database_Analysis_Center
SELECT code_markaz, name_markaz
FROM Customer_Care_Database_Analysis_TEMP
WHERE NOT EXISTS (SELECT 1
FROM Customer_Care_Database_Analysis_TEMP ti
WHERE ti.code_markaz = Customer_Care_Database_Analysis_Center.code_markaz);
but when I run that query I get this error:
Msg 4104, Level 16, State 1, Line 6
The multi-part identifier "Customer_Care_Database_Analysis_Center.code_markaz" could not be bound.
What happened? How can I solve that?
Not sure what's happening with the answers, but I would assume you want to insert the rows that don't exist in the table (field code_markaz) and that you can do this way:
INSERT INTO Customer_Care_Database_Analysis_Center (code_markaz, name_markaz)
SELECT code_markaz, name_markaz
FROM Customer_Care_Database_Analysis_TEMP tmp
WHERE NOT EXISTS (SELECT 1
FROM Customer_Care_Database_Analysis_Center c
WHERE c.code_markaz = tmp.code_markaz)
I also added the columns in the insert statement. If there's only 2 columns in the table it will work without them.
The table being inserted into (Customer_Care_Database_Analysis_Center) does not have a column called code_markaz.
Name the target columns:
INSERT INTO Customer_Care_Database_Analysis_Center (some_col_1, some_col_2)
SELECT code_markaz, name_markaz
FROM ...
The error comes due to Customer_Care_Database_Analysis_Center table not reference in you not exist inner query.
As above suggested you have to give either alias or join that table.
...FROM Customer_Care_Database_Analysis_TEMP t
JOIN Customer_Care_Database_Analysis_Center ccdac
NOT EXISTS (
SELECT 1
FROM Customer_Care_Database_Analysis_TEMP ti
WHERE ti.code_markaz = CCDAC.code_markaz);
I'm trying to insert new_file_name column data from document_image_volume1_rename_temp table into the document_image_volume1_rename table. Both tables have a column document_image_id.
USE myDatabase
INSERT INTO document_image_volume1_rename (new_file_name)
SELECT
new_file_name
FROM
document_image_volume1_rename_temp
WHERE
document_image_volume1_rename.document_image_id = document_image_volume1_rename_temp.document_image_id
Here's the error message:
Msg 4104, Level 16, State 1, Line 10
The multi-part identifier "document_image_volume1_rename.document_image_id" could not be bound.
I think what you are actually looking for is an UPDATE query not an INSERT query. If you don't want to add new rows but just modify a column in existing rows, try this:
UPDATE t2
SET t2.new_file_name = t1.new_file_name
FROM document_image_volume1_rename_temp t1 INNER JOIN document_image_volume1_rename t2 ON t1.document_image_id = t2.document_image_id
A handy reference on UPDATE query syntax by DBMS can be found in Eric's answer here.
I have to update a column in table_b when a row inserted to table_a.
Query
CREATE TRIGGER trg_Update
ON table_a FOR INSERT
AS
UPDATE table_b
SET Sold_Qty=inserted.qty
WHERE OrdNo=inserted.OrdNo;
I am getting the following error.
Msg 4104, Level 16, State 1, Procedure trg_Update, Line 6
The multi-part identifier "inserted.OrdNo" could not be bound.
Try this in place of the UPDATE query:
UPDATE table_b
SET Sold_Qty=inserted.qty
FROM table_b b
INNER JOIN inserted ON b.OrdNo=inserted.OrdNo;
I have four tables I need to join in the following query attemp:
SELECT PROJECT.PROJECT_ID
, PROJECT_STATUS.PRO_STATUS_NAME
, PROJECT.PROJECT_NAME
, SKILL.SKILL_NAME
FROM PROJECT_STATUS
INNER JOIN PROJECT ON PROJECT_STATUS.PRO_STATUS_ID = PROJECT.PRO_STATUS_ID
INNER JOIN PRO_SKILL ON SKILL.SKILL_ID = PRO_SKILL.SKILL_ID
AND PRO_SKILL.PROJECT_ID = PROJECT.PROJECT_ID;
Unfortunately the PRO_SKILL table uses a composite key so I keep getting the following error when trying to run the query:
Msg 4104, Level 16, State 1, Line 6 The multi-part identifier
"SKILL.SKILL_ID" could not be bound. Msg 4104, Level 16, State 1, Line
1 The multi-part identifier "SKILL.SKILL_NAME" could not be bound.
Is there a way to join properly with a table that uses a composite key?
PRO_SKILL HAS A COMPOSITE KEY MADE OF:
PROJECT_ID AND SKILL_ID
SKILL ONLY HAS A REGULAR PK MADE OF SKILL_ID
Thank you for your help in advance!.
You missed out the Skills table , Try this....
SELECT PROJECT.PROJECT_ID
, PROJECT_STATUS.PRO_STATUS_NAME
, PROJECT.PROJECT_NAME
, SKILL.SKILL_NAME
FROM PROJECT_STATUS
INNER JOIN PROJECT ON PROJECT_STATUS.PRO_STATUS_ID = PROJECT.PRO_STATUS_ID
INNER JOIN PRO_SKILL ON PRO_SKILL.PROJECT_ID = PROJECT.PROJECT_ID
INNER JOIN SKILL ON SKILL.SKILL_ID = PRO_SKILL.SKILL_ID
I can't quite figure out how to update the last inserted row using table triggers.
I tried this:
create TRIGGER [dbo].[t_checkAucEquFields]<br />
ON [dbo].[_aucEquUpdateLog]
AFTER INSERT
as
update a set inserted.[Status] = coalesce(pes.id,'22')
from [_aucEquUpdateLog] a
left join v_pobEquStatus pes on pes.statusDescr = inserted.[Status]
I would like the text of inserted.Status - ex being "In stock" to be replaced with the integer pes.id. If the join cannot be made, I would like it to default to value 22.
I get this error when trying above query:
Msg 4104, Level 16, State 1, Procedure t_checkAucEquFields, Line 7
The multi-part identifier "inserted.Status" could not be bound.
I might be way off, but I am quite lost as of how to do it in a proper way?
You need to join the inserted table, and update the actual table instead of the inserted table
Try this:
CREATE TRIGGER [dbo].[t_checkAucEquFields]
ON [dbo].[_aucEquUpdateLog]
AFTER INSERT
as
begin
UPDATE a
SET
[Status] = coalesce(pes.id,'22')
FROM
[_aucEquUpdateLog] a
JOIN
inserted i
ON
i.[your primary key] = a.[your primary key]
LEFT JOIN
v_pobEquStatus pes
ON
pes.statusDescr = i.[Status]
end