I'm trying to copy the column 'REGEX_MIN_DATE' from one table to another, matching on another column, 'MSCAD_ID'. Both tables have these columns. Why does the following code fail?
UPDATE dbo.std_cyber_mscad_cases
SET REGEX_MIN_DATE = dbo.temp_min_dates.REGEX_MIN_DATE
WHERE MSCAD_ID = dbo.temp_min_dates.MSCAD_ID
I'm getting the error 'The multi-part identifier "dbo.temp_min_dates.MSCAD_ID" could not be bound.'
UPDATE A
SET REGEX_MIN_DATE = B.REGEX_MIN_DATE
FROM dbo.std_cyber_mscad_cases A
INNER JOIN dbo.temp_min_dates B ON A.MSCAD_ID = B.MSCAD_ID
Related
I have created a method for fetching data from tables.
My importing parameters are IV_RCP_NO, IV_VERS, IV_ALT and exporting parameter is ET_TABLE.
Now I am using INNER JOIN for joining 4 tables but the requirement says it should be done using FOR ALL ENTRIES clause.
Can someone please help me out to proceed with it?
Here is my code:
SELECT RCP_TBL~SEARCH_TERM PLNT_TBL~PLANT DESCR_TBL~DESCR RCP_STATUS~DESCR
INTO TABLE ET_TABLE
FROM ESTRH AS ESTRH_TBL
INNER JOIN /PLMB/RCP_RECIPE AS RCP_TBL
ON ESTRH_TBL~RECN = RCP_TBL~SUBRECN
INNER JOIN /PLMB/SAM_NODE_T AS RCP_STATUS
ON RCP_STATUS~STATUS_ID = RCP_TBL~STATUS
INNER JOIN /PLMB/RCP_PLNT AS PLNT_TBL
ON RCP_TBL~RCP_GUID = PLNT_TBL~RCP_GUID
INNER JOIN /PLMB/RCP_DESCR AS DESCR_TBL
ON PLNT_TBL~RCP_GUID = DESCR_TBL~OBJECT_GUID
AND RCP_TBL~RCP_GUID = DESCR_TBL~OBJECT_GUID
WHERE SUBID = IV_RCP_NO AND ALT_NO = IV_ALT AND VERS_NO = IV_VERS.
If the join can be done without for all entries then do that. This will yield best performance.
For all entries is a tool to simplify queries where you already have part of the data in an internal table, from some previously executed code or pre-computation. It effectively results in a series of independent selects whose results are merged after individual completion - meaning this results in multiple database roundtrips, which can drastically worsen performance.
If you have specific requirements to apply the for all entries, you should clarify which part of the data is already there and needs to be joined that way. Otherwise any suggestion from StackOverflowers will remain inefficient guesswork.
You can refer the below code-
* Local structure
TYPES: BEGIN OF ty_estrth,
subid TYPE esesubid,
recn TYPE eserecn,
END OF ty_estrth.
* Internal table
DATA: lt_estrh TYPE STANDARD TABLE OF ty_estrth.
SELECT subid recn FROM estrh
INTO TABLE lt_estrh
WHERE subid = iv_rcp_no.
IF lt_estrh IS NOT INITIAL.
SELECT * FROM /plmb/rcp_recipe
INTO TABLE lt_rcp_recipe "Internal table of type /PLMB/RCP_RECIPE
FOR ALL ENTRIES IN lt_estrh
WHERE subrecn = lt_estrh-recn
AND alt_no = iv_alt AND vers_no = iv_vers.
IF lt_rcp_recipe IS NOT INITIAL.
SELECT * FROM /plmb/sam_node_t
INTO TABLE lt_sam_node_t "Internal table of type /PLMB/SAM_NODE_T
FOR ALL ENTRIES IN lt_rcp_recipe
WHERE status_id = lt_rcp_recipe-status.
SELECT * FROM /plmb/rcp_plnt
INTO TABLE lt_rcp_plnt " Internal table of type /PLMB/RCP_PLNT
FOR ALL ENTRIES IN lt_rcp_recipe
WHERE rcp_guid = lt_rcp_recipe-rcp_guid.
SELECT * FROM /plmb/rcp_descr
INTO TABLE lt_rcp_descr " Internal table of type /PLMB/RCP_DESCR
FOR ALL ENTRIES IN lt_rcp_recipe
WHERE rcp_guid = lt_rcp_recipe-rcp_guid.
ENDIF.
ENDIF.
You'll get your data in below internal tables
lt_rcp_recipe
lt_sam_node_t
lt_rcp_plnt
lt_rcp_descr
Better declare a local structure with specific fields which you want to read, as i declared above. After that you have to read and fill data in exporting table ET_TABLE.
I need to update an existing table (First) with a new column (Ticket_No_Calc). Basically, there are two columns (Doc_Header and Doc_No) that I have to lookup/refer to in the first table against the second table (Doc_No) column.
The desired column Ticket_No_Calc result is retrieved from the column Ticket_No.
Below is the screenshot of sample data of the first table
and this is the screenshot of sample data of the second table
Currently, in Excel, I use the following formula to get the desired column in the existing table
=IFERROR(VLOOKUP(FIRST!B2,'SECOND'!A:B,1,0),(VLOOKUP(FIRST!C2,'SECOND'!A:B,1,0)))
I am not sure how to do this in SQL. I know that I can get this done in SQL with the following formula if I only have to refer to one column Doc_Header:
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
LEFT JOIN Second AS B ON A.Doc_Header = B.Doc_NO ;
But I need to refer to two columns in the existing table. I have been researching for sometimes to get the correct command but I can't seem to be able to crack it.
Following statement may help
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
INNER JOIN Second AS B ON (A.Doc_Header = B.Doc_NO) OR (A.Doc_NO = B.Doc_NO) ;
As per the sample data provided this is query seems incorrect. But as per the comments mentioned in your question it is better to perform this in 2 steps.
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
LEFT JOIN Second AS B ON A.Doc_Header = B.Doc_NO ;
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A
LEFT JOIN Second AS B ON A.DOC_NO=B.DOC_NO
WHERE A.Doc_Header <> B.Doc_NO
Try this:
UPDATE A
SET A.Ticket_No_Calc = B.Ticket_No
FROM First AS A ,Second AS B where A.Doc_Header = B.Doc_NO or A.Doc_NO = B.Doc_NO
I have two SQL Server tables where I need to add records from one table to the next. If the unique identifier already exists in the target table, then update the record to the data coming from source table - If the unique identifier doesn't exist, then insert the entire new record into the target table.
I seem to have gotten the initial part to work where I update the records in target table but the the part where I would INSERT new records does not seem to be working.
if exists (
select 1
from SCM_Top_Up_Operational O
join SCM_Top_Up_Rolling R ON O.String = R.string
)
begin
update O
set O.Date_Added = R.Date_Added,
O.Real_Exfact = R.Real_Exfact,
O.Excess_Top_Up = R.Excess_Top_Up
from SCM_Top_Up_Operational O
join SCM_Top_Up_Rolling R on O.String = R.String
where O.String = R.string and R.date_added > O.date_added
end
else
begin
insert into SCM_Top_Up_Operational (String,Date_Added,Real_Exfact,Article_ID,Excess_Top_Up,Plant)
select String,Date_Added,Real_Exfact,Article_ID,Excess_Top_Up,Plant
from SCM_Top_Up_Rolling
end
If I followed you correctly, you should be able to solve this with a single SQL query, using SQL Server MERGE syntax, available since SQL Server 2008.
From the documentation:
Runs insert, update, or delete operations on a target table from the results of a join with a source table. For example, synchronize two tables by inserting, updating, or deleting rows in one table based on differences found in the other table.
Consider the following query:
MERGE
SCM_Top_Up_Operational O
USING SCM_Top_Up_Rolling R ON (O.String = R.string)
WHEN MATCHED
THEN UPDATE SET
O.Date_Added = R.Date_Added,
O.Real_Exfact = R.Real_Exfact,
O.Excess_Top_Up = R.Excess_Top_Up
WHEN NOT MATCHED BY TARGET
THEN INSERT ( String, Date_Added, Real_Exfact, Article_ID, Excess_Top_Up, Plant)
VALUES (R.String, R.Date_Added, R.Real_Exfact, R.Article_ID, R.Excess_Top_Up, R.Plant)
I am trying to copy the values of one column (reg_id) in members_profile to the empty (reg_id)
column in reg_members.
On executing this query i am getting the multi-part identifier could not be bound error.
INSERT INTO testing.db1.members_profile(reg_id)
SELECT reg_id
from testing.db1.reg_members
WHERE testing.db1.members_profile.loginname = testing.db1.reg_members.loginname;
If you're trying to update an existing row (or rows), you should be writing an UPDATE statement, not an INSERT. An INSERT adds rows, an UPDATE updates them.
I believe what you're looking for is an UPDATE based on a JOIN (and you should use table aliases so that you don't have to repeat database.table.column on every single reference and clause):
UPDATE r
SET r.reg_id = m.reg_id
FROM testing.db1.members_profile AS m
INNER JOIN testing.db1.reg_members AS r
ON m.loginname = r.loginname;
I have 2 tables with same table structure. Table A is having all transaction with 3 unique key in each record. Table B have only condition base record only.
I want compare both tables if Table B has matching record than I want to update and Table B have not matching record than insert in Table B.
Can you please suggest best way to do it like ssis or any thing else
The easiest way is a MERGE statement:
MERGE INTO Table_B
USING Table_A
ON TableA.ID1 = Table_B.ID1 AND TableA.ID2 = Table_B.ID2 AND TableA.ID3 = Table_B.ID3
WHEN MATCHED THEN UPDATE SET A = Table_A.A, B = Table_A.B -- Etcetera...
WHEN NOT MATCHED THEN INSERT (A, B) VALUES (Table_A.a, Table_A.B) -- Etcetera...
WHEN NOT MATCHED BY SOURCE THEN DELETE -- If Necessary...
;
By the way, don't forget the ";" at the end. SQL Server doesn't usually need them, but a MERGE does.