Please help me make this Access 2007 Friendly - sql

I asked a friend to hellp me with an issue I was having with my Access Database since I haven't programmed in years, and here's what he replied with:
Let me toss an example out just to make sure I'm looking at this
right. You start with a record with ID 1. This gets renewed, and the
system generates a new record with ID 2, and brings along the old ID
of 1 in the RenewalOf field, and so on for future renewals. If that
is correct, and each record is only allowed to be referenced once (so
there will only ever be one record with ID of 1 in the RenewalOf
field), then the following should work:
This bit of code didn't work:
UPDATE
tblSold
SET
RenewedToID = RenewalRecord.SoldID
FROM
tblSold
INNER JOIN tblSold RenewalRecord ON
tblSold.SoldID = RenewalRecord.RenewalOf
Not sure what is allowed in your SQL queries, but this is basic and
should be fine. You can also add in some criteria to only update
records where the RenewedToID field is blank, or only for one record
if you are processing this just after you add a new record. You can
check to make sure this is going to work by running the following:
But this did:
SELECT
tblSold.SoldID
,RenewalRecord.SoldID
FROM
tblSold
INNER JOIN tblSold RenewalRecord ON
tblSold.SoldID = RenewalRecord.RenewalOf
This will list the original ID along with the renewal ID, i.e. the one
that will be put in the original record. Let me know if this works or
if you have any issues with it.
Can you help me make his first code snippet work in Access 2007?

You may need to rearrange the update slightly for Access:
UPDATE
tblSold
INNER JOIN tblSold RenewalRecord ON
tblSold.SoldID = RenewalRecord.RenewalOf
SET
tblSold.RenewedToID = RenewalRecord.SoldID
Some other SO answers showing this type of syntax:
SQL Update Statement in MS Access
How to create a correlated update subquery in MS-Access?

Related

Error running MS Access delete query with left join

