First of all, I'm sorry because I know there are many questions regarding "Ambiguous column name" error here in Stack but I'm really newbie in SQL and doing my first queries using SQLite I found this error. After analyzing other several questions I didn't find a solution for my problem (maybe, it's here for sure but I couldn't find) and this is:
When I use update, set and doing any operation such as the example I put... Well, the error appears and I don't understand the problem. I tried some options but nothing.
update nota
set subtot=cantidad*precio
from nota inner join producto on producto.clave_prod=nota.clave_prod1;
"Cantidad" column is on table called "nota" and "precio" column is on table called "producto" and both are linked between foreign keys.
Thank you so much in advance!
Your syntax is wrong.
There is no need to refer to the updated table after FROM and the ON clause must be replaced with a WHERE clause.
This is the correct syntax (if your SQLite version is 3.33.0+) for a join-like UPDATE satement:
update nota
set subtot = nota.cantidad * producto.precio -- subtot must not be qualified with nota.
from producto
where producto.clave_prod = nota.clave_prod1;
or with aliases:
update nota AS n
set subtot = n.cantidad * p.precio -- subtot must not be qualified with n.
from producto AS p
where p.clave_prod = n.clave_prod1;
Related
I know a similar question to this has been asked a fair few times on here, but none seem to give me the answer I need - specifically because I am talking about the relationship between SQL and FileMaker.
See, I'm not actually using an OUTPUT clause at all in my Trigger! But FileMaker seems to either think I am, or insert something itself as an OUTPUT clause that I can't see.
Here is an example trigger:
CREATE TRIGGER [dbo].[TRG_person__name]
ON [dbo].[person]
AFTER INSERT,UPDATE
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF UPDATE ( person_name_first )
BEGIN
UPDATE person
SET person_name_first = dbo.mslTitleCase ( i.person_name_first )
FROM person AS x
JOIN inserted AS i ON x._pk_person = i._pk_person
WHERE x._pk_person = i._pk_person
END
--END IF
END
The mslTitleCase function has been declared and works, if I was to create a record using a SQL Query.
But I can an error when creating a new record in FileMaker.
Does anyone have any tips to find a way to stop this error in FileMaker?
Any guidance greatly appreciated.
Thank you
Lewis
This has been reported as a bug: http://help.filemaker.com/app/answers/detail/a_id/7870/~/external-sql-data-sources-(ess)%3A-unable-to-write-to-a-table-with-triggers-on
It does not look like it was resolved.
You may have other problems, but the correct syntax for the update is:
UPDATE p
SET person_name_first = dbo.mslTitleCase ( i.person_name_first )
FROM person p JOIN
inserted i
ON p._pk_person = i._pk_person
WHERE p._pk_person = i._pk_person;
The important change is that the update references the table alias not the table name. I think p is a much better alias than x for a table named person.
It sounds like your application is using the OUTPUT clause to directly return the modified data. If the table has any trigger, this is not allowed. They would have to output the modified data into a table, and then select the data from the table.
The issue is covered in this article:
https://blogs.msdn.microsoft.com/sqlprogrammability/2008/07/11/update-with-output-clause-triggers-and-sqlmoreresults/
If this is the case, you will not be able to add a trigger to the table. The corrections to the data will need to happen from an external source.
I have a question on SQL execution sequence. I'm using nested iif() conditional statements in MS-Access and because character length became too long, I wanted to use an alias in the statement.
I tried this (by accident) and it works and I'm not sure why and if I should actually use it. Below are shortened examples of the original format compared to the second with enough of the statement to get the gist of it (using generic table names).
I want to update the upDateMe table.
Original pre-alias :
UPDATE upDateMe
INNER JOIN linkMe
ON (linkMe.UniqueID = upDateMe.UniqueID)
AND (linkMe.SrcNumber = upDateMe.SrcNumber)
SET upDateMe.ExpiryDate = [linkMe].[ExpiryDate]
, upDateMe.PermitEnd = [linkMe].[PermitEnd]...
Here I've reversed the tables and put in the alias 'bData':
UPDATE linkMe
INNER JOIN upDateMe AS bData
ON (linkMe.UniqueID = bData.UniqueID)
AND (linkMe.SrcNumber = bData.SrcNumber)
SET bData.ExpiryDate = [linkMe].[ExpiryDate]
, bData.PermitEnd = [linkMe].[PermitEnd]...
This second query works!??. I'm not really sure as to why it would. Can someone explain it??
Because the left side of the set statement is ALWAYS what gets updated.
In code (most maybe not all) you set a variable by setting it = to some value on the right side. The LEFT side is always the target.
In this case, you're telling the database to update the join of linkMe and upDateMe (as bData) setting the bData values to the linkme values. You would likely get an error if you tried to update both bdate and linkMe at the same time as engines generally are only able to update 1 table at a time, since no such conflict seems to exist here, bData is updated without issue.
I need to create a query for updating a column in a table with values taken from another table and matching a field.
These are the 2 tables:
tblMain
ID Autonumbering
Key Text
Stat1 Integer
tblStat1
ID Autonumbering
Key Text
Freq Integer
I want to UPDATE the tblMain.Stat1 column with tblStat1.Freq value on each record in which tblMain.Key = tblStat1.Key.
I tried this syntax (found somewhere as an example)
UPDATE tblMain
SET tblMain.Stat1 = tblStat1.Freq
WHERE tblMain.Key = tblStat1.Key;
This doesn't work and returns an error on the 2nd row.
After some trials I found that the correct syntax (built with the Access query generator) is this:
UPDATE (tblMaibn INNER JOIN tblStat1 ON tblMain.Key = tblStat1.Key)
SET tblMain.Stat1 = tblStat1.Freq;
In this 2nd syntax, there is no trace of the WHERE condition.
Can someone help me to understand what's wrong with the 1st syntax.
Since I'm building a new table (the join), how can it work on tblMain?
As I said, I found the wrong syntax as an example of UPDATE statement.
Thank you in advance.
Bye,
Ivano
What is happening in your first query on the 2nd row, is that Access isn't aware of what tblStat1 represents in your query.
The reason your 2nd query is working is because it uses an inner join on the relevant key. In order for SQL to be aware of what record in tblMain relates to which record in tblStat1, you need to use a join.
You can see in the generated code that it is updating your desired table, but joining onto the second table. The where condition is redundant as you're updating every record.
In 1st syntax, you can change:
UPDATE tblMain
SET tblMain.Stat1 = (SELECT Freq
FROM tblStat1
WHERE tblMain.Key = tblStat1.Key)
I have a column where I have values like:
Email_Password+oi8hu907b;New_eMail+Y;Email_Username+iugbhijhb8
Now I want to update New_eMail attribute for all rows which has Y to N without affecting anything else.
Please advise.
i hate it but...
update table
set column = replace(column,'New_eMail+Y','New_eMail+N')
where column like '%New_eMail+Y%'
you don't need the WHERE clause but if you put a functional index on the table it may be quicker with it
Since it may be the only place in the string where '+Y;' occurs the following statement may do the trick:
update <your_table>
set <your_column> = replace(<your_column>,'+Y;','+N;')
where instr(<your_column>,'+Y;')>0
This solution differs from the others provided because it does not depend on the value of the email address.
My answer is a slight improvement over the answer from user davegreen100
Since they don't allow me to post it as a comment, I add it here.
update <<tablename>>
set <<columnname>> = replace(<<columnname>>,';New_eMail+Y;',';New_eMail+N;')
where <<columnname>> like '%;New_eMail+Y;%'
From SAS, I am updating a table in MS Access with values from another table. Some of the fields in the table being updated contain spaces. This seems to be causing problems in the update statement. This gives me the error "Too few parameters. Expected 1.":
update [Original Table] as a inner join Updates as b on a.ID = b.ID
set a.[Variable 1] = b.[Variable 1]
where Year = "2000";
For field names without spaces, the statement works without error. And since I am using the field names elsewhere without table references/aliases, I figure the combination of [] and aliases is causing the problem. Any suggestions to address this?
Year() is a function which returns a variant subtype integer which corresponds to the calendar year of the date value you give to the function.
In your case, it seems you have a field named Year. So perhaps the "missing parameter" is the expected date argument to the Year() function.
You can avoid confusing the db engine by enclosing Year in square brackets. The brackets signal the engine that Year is an object (field) name instead of the function.
update [Original Table] as a inner join Updates as b on a.ID = b.ID
set a.[Variable 1] = b.[Variable 1]
where [Year] = "2000";
Whenever possible, it's better to use names which don't conflict with reserved words. That may not be practical in your situation ... but if you can do it you will reduce the number of Access development headaches you will suffer. :-)
For further information about "naming challenges", see Problem names and reserved words in Access.
Sorry I overlooked the point that the query can work in spite of that WHERE clause issue.
I can't see anything about the remainder of your SQL which should trigger a complaint from the db engine. I assume you tested that statement directly in Access, and got no errors.
If there is something peculiar to the interaction between SAS and Access which causes this, perhaps you could use a saved Access query as a work-around. Take that SQL and save it as a named query, qrySasTest, in your Access db. Then try executing qrySasTest from the SAS side.
This query worked as is for me (modifying only table names), both run from access and run from SAS. This is with SAS 9.3 64 bit and Office 2010 64 bit, so I suppose there could be something different going on with your version(s) of both, but it worked as expected.
proc sql;
connect to access (path="c:\temp\test.accdb");
execute
(
update [Test2] as a inner join Test as b on a.ID = b.ID
set a.[Variable 1] = b.[Variable 1]
where Year ="2000";
)
by access;
disconnect from access;
quit;
If you want to use the libname reference instead of SAS access, you can use the "dquote=ansi" option after your proc sql statement as shown below. In this example I created a library reference called mydbms:
libname mydbms odbc dsn=prompt preserve_names=yes;
proc sql dquote=ansi;
update mydbms."Original Table" as a inner join mydbms.Updates as b on a.ID = b.ID
set a."Variable 1" = b."Variable 1"
where Year = "2000";
quit;