We are migrating a Access database to Azure, one update queries is not working despite trying several syntax changes and removals of spaces.
Below is the design view of the query in Access:
Query Design
The following is the SQL expression of the update query in Access:
UPDATE SPEND_steve, KeywordRULES
SET SPEND_steve.Category = [KeywordRULES].Category
WHERE (((SPEND_steve.Category) Is Null) AND ((SPEND_steve.ItemDescription) Like "*"
And (SPEND_steve.ItemDescription)=[KeywordRULES].[ItemDescription]
And (SPEND_steve.ItemDescription) Like "*"));
With the above I receive error 102: Incorrect syntax near ','.
Thank you in advance for any help to move this functioning query from Access to SQL server!!!
Try removing ", KeywordRULES" from Line 1, an UPDATE statement is only able to update one table (or view) at a time.
UPDATE SPEND_steve
SET SPEND_steve.Category =
[KeywordRULES].Category WHERE (((SPEND_steve.Category) Is Null) AND
((SPEND_steve.ItemDescription) Like "" And (SPEND_steve.ItemDescription)=
[KeywordRULES].[ItemDescription] And (SPEND_steve.ItemDescription) Like "")) ;
SQL Update syntax is a bit different to Access, in that each update statement should affect a single table. You can however reference other tables through a join or other ways depending on what you are trying to do.
So KeywordRules is implicitly joined in your query. So your intention is to update the SPEND_steve table based on info from the KeywordRules table. You can do this by a join to the keyword rules.
Update SPEND_steve
Set SPEND_steve.Category = [KeywordRULES].Category WHERE
(((SPEND_steve.Category) Is Null) AND ((SPEND_steve.ItemDescription) Like ""
And (SPEND_steve.ItemDescription) Like ""))
JOIN KeywordRules on (SPEND_steve.ItemDescription)=[KeywordRULES].[ItemDescription];
Also you should change like "" to = "" which may yeild a performance increase, although you might want to check for AND IS Not NULL as well if it could be null.
Related
I have a form that is linked to an ODBC (MS SQLServer), that is displaying a result of a VIEW, when I try to delete a record, I'm invoking a function at VBA level
Form_BeforeDelConfirm(Cancel As Integer, Response As Integer){
If (IsNull(Text104.value) = False) Then
Dim deleteLabel As DAO.QueryDef
Set deleteLabel = CurrentDb.CreateQueryDef("")
deleteLabel.Connect = CurrentDb.TableDefs("KontrollWerte").Connect
If (InStr(QuerySave, "KontrollWerteVIEW") <> 0) Then
deleteLabel.sql = "delete from KontrollWerte_Label where Kontrolle_Label_ID = " & Text104.value
End If
Close
End If
}
Error shown:
[Microsoft][SQL Server Native Client 11.0][SQL Server] View or function 'dbo.KontrollwerteVIEW' is not updatable because the modification affects multiple base tables. (#4450)
This error is okay, as the view is a select with multiple tables.
It seems the function is not called at all, and the "default" delete function of the MS Access is being called, there is a way to say to MS Access don't do the default delete and instead execute my sql statement inside of the Form_BeforeDelConfirm function?
Thanks!
I tried to change when the call of function is called, but no luck.
You have to ensure that the view allows updates.
When you link a table, and it is a view?
The during the creating of the table link, then you will see this prompt:
First, we select the "view" when linking that "view/table"
and note I also checked the Save password option.
Then next we get/see this:
DO NOT skip the above prompt. If you don't select the PK column for that view, then it will be read only.
FYI:
The above prompt to save password, and the above prompt to select the PK when linking to that view?
It DOES NOT appear on re-link, ONLY when adding a new link!!!!
So, that's why I stated to delete the link to sql server (not just re-link and not just re-fresh table link, but Adding for first time are the key instructions here).
So, yes, views are and can be up-datable, but you MUST answer the above prompt during the linking process in Access, or the result is a read-only table.
I have the following error when I try to UPDATE a Table via JDBC - SQL Server Configuration in Database Connector:
org.mule.api.MessagingException: Must declare the table variable "#P0". (com.microsoft.sqlserver.jdbc.SQLServerException).
My code fragment is this:
<db:update config-ref="SQLSERVER_C" doc:name="UPDATE TABLE">
<db:parameterized-query><![CDATA[UPDATE #[flowVars.c_table]
SET C_STATUS= #[flowVars.errorNo], PROCESSED=GETDATE(), RETRY=#[flowVars.retry], MESSAGE=#[flowVars.error]
WHERE
ACCOUNT = #[flowVars.accountNo]
AND LTRIM(RTRIM(SRVORDNO)) = #[flowVars.servOrdNo]]]></db:parameterized-query>
</db:update>
OK. I did realize what happened here...
As you can see, I'm using a parametrized query. My bad.
I have no problem with parametrized queries where I use a explicit table name. (Just text, not a flowVar value nor a MEL expression).
But, according to documentation https://docs.mulesoft.com/mule-user-guide/v/3.6/database-connector#query-types It eems that I cannot use a variable table name, or a variable portion of the SQL Statement in general... Just the "parameter values".
In the other hand, I was able to use dynamic query for this purpose, but I had to ensure the correctness of data inserted (quoted strings in this simple case).
The working code shows as follows:
<db:update config-ref="SQLSERVER_C" doc:name="UPDATE TABLE">
<db:dynamic-query><![CDATA[UPDATE #[flowVars.c_table]
SET C_STATUS= #[flowVars.errorNo], PROCESSED=GETDATE(), RETRY=#[flowVars.retry], MESSAGE='#[flowVars.error]'
WHERE ACCOUNT='#[flowVars.accountNo]' AND LTRIM(RTRIM(SRVORDNO))='#[flowVars.serviceOrderNo]']]></db:dynamic-query>
</db:update>
I want to create VIEW definitions on MS Access. I have used following CREATE VIEW Statement:
SELECT
MFP.FollowUpPlan_Id,
MFP.FollowUpPlan_Name AS PlanName,
DFP.Sequence_No AS SequenceNo,
MFS.FollowUpSchedule_Name AS ScheduleName
FROM
MAS_FollowUp_Plan AS MFP,
DET_FollowUp_Plan AS DFP,
MAS_FollowUp_Schedule AS MFS
WHERE
(((MFP.FollowUpPlan_Id)=DFP.FollowUpPlan_Id) AND
((DFP.FollowUpSchedule_Id)=MFS.FollowUpSchedule_Id)) AND
MFP.is_Deleted = FALSE AND
DFP.is_Deleted = false
ORDER BY
MFP.FollowUpPlan_Id, DFP.Sequence_No;
but it throw an error:
Only Simple Select Queries are allowed in view.
Please Help, Thanks in Advance.
The issue here, as Jeroen explained, is a limitation of Access' CREATE VIEW statement. For this case, you can use CREATE PROCEDURE instead. It will create a new member of the db's QueryDefs collection --- so from the Access user interface will appear as a new named query.
The following statement worked for me using ADO from VBScript. From previous Delphi questions on here, my understanding is that Delphi can also use ADO, so I believe this should work for you, too.
CREATE PROCEDURE ViewSubstitute AS
SELECT
MFP.FollowUpPlan_Id,
MFP.FollowUpPlan_Name AS PlanName,
DFP.Sequence_No AS SequenceNo,
MFS.FollowUpSchedule_Name AS ScheduleName
FROM
(MAS_FollowUp_Plan AS MFP
INNER JOIN DET_FollowUp_Plan AS DFP
ON MFP.FollowUpPlan_Id = DFP.FollowUpPlan_Id)
INNER JOIN MAS_FollowUp_Schedule AS MFS
ON DFP.FollowUpSchedule_Id = MFS.FollowUpSchedule_Id
WHERE
MFP.is_Deleted=False AND DFP.is_Deleted=False
ORDER BY
MFP.FollowUpPlan_Id,
DFP.Sequence_No;
You cannot mix ORDER BY with JOIN when creating views in Access. It will get you the error "Only simple SELECT queries are allowed in VIEWS." (note the plural VIEWS)
Having multiple tables in the FROM is a kind of to JOIN.
either remove the ORDER BY,
or have only one table in the FROM and no JOINs.
I remember from the past (when I did more Access stuff than now) seeing this for a large query with a single table select with an ORDER BY as well.
The consensus is that you should not have ORDER BY in views anyway, so that is your best thing to do.
Another reason that you can get the same error message is if you add parameters or sub selects. Access does not like those in views either, but that is not the case in your view.
Declare variable olevarCatalog ,cmd as OleVariant in Delphi, Uses ComObj
olevarCatalog := CreateOleObject('ADOX.Catalog');
olevarCatalog.create(YourConnectionString); //This Will create MDB file.
// Using ADO Query(CREATE TABLE TABLEName....) add the required Tables.
// To Insert View Definition on MDB file.
cmd := CreateOleObject('ADODB.Command');
cmd.CommandType := cmdText;
cmd.CommandText := 'ANY Kind of SELECT Query(JOIN, OrderBy is also allowed)';
olevarCatalog.Views.Append('Name of View',cmd);
cmd := null;
This is a best way to Create MS ACCESS File(.MDB) and VIEWs using Delphi.
Im learning DB2 and I had a problem while testing some options in my db.
I have 2 tables like this:
Country
=========
IdCountry -- PK
Name
State
=========
IdState -- PK
IdCountry -- FK to Country.IdCountry
Name
Code
And I am using queries like:
SELECT IdState, Name
FROM Tables.State
WHERE IdCountry = ?
Where ? is any working IdCountry, and everything worked fine.
Then I used set integrity in my db2 control center using the default info in the options and the process was successful but now my query isn't giving me any results.
I tried using :
SELECT *
FROM Tables.State
Where IdCountry = ?
and it gives me back results.
While making tests to the table I try adding new States and they appear in the query using column names instead of *, but old records still missing.
I have no clue about what's happening, does anyone have an idea?.
Thanks in advance, and sorry for my poor English.
I'm assuming here that you're on Linux/Unix/Windows DB2, since z/OS doesn't have a SET INTEGRITY command, and I couldn't find anything about it with a quick search on the iSeries Info Center.
It's possible that your table is still in "set integrity pending" state (previously known as CHECK PENDING). You could test this theory by checking SYSCAT.TABLES using this query:
SELECT TRIM(TABSCHEMA) || '.' || TRIM(TABNAME) AS tbl,
CASE STATUS
WHEN 'C' THEN 'Integrity Check Pending'
WHEN 'N' THEN 'Normal'
WHEN 'X' THEN 'Inoperative'
END As TblStatus
FROM SYSCAT.TABLES
WHERE TABSCHEMA NOT LIKE 'SYS%'
If your table shows up, you will need to use the SET INTEGRITY command to bring your table into checked status:
SET INTEGRITY FOR Tables.State IMMEDIATE CHECKED
I have two tables, Hosts, and UnusedHosts. Hosts has 17 columns, and UnusedHosts has 14 columns, where the first 12 is the same as in Hosts, and the 13th is a UserName, who moved a host to UnusedHosts, and the 14th is a date, when he did it. In Hosts there is a Column Unused which is False. I want do the following. If i change in Hosts this value to True, then it should automatically removed to UnusedHosts.
How can i do this? Could someone provide some example?
P.S.: My SQL knowledge is very small, i can use only very simple selects, updates, inserts, and delete commands.
Thanks!
There's two main types of query in SQL Server - the AFTER and the INSTEAD OF. They work, much as they sound - the AFTER performs your original query, and then runs your trigger. The INSTEAD OF runs your trigger in place of the original query. You can use either in this case, though in different ways.
AFTER:
create trigger hosts_unused
on Hosts
after UPDATE
as
insert into UnusedHosts
select h.<<your_columns>>...
from Hosts h
where h.unused = 1 --Or however else you may be denoting True
delete from Hosts
where unused = 0 --Or however else you may be denoting False
GO
INSTEAD OF:
create trigger hosts_unused
on Hosts
instead of UPDATE
as
insert into UnusedHosts
select i.<<your_columns>>...
from inserted i
where i.unused = 1 --Or however else you may be denoting True
delete h
from inserted i inner join
Hosts h on i.host_id = h.host_id
where i.unused = 1 --Or however else you may be denoting True
update h
set hosts_column_1 = i.hosts_column_1,
hosts_column_2 = i.hosts_column_2,
etc
from inserted i inner join
Hosts h on i.host_id = h.host_id
where i.unused = 0 --Or however else you may be denoting False
GO
It's always important to think of performance when applying triggers. If you have a lot of updates on the Hosts table, but only a few of them are setting the unused column, then the AFTER trigger is probably going to give you better performance. The AFTER trigger also has the benefit that you can simply put in , INSERT after the after UPDATE bit, and it'll work for inserts too.
Check out Books Online on the subject.