I am using Microsoft Access, I believe from the Office 16 suite.
I am trying to delete records in a Contacts table that are:
listed as "Out of Business" and
no longer have any logged donations in a Donations table.
(We only keep Donations records for five years to maintain a manageable database size. So once an out-of-business company's Donations have worked their way out of that five-year mark, we can go ahead and delete them.)
I've successfully built a query that returns the results I'm looking for - I can see the results in Datasheet view both when doing a SELECT and when switching to DELETE.
However, when I go to run the Delete Query, I get an error message:
Specify the table containing the records you want to delete.
Here is the SQL code:
DELETE Contacts.*
FROM Contacts
LEFT JOIN Donations ON Contacts.[ContactID] = Donations.[DonorID]
WHERE Contacts.ProcurePreference="Out of Business" AND Donations.DonorID Is Null
I am new to SQL code, so am guessing there's something in my syntax that's wonky (although, again, Datasheet view shows exactly what I would expect). Or maybe there's something in the way I've set up my tables and records and relationships that is preventing the final step.
I've searched and read other similar problems on this forum but could not successfully incorporate any of those answers to address my own problem.
Thanks in advance!
You can do what you want with NOT EXISTS:
DELETE Contacts.*
FROM Contacts
WHERE Contacts.ProcurePreference = 'Out of Business'
AND NOT EXISTS (SELECT 1 FROM Donations WHERE Contacts.[ContactID] = Donations.[DonorID])
try using this SQL:
DELETE FROM Contacts
WHERE ProcurePreference = "Out Of Business"
AND ContactId NOT IN (SELECT DonorID FROM Donations)

What is a bandwidth efficient way to query Microsoft SQL Server to find a unique row where ID exists in Foreign Table

Sorry if I have not worded the question well. I thought about it for a few minutes and this is the best I could come up with.
Long story short, I am adding some Audit Logging into my software (.NET) and am looking for the most efficient way to find out if any modifications were made.
My structure is I have a data object, lets call it a UserLogon, which contains the timestamp, username and access level of somebody logging in to the program.
Now, I have a table of Changesets where one record is created whenever somebody changes the details of the UserLogon. Eg - I logged on as the wrong User, so a data validation person has gone into the event editing system and changed the history for me.
So, there might be 5 different changesets tagged onto the User Logon.
All I want to ask of SQL is for it to tell me True or False that there is at least 1 Changeset linked to the UserLogon.
Currently I have a Join
SELECT Event.Id, Changeset.Id AS ChangesetId
FROM Event
LEFT JOIN Changeset
ON Event.Id = Changeset.EventId
WHERE Changeset.EventId = Event.Id
Which produces the following
Id ChangesetId
F12E54FE-72DF-4A3B-B61B-A4DD00F02597 FA2E0EEB-E5FA-41D1-8C61-A4DD00F025A0
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 FC2496DC-9DF7-4C47-959A-A4DE00BB153C
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 F0CB41F3-D8E3-40F2-B3CE-A4DF00918478
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 4E974BB8-CB41-49E4-A2E7-A4DF00951AE4
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 2887ACBB-4032-4BDD-B8EF-A4E400BD5385
7D1372A2-AE4A-4BB9-9800-A4DE00BB1527 8BC5CC13-F557-42FA-9A50-A4E400D370AC
So we can see here there are 2 UserLogons, but there are 5 ChangeSets for the second one.
I would ultimately like to end up with just a True/False on the second column but not too sure where to go from here, or if thats even possible.
My intention is to use this to display an Icon on a ListView in WPF for modified rows to show the user if the row is original or altered.
You can use exists:
select e.*,
(case when exists (select 1 from ChangeSet cs where cs.EventId = e.EventId)
then 1 else 0
end) as HasChangesetFlag
from event e;
For performance, you want an index on ChangeSet(EventId).
Note: This is ANSI SQL and should work in almost any database.

SQL UPDate same table

I know this has been posted before but I am not sure I have got my head around the logic let aloan trying to get it into to JET Friendly Syntax.
Here is what I am trying to do
I have a bunch of records that relate to documents and I am planning on renaming the documents with GUID's however some records point to the same document here lays the problem.
Table
ID, LegacyFullPathNme, GUID, isDuplicate
my code loops through and assigns each record a GUID. then I want to update the Duplicate Documents records with the same GUID
below is my hash at it but doesn't work "Operation must use an updateable Query
UPDATE [IO Documents] a
set a.codedFileName = (SELECT B.codedFileName
FROM [IO Documents] b
WHERE b.LegacyFullPathName = a.LegacyFullPathName)
Currently use a macro to go throw RBAR
I'm a little confused on why you would do it this way since now your globally unique id column isn't unique in that multiple rows will have it.
I think a better method would be to simply create a new table from your old one with a row for each file path.
SELECT LegacyFullPathNme
INTO newtable
FROM oldtable
GROUP BY LegacyFullPathNme;
and then add the guid into the new table afterwards. (note that I didn't test that sql snippet so that might not be proper syntax but I think it gets the point across).
I believe you are looking for something like this:
UPDATE [IO Documents] SET
codedFileName = DMin("codedFileName","IO Documents","LegacyFullPathName='" & LegacyFullPathName & "'")

How to remove row that exists in another table?

I have two tables. Main table is "CompleteEmailListJuly11" and the second table is "CurrentCustomersEmailJuly11". I want to delete rows in CompleteEmailListJuly11 table that CurrentCustomersEmailJuly11 has based off email.
I've tried this following Delete example, but it doesn't do anything close to what I'm trying to do. This only shows me the ones that EXIST in the database, it doesn't show me the the list of emails that AREN'T matching.
DELETE * FROM CompleteEmailListJuly11 AS i
WHERE EXISTS (
SELECT 1 FROM CurrentCustomersEmailJuly11
WHERE CurrentCustomersEmailJuly11.email = i.EmailAddress
)
Help is greatly appreciated.
This is the query I think you need:
DELETE FROM CompleteEmailListJuly11
WHERE EmailAddress IN (SELECT email FROM CurrentCustomersEmailJuly11)
Ps: The DELETE query does not delete individual fields, only entire rows, so the * is not necessary, you will also need to "Execute" this query rather than "Previewing" or "Exporting"
If you're building your DELETE query in Access' query designer, notice there are two different modes of operation which seem similar to "go ahead and do this".
Datasheet View (represented by the grid icon labeled "View" on the "Design" section of the ribbon). That view enables you to preview the affected records, but does not actually delete them.
The "Run" icon (represented by a red exclamation point). "Run" will actually execute the query and delete the affected records.
If you already know this, my description may seem insulting. Sorry. However, it seems that folks new to Access can easily overlook the distinction between them.
You can use something like this adapted to delete
SELECT ... // complete
EXCEPT
SELECT ... // current
I am not sure exactly how it maps to delete but take a look at that.
I fond it in a similar question: How do I 'subtract' sql tables?
We can use Correlated Query to resolve the issue like
DELETE FROM COMPLETE C
WHERE EMAIL = (SELECT EMAIL FROM CURR CU WHERE CU.EMAIL=C.EMAIL);

sql update multiple rows with multi join subselect

This is an updated part 2 of a question I asked earlier. I'm trying to make the following update, but this query does not actually seem to do anything.
UPDATE u
SET graduation_class_id = gc.graduation_class_id
FROM [user] u
JOIN graduation_class gc
ON u.graduation_class_id = gc.graduation_class_id
JOIN graduation_term gt
ON gt.graduation_year_id = gc.graduation_year_id
TABLE SCHEMA
**user
user_id
graduation_class_id
**graduation_class
graduation_class_id
graduation_year_id
**graduation_term
graduation_term_id
graduation_year_id
The goal is to get the matching graduation_class_id value into the user table. I've been told this won't work because the match won't be found unless the user already has the matching graduation_class_id. That is a problem, because I'm trying to actually get the proper one in there!
This idea is fundamentally doomed to fail. You're trying to get the SQL server to link a graduation class to a user by itself, despite the SQL server not having information on which graduation class should be linked to the user. Unless you have some data somewhere (in user, in graduation_class, in some other table) that links a user_id to the graduation term, class, or year (or someone manually telling SQL which data match up), SQL can't magically do that job for you.