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;
Related
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
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'm quite new to SQL and hope you can help me with my problem.
I have a table called Order_Status_Form_3 with the columns Order_ID (KEY), Customer_ID, Customer_Unique_ID, Status(KEY) and Date.
The table is filled, except for the Customer_Unique_ID Column.
To fill this Column I need to reference the Customer table where the Customer_ID is linked to the Customer_Unique_ID, so the right IDs cover the right places. When the Customer_ID in Order_Status_Form_3 equals the Customer_ID in the Customer table the given Customer_Unique_ID shall be inserted into the Customer_Unique_ID column in Order_Status_Form_3.
I tried to combine an INSERT INTO with a SELECT and INNER JOIN, but received an error message that says:
"cannot insert NULL or update to NULL: Order_ID".
I guess it's not clear for the program where to insert the values found and it tries to insert into all columns. I searched for similar problems but could not find any satisfying answers for my specific problem.
Here's the code I used:
Insert Into "HXE_109"."Order_Status_Form_3" ("Customer_Unique_ID")
Select customer."customer_unique_id"
From "HXE_109"."Customer" As customer
Inner Join "HXE_109"."Order_Status_Form_3" As OrderStatus3
On OrderStatus3."Customer_ID" = customer."customer_id"
I tried to specify the place to insert the values found by attaching a WHERE at the end, but received the same error.
Where OrderStatus3."Customer_ID" = customer."customer_id"
Does anyone know how to solve this issue and can tell me where my mistake is?
Thanks in advance for reading this long question and leaving an answer.
Edit
I tried using update but it seems like I cannot get it right.
Update "HXE_109"."Order_Status_Form_3"
Set "Customer_Unique_ID" = (Select customer."customer_unique_id"
From "HXE_109"."Customer" As customer
Inner Join "HXE_109"."Order_Status_Form_3" As OrderStatus3
On OrderStatus3."Customer_ID" = customer."customer_id")
Now I get the following error:
single row query returns more than one row
Do I need to use a Where condition here?
Sorry for my stupidity. :(
As I could follow the comments, what you want to do is running an UPDATE statement
Please check following DML command
Update "HXE_109"."Order_Status_Form_3"
Set
"Customer_Unique_ID" = customer."customer_unique_id"
From "HXE_109"."Order_Status_Form_3" As OrderStatus3
Inner Join "HXE_109"."Customer" As customer
On OrderStatus3."Customer_ID" = customer."customer_id"
If I try to explain the error messages:
"cannot insert NULL or update to NULL: Order_ID".
That is related with a field defined as NOT NULL. So in your target table ORDER_ID is defined as "not null", so in INSERT you have to provide a value for it or define it as an identity field in your HANA table definition
The second error: single row query returns more than one row
This is related with the case where SQL Engine expects a value not a set of values.
So the SELECT statement that you assign the results to Customer_Unique_ID field returns more than 1 value. In this case SQL engine raises an exception
What would be the update statement for all records WHERE USID = 49?
This is what I currently have:
INSERT INTO ADDRESSES (AddressLine1, AddressLine2, PostCode)
VALUES ('1A','2B', '3C') WHERE USID = 49;
But I'm getting an error "Incorrect syntax near the keyword WHERE." What am I doing wrong here?
TL;DR:
INSERT is used when you want to create new records in a table. It appears that you do not actually want to add new records. If you want to modify existing records, use UPDATE instead:
UPDATE ADDRESSES
SET AddressLine1 = '1A', AddressLine2 = '2B', PostCode = '3C'
WHERE USID = 49;
Explanation why INSERT … WHERE is invalid syntax:
The purpose of a WHERE clause is to exclude all rows but those that meet the specified condition. When INSERT-ing, there is no need to look at any existing rows at all; you want to create new rows, and these are completely independent from any existing records. Therefore INSERT does not require WHERE clause. In fact it doesn't allow it because it wouldn't make any sense at all.
With UPDATE, on the other hand, a WHERE clause makes sense because you are dealing with existing rows, and you might not want to change all existing rows, but only a subset. The WHERE clause allows you to define the subset of rows that will be updated.