Stored Procudure SQL error in Node `mssql` package - sql

I am trying to recreate a Sql Server stored procedure in my Node app, which uses the mssql npm package. Right now, when I try and run the following query, I get an incorrect SQL error:
UPDATE
dbo.C2980251
RIGHT JOIN CFRTR...dbo.UPR10301 ON dbo.C2980251.BACHNUMB = dbo.UPR10301.BACHNUMB
SET
dbo.C2980251.UPRBCHOR = dbo.UPR10301.uprbchor,
dbo.C2980251.BACHNUMB = dbo.UPR10301.bachnumb,
dbo.C2980251.TRU_TYPE_ID = "132"
WHERE
(((dbo.C2980251.BACHNUMB) Is Null))
The specific error is this:
Incorrect syntax near the keyword 'RIGHT'.
To clarify, CFRTR, refers to the db being targeted. All of the tables being used here are from the same db.
It's not clear to me what the error is here. How should this be written?

The correct syntax is:
UPDATE c
SET UPRBCHOR = u.uprbchor,
BACHNUMB = u.bachnumb,
TRU_TYPE_ID = 132
FROM dbo.C2980251 c LEFT JOIN
CFRTR.dbo.UPR10301 u
ON u.BACHNUMB = c.BACHNUMB
WHERE u.BACHNUMB Is Null
Notes:
SQL Server requires a separate FROM clause.
Table aliases make the query much easier to write and read.
A LEFT JOIN makes much more sense here than a RIGHT JOIN. The database cannot update records that don't exist.
u.uprbchor and u.bachnumb are going to be NULL because the WHERE clause only keeps rows with no matches.

Related

SQL Update Query with two levels of join?

I'm trying to put together an update query where the source field is two tables away. Here's a query I got to work in the SQLCMD window which pulls both fields when they don't match:
SELECT
Act_det.Shift AS Shift_a, Inc_main.Shift AS Shift_b,
FROM
dbo.Act_det
INNER JOIN
dbo.Act_main ON Act_det.Activ_id = Act_main.Activ_id
INNER JOIN
dbo.Inc_main ON Act_main.Inci_no = Inc_main.Inci_no
WHERE
Act_det.Shift <> Inc_main.Shift;
I want to set Act_det.Shift = Inc.main_Shift when they don't match.
The relationships are that the Act_det table has a field Activ_id which matches to a single record in the Act_main table. The Act_main then has an inci_no which matches to a single record in the Inc_main table which has the "shift" value that we want to query and update "shift" in the act-det table with if they don't already match.
I've tried putting together an update query based on other responses found here and other places but I can't figure out how to make an Update/Set and the joins coexist.
UPDATE dbo.Act_det
SET db.act_det.shift = dbo.inc_main.shift,
INNER JOIN dbo.Act_main ON Act_det.Activ_id = Act_main.Activ_id
INNER JOIN dbo.Inc_main ON Act_main.Inci_no = Inc_main.Inci_no
WHERE Act_det.Shift <> Inc_main.Shift;
This bombs out with
Incorrect syntax near the keyword 'INNER'.
I can't just update every record even if they already match, since at the application level every Incident that gets updated will get flagged for re-submission to the state, and we'll end up with like 15,000 records going up for audit.
I did post the improper "latest" version of my attempts. (When cutting and pasting from OneNote it wants to do it as a picture, so I ended up using the up arrow in the SQLCMD and landed on a prior attempt.) I had it formatted with a FROM and without the extraneous comma. BUT I never caught me having dbo instead of db.
From the comments:
I presume you're using SQL Server - in which case you're missing a 'FROM' (and have an extra comma). Also you have a 'db' where it should be either 'dbo' or blank in the 'set' area. It should start as Update dbo.Act_det set shift = dbo.inc_main.shift FROM dbo.Act_det INNER JOIN dbo.Act_main etc –
seanb

Arel UpdateManager with Join creates invalid SQL - how to rephrase?

Apparently there is an issue in Arel core, where Arel::UpdateManager, when performing a column update on a join, does not generate the table name for the update column. It results in invalid SQL.
I ran into this in a Rails 5.2 app, where I had an SQL literal UPDATE statement that I was trying to rephrase in Arel.
UPDATE observations o, names n
SET o.lifeform = n.lifeform
WHERE o.name_id = n.id
AND o.lifeform != n.lifeform
In Arel, i wrote this:
names = Name.arel_table
obs = Observation.arel_table
join_source = Arel::Nodes::JoinSource.new(
obs, [obs.create_join(names)]
)
Arel::UpdateManager.new.
table(join_source).
where(obs[:id].eq(names[:id]).
and(obs[:lifeform].not_eq(names[:lifeform]))).
set([[obs[:lifeform], names[:lifeform]]])
This returns:
Mysql2::Error: Column 'lifeform' in field list is ambiguous:
The problem is at the end. The SQL generated from this does not specify the table where the column is to be set.
UPDATE `observations`
INNER JOIN `names`
SET `lifeform` = `names`.`lifeform`
WHERE (`observations`.`id` = `names`.`id`)
AND (`observations`.`lifeform` != `names`.`lifeform`)
Elsewhere, Arel-generated SQL usually qualifies columns with table names to avoid ambiguity. But the source code for update_manager.rb definitely uses Nodes::UnqualifiedColumn.new(column). (I have added my description to the Arel issue on GitHub.)
For now I'd maybe like to rephrase my Arel some other way. Is there a way to force Arel to quote the table name, similar to connection.quote_table_name?
Or would using a CTE be appropriate?
I guess one way to do this is with ActiveRecord's connection.update_all.
names = Arel::Table.new(:names)
Observation.joins(:name).
 where(names[:correct_spelling_id].not_eq(nil)).
  update_all("`observations`.`name_id` = `names`.`correct_spelling_id`")
