sql: update a given field in all tables instead of just one table - sql

As per another question, sql: update a column if another column poses a conflict in namespace, we know that the following could be used to ensure that a set of 10000 unique package names and 100 categories, having 15000 unique combinations (thus table entries in table categories), could be updated to ensure that the two namespaces don't collide with one another (affecting about 10 entries in total):
UPDATE categories
SET fullpkgpath = fullpkgpath || ',pkg'
WHERE fullpkgpath IN (SELECT value
FROM categories)
However, the fullpkgpath field also repeats in other tables within my sqlite3 database, as per the schema.
Is there a way to have the above UPDATE statement applied to all other tables within a given sqlite3 database with the same fullpkgpath field, without having to manually specify any such extra tables?
If the answer to be above is, "no", then how would I manually specify which other tables I want the statement applied to? Consider that only the categories table has the categories that could be directly compared with names of packages (to be fair, the ports table also has a categories field, but it has all the categories of a given port jammed into one field (space separated), as opposed to separate table entries as is the case in the categories table).

Related

Select a single column across an entire database

I have database (couple hundred tables) that all contains a specific column called LastReplicationDate. This column name is always the same in each table, and is always the same value within each table.
Is it possible to write a query that gets the distinct value of this column assigned to each table in my database without having to select each table via a union query?

How can I use record content from one table to update another, without a join option?

I need to update values in a column in a specific table that exists in all our databases, but do not know the name of the column as it is user-generated.
I have two tables: one of them with user-generated columns tab_Case. In this table there is a column attachment that I need to update if the following condition applies: WHERE attachment = '0' (if true then the value needs to be changed to NULL).
In its simplest form the update query would look something like this:
UPDATE tab_Case
SET attachment = 'NULL'
WHERE attachment = '0'
This table is used in all our databases, so I need to write a query general enough to be usable across all of them.
The problem is that as the table uses user-generated columns, I have no way of knowing what the exact name is of concerned column-type, and exactly how many of those columns exist in the table.
I can, however, find out the type of the column by looking it up in another table tbl_itemPart inner joined with tbl_ValueType, like this:
SELECT ip.DbReference, ip.DbTableName, vt.ValueDescription
FROM tbl_itemPart ip
INNER JOIN tbl_ValueTypes vt ON ip.ValueTypeId = vt.ValuetypeId
WHERE vt.ValueDescription = 'file'
AND ip.DbTableName = 'tab_Case'
The columns I need are always of type 'file' and as the tab_Case table is referenced in tbl_ItemPart it is easy to find out 1) if any columns of type 'file' exist in this table, and 2) when true, what their respective names are.
So great, now I know the names of the columns that I need to potentially update. But, this is where I get lost: how do I use that information in my update query?
How do I write a script that first checks the tbl_itemPart for existence of any columns in tab_Case of type ' file', then retrieves the actual values (= names of those columns) from the DbReference column in tbl_itemPart and then finally uses those values in the update query for tab_Case?
Remember that this scripts needs to automatically do this for each of our databases, so I do not want to look up column names manually per database and then adjust my script accordingly for each of the databases.
I am very new to programming, and may be missing something very obvious, but so far I haven't been able to find a solution, or any relevant information to help me on my way.

rearrange two columns into single unique rows

I have a table that has two ID fields:
ID_expired
ID_issued
This table is the only place where this link between IDs is recorded. An ID_issued will show up with a match in these ID_expired column when a new ID_issued occurs (e.g. z234, g123). There are some exact duplicate rows. There are also instances where there is a duplicate ID_issued but no ID_expired listed (e.g. b111).
My objective is to connect all the IDs into a single row so I can refer to the ID history of an individual.
You can create a View and add the table to the view for as many times as there are ID's and join it on itself as many times as well.

Power Pivot relationships

Trying to create relationships (joins) between tables in power pivot.
Got 2 tables I wold like to join together, connected with a common column = CustomerID.
One is a Fact Table the other Dim table (look up).
I have run the "remove duplicates" on both tables without any problem.
But I still get an error saying : "the relationship cannot be created because each column contains duplicate values. Select at least one column that contains only unique values".
The Fact Table contains duplicates (as it should?) and the Dim Table do not, why do I get this error?
Help much appreciated
Created an appended table with both columns "CustomerID". After the columns where appended together I could "remove duplicates" and connect the tables together through the newly created appended table.
Don't know if this causes another problem later however.
You can also check for duplicate id values in a column by using the group by feature.
Remove all columns except ID, add a column that consists only of the number 1.
Group by ID, summing the content of the added column and filter out IDs whose total equals 1. What's left are duplicated IDs.

Building queries in Access 2007?

I'm trying to build a query where I'm able to get names of clients. So I have two tables, the 1st table has a column AppointmentNO, and this field is a number (there are other columns but they're irrelevant). In the 2nd table I have an ID as primary key, FirstName, LastName. ID is what matches the AppointmentNO in the first table.
Basically what I'm trying to do is link the two tables so that when I have an AppointmentNO in one column, I can see the LASTNAME associated with it in the 2nd column (need to include this in my report). I'm trying to link the AppointmentNO to ID and on JOIN PROPERTIES -> include all records from left table (1st table) and only those from right table (2nd table) where the joined fields are equal.
If I try to run the query it gives me a MISMATCH error. What am I doing wrong?
The type mismatch error could be happening:
because the two fields that you're trying to join aren't set as the same data type (e.g. one is Number and the other is Text) - check this in the Properties tabs for the relevant fields in each table;
it could be that Access has a join between the tables involving other fields (it will sometimes do this with AutoIDs) - you can check the relationships (and establish them) in the Tools -> Relationships window (where this is located might depend on your version). You can also use this tool to explicitly build the relationship, by connecting your 'ID' to 'AppointNO' - though you should still ensure that the fields are of the same data type.
ADDITION:
Based on what you're describing, I think this is the situation (correct me if I'm wrong, though):
Three tables - Client, AppointmentNO, Children
In each table, there is a 'MemberID' - this is primary key in Client Table, and is Foreign Key in the other tables.
The Children and AppointmentNO tables are linked to Client table by one-to-many relationships (a client can have >1 children and >1 appointment).
I'd set this up so that the Member ID is the same datatype in each table, and join all tables on that field. Then, when set up a query that gives you MemberID, ClientName, ClientDOB (and anything else you want from the client table), ChildName, and AppointmentID. Once the query is working and giving you the desired output, you can build a report and group the output by Client and Client Description, so you'll get "Client A" followed by list of appointments and children, then "Client B" etc.
Hope that's clear-ish.