SQL UPDate same table - sql

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 & "'")

Related

SSIS Inserting incrementing ID with starting range into multiple tables at a time

Is there are one or some reliable variants to solve easy task?
I've got a number of XML files which will be converting into 6 SQL tables (via SSIS).
Before the end of this process i need to add a new (in fact - common for all tables) column (or field) into each of them.
This column represents ID with assigning range and +1 incrementing step. Like (350000, 1)
Yes, i know how to solve it on SSMS SQL stage. But i need a solution at SSIS's pre-SQL converting lvl.
I'm sure there should be well-known pattern-solutions to deal with it.
I am going to take a stab at this. Just to be clear, I don't have a lot of information in your question to go on.
Most XML files that I have dealt with have a common element (let's call it a customer) with one to many attributes (this can be invoices, addresses, email, contacts, etc).
So your table structure will be somewhat star shaped around the customer.
So your XML will have a core customer information on a 1 to 1 basis that can be loaded into a single main table, and will have array information of invoices and an array of addresses etc. Those arrays would be their own tables referencing the customer as a key.
I think you are asking how to create that key.
Load the customer data first and return the identity column to be used as a foreign key when loading the other tables.
I find it easiest to do so in script component. I'm only going to explain how to get the key back. I personally would handle the whole process in C# (deserializing and all).
Add this to Using Block:
Using System.Data.OleDB;
Add this into your main or row processing depending on where the script task / component is:
string SQL = #"INSERT INTO Customer(CustName,field1, field2,...)
values(?,?,?,...); Select cast(scope_identity() as int);";
OleDBCommanad cmd = new OleDBCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = SQL;
cmd.Parameters.AddWithValue("#p1",[CustName]);
...
cmd.Connection.Open();
int CustomerKey = (int)cmd.ExecuteScalar(); //ExecuteScalar returns the value in first row / first column which in our case is scope_identity
cmd.Connection.Close();
Now you can use CustomerKey for all of the other tables.

Django: How do I update only ONE record in my database?

I'm having a lot of trouble trying to update a single (record) object in my database.
context['eval_list'] = Evaluering.objects.update(eval_relationer_id=self.kwargs.get('pk'))
I use objects.update, but it updates ALL my objects fk. How do I achieve only updating one object? I have also tried this:
context['eval_list'] = Evaluering.objects.update_or_create(eval_relationer_id=self.kwargs.get('pk'))
But this creates a new object and does not update the record that I want to update. I know why it creates a new objects, and it is because the FK I'm trying to update is null. Surely, there must be a way to only update and not create a single record? What am I missing here?
I tried adding a filter, but it feels redundant? I tried this:
context['eval_list'] = Evaluering.objects.filter(eval_relationer_id=self.kwargs.get('pk')).update(eval_relationer_id=self.kwargs.get('pk'))
I did consider trying to create an ID of the FK instantly and not later on, but I couldn't really get that to work, but if I created an ID then the update_or_create would work because an ID would exist already, BUT I cannot believe that I can't update a single object without create?
If creating the ID earlier on is the only work around, I will have to figure out how.
MyModel.objects.filter(pk=some_value).update(field1='some value')
The filter gets your object (returns the Queryset with only that object), then the update changes some other field that is not the PK to whatever you want.
In your case probably something like this:
context['eval_list'] = Evaluering.objects.filter(eval_relationer_id=self.kwargs.get('pk')).update(some_attribute='some value')
After help from #Hanny I've figured out was going wrong.
I was trying to filter by the eval_relationer_id, when I should have been filtering by the evaluation pk and getting that specific PK. Otherwise I would be updating ALL the values which is not what I wanted.
So by filtering by pk:
filter(pk=self.kwargs.get('pk'))
And updating by the attribute / fk that I want to update
update(eval_relationer_id=self.kwargs.get('pk'))
This is the end-result:
context['eval_list'] = Evaluering.objects.filter(pk=self.kwargs.get('pk')).update(eval_relationer_id=self.kwargs.get('pk'))

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);

Please help me make this Access 2007 Friendly

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?

SQLite issue with Table Names using numbers?

I'm developing an app which requires that the user selects a year formatted like this 1992-1993 from a spinner. The tablename is also named 1992-1993 and the idea is that using SQL the values from this table are pulled through with a statement like this select * from 1992-1993. When I run the emulator however, it throws an error.
If I then relabel the Spinner item to NinetyTwo and rename the table to NinetyTwo and run the emulator it runs as expected and the data is pulled through from the table.
Does SQLite have an issue with numbers in table names?
Yes and No. It has an issue with numbers at the beginning of a table name. 1992-1993 is an expression returning -1. Try to rename the table to Year1992.
Here a similar issue on SO.
sorry for late post
There may be a deeper issue here - is the structure you are using (table name per item in spinner) the best one for the job?
You may find that you want a number of tables e.g.
spinner_value (id, value)
form_data(id, spinner_value_id, etc ....)