This generates the desired SQL:
UPDATE `observations`
INNER JOIN `names`
ON (`observations`.`name_id` = `names`.`correct_spelling_id`)
AND (`names`.`correct_spelling_id` IS NOT NULL)
SET `observations`.`name_id` = `names`.`correct_spelling_id`
I think this is the way to go.

Using a select statement as criteria for an update query

I'm trying to put together a query that updates a field within a table. I'm attempting to run a sub select query that gives me a number, and then use that number that resulted from the sub-query as part of the criteria for the update query.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =[tbl_Foundation_Account].[Client_ID]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID =
[2_Import_tbl_AWCDSU].[ECPD Profile ID]))
My issue is I keep receiving this error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
Am I using the sub-query incorrectly? When I've received this error before, it's been because of some ambiguity in the table or field names, but this time I've checked for all that and it should be fine. Can anyone explain what SQL sin I have committed?
On the error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
This is because the table column [tbl_Foundation_Account].[Client_ID] does not exists in the scope of outer UPDATEquery .
The only table the outer query has an inkling about is [2_import_VZW_tbl_SMTN] and it does not have a column like [tbl_Foundation_Account].[Client_ID].
It is akin to writing a column name with a typo or like you said
When I've received this error before, it's been because of some
ambiguity in the table or field names
Please try a query like below.
Note that I am using Inner query syntax and ensuring that a single value is returned by using
select top 1 [Client_ID]
in the inner query. rest of the query syntax is same.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =
(
select top 1 [Client_ID]
from [tbl_Foundation_Account]
where [Foundation_Account_ID] =
(
Select TOP 1 a.Foundation_Account_ID
FROM tbl_Foundation_Account a
INNER JOIN [2_Import_tbl_AWCDSU] b
ON a.Foundation_Account_ID = b.[ECPD Profile ID]
)
)
Another poster submitted this answer earlier, but then deleted it. I was able to try it before they deleted it and it works exactly how I needed it to work. I will use this as the right answer unless someone else can tell me why this is a bad Idea.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] = [tbl_Foundation_Account].[Client_ID]
from [tbl_Foundation_Account]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID = [2_Import_tbl_AWCDSU].[ECPD Profile ID]))

Updating Field Based on Another Table

As someone new to SQL can you please point me in the right direction. I know the following is wrong but I am not sure why.
UPDATE cus
SET cus.leg_no = new.leg_no
WHERE cus.c_no = new.c_no
The cus table currently has null in the leg_no. I want to update it from the new table. I will be joining on c_no which is in both tables.
I have tried searching the web but I am getting further confused. This has lead me to think I need FROM but something is telling me that is when using SELECT rather than UPDATE.
UPDATE cus,new
SET cus.leg_no = new.leg_no
WHERE cus.c_no = new.c_no
Here's the standard way to do it:
UPDATE cus
SET cus.leg_no = (select new.leg_no from new WHERE cus.c_no = new.c_no)
Here's the SQL Server/MS access dialect way to do it:
UPDATE cus
SET cus.leg_no = new.leg_no
FROM cus
INNER JOIN new
ON cus.c_no = new.c_no
Note that, with the standard method, if there are multiple rows in new that match a particular row from cus, you'll get an error message. Whereas with the SQL Server/MS access dialect form, the system will arbitrarily select one of the rows, and issue no warning or error.

Syntax error (missing operator) when using UPDATE with INNER JOIN

I'm trying to execute this SQL query on an MS Access DB:
UPDATE WW_B
SET WW_B.WWtype_ID=1
INNER JOIN New_data
ON WW_B.StdNr = New_data.StdNr;
But I get the following error:
[Microsoft][ODBC Microsoft Access Driver]
Syntax error (missing operator) in expression 1 INNER JOIN New_data on WW_B.StdNr = New_data.StdNr.
I don't see where any operator is needed, since I don't use any parentheses or quotation marks.
I've also tried WWtype_ID='1' and WWtype_ID="1" and got the same error.
What am I doing wrong?
I was having the same problem and found this question while searching for an answer. Luckily I was able to find a solution while digging through some Access queries at work.
This is just another situation where MS Access does not play nice with standard SQL syntax.
The other answers are incorrect. You do not need a FROM clause, Access will just give you the same error message. Where you ran into the error was where you placed the JOIN. It seems intuitive that you would have FROM...JOIN... But this is MS Access where working with SQL is never intuitive.
In Access, UPDATE seems to take the place of FROM. Therefore, you add the JOIN statement to the end of the UPDATE clause.
UPDATE WW_B
INNER JOIN New_data
ON WW_B.StdNr = New_data.StdNr
SET WW_B.WWtype_ID=1;
You can also add a WHERE clause after the SET statement if desired.
You need a FROM clause when using an INNER JOIN on an UPDATE:
UPDATE WW_B
SET WW_B.WWtype_ID = 1
FROM WW_B
INNER JOIN New_data
on WW_B.StdNr = New_data.StdNr
You are missing the FROM clause
UPDATE WW_B SET WW_B.WWtype_ID=1 FROM <your table> INNER JOIN New_data on WW_B.StdNr = New_data.StdNr