Is it possible to run a query to rename the fieldnames of each field? - sql

I have like 20 datasets, each with 28 fields without the first row having fieldnames. So MS Access assigned 'Field1', 'Field2'.... 'Field28' as fieldnames for each column. I'd like to know if it is possible to run a query to rename them? I couldn't find any information online and I tried manually entering the names but it is taking too long.

Access SQL does not support changing the name of an existing field.
You could create a SELECT query and alias the field name:
SELECT Field1 AS FieldA FROM YourTable;
Then you would use the query instead of the table whenever you want to display the aliased name.
Or you could use a "make table" query to create a new table with the new field name:
SELECT Field1 AS FieldA INTO NewTable FROM YourTable;
Or you could execute an ALTER TABLE statement to add the new field, then an UPDATE to populate the new field with the old field data, and finally DROP the old field. But that seems like too much work.
Instead of SQL, consider using VBA to rename the field:
CurrentDb.TableDefs("YourTable").Fields("Field1").Name = "FieldA"

Related

Google BigQuery: Add new column to a table with constant value

I would like to know a way of adding one additional column to a BigQuery table, that will populate all the rows for this newly created column with specific constant value.
I know how to create column with NULL values:
ALTER TABLE project_id.dataset.table
ADD COLUMN A STRING
But my goal is to also add the ingestion time with CURRENT_TIMESTAMP() function. Is it even possible with one command? Or maybe I need to apply subsequently some second command?
Seems like a solution is to use another query after the mentioned one:
UPDATE project_id.dataset.table SET A = CAST(CURRENT_TIMESTAMP() AS STRING) WHERE 1=1

T-SQL: Exclude Columns from a SELECT statement based on string

My overall goal is to create new tables by selecting columns in existing tables with certain patterns/tags in their column names. This is in SQL Server.
For example, I have a common need to get all contact information out of a company table, and into its own contact table.
I haven't been able to find a programmatic approach in SQL to express excluding columns from a SELECT statement based on string.
When looking to options like the COL_NAME function, those require an ID arg, which kills that option for me.
Wishing there was something built in that could work like the following:
SELECT *
FROM TABLE
WHERE COL_NAME() LIKE 'FLAG%'
Any ideas? Open to anything! Thanks!!
One trick could be to firstly using the following code to get the column names you desire:
select * from information_schema.columns
where table_name='tbl' and column_name like 'FLAG%'
Then concatenate them into a comma-delimited string and finally create a dynamic sql query using the created string as the column names.

Update A multi-valued field in Access

I have created a lookup table in Access to provide the possible values for a column. Now I need to update this column with the data it had before I converted the column. I am unable to figure out a SQL Query that will work. I keep getting the error "An UPDATE or DELETE query cannot contain a multi-valued field." My research has suggested that I just need to set the value of the column but this always updates 0 records:
UPDATE [table_name] SET [column_name].Value = 55 WHERE [table_name].ID = 16;
I know this query will work if I change it to update a text column, so it is definitely a problem with just this column.
If you're adding a value to your multi-valued field, use an append query.
INSERT INTO table_name( [column_name].Value )
VALUES (55)
WHERE ID = 16;
If you want to change one particular value which exists in your multi-valued field, use an UPDATE statement. For example, to change the 55 to 56 ...
UPDATE [table_name]
SET [column_name].Value = 56
WHERE [column_name].Value = 55 And ID = 16;
See Using multivalued fields in queries for more information.
I have figured this out! It certainly was counter-intuitive! You have to use an INSERT statement to do the update.
-- Update a record with a multi-valued field that has no value
INSERT INTO [table_name] ( [[column_name].[Value] )
VALUES(55)
WHERE [table_name].ID = 16;
This confused me because I was expecting an UPDATE statement. I think it actually inserts a record into a hidden table that is used to associate multiple values with this column.
I am working with Sharepoint, I created the tables as multi-value fields, ran into the error with my INSERT INTO statement, went back to Sharepoint to change to non-multi-value fields, but that didn't fix it.
Recreated the table without using multi-value fields, and the INSERT INTO worked just fine.
do not use the .value part
UPDATE [table_name] SET [column_name] = 55 WHERE [table_name].ID = 16;
INSERT INTO Quals (cTypes.[value])
SELECT Quals_ContractTypes.ContractType
FROM Quals_ContractTypes
WHERE (Quals.ID = Quals_ContractTypes.ID_Quals);
I gotta say I didn't understand very well your problem but I saw something strange in your query. Try this:
UPDATE [table_name] SET [column_name]= 55 WHERE [table_name].ID = 16;
UPDATE:
Look at this link: it has an example
UPDATE Issues
SET Issues.AssignedTo.Value = 10
WHERE (((Issues.AssignedTo.Value)=6)
AND ((Issues.ID)=8));
NOTES
You should always include a WHERE
clause that identifies only the
records that you want to update.
Otherwise, you will update records
that you did not intend to change. An
Update query that does not contain a
WHERE clause changes every row in the
table. You can specify one value to
change.
The Multi-Valued field refers to Access databases that have tables with columns, that allow you to select multiple values, like a Combo Checkbox list.
THOSE are the only Access types that SQL cannot work with. I've tested all Access lookup possibilities, including hard-coded values, and lookup tables. They work fine, but if you have a column that has the Allow Multiple select options, you're out of luck. Even using the INSERT INTO as mentioned below, will not work as you'll get a similar but different error, about INSERTing into multi-valued fields.
As mentioned it's best to avoid using such tables outside of Access, and refer to a table specifically for your external needs. Then write a macro/vba script to update the real tables with the data from the "auxiliary" table.

Storing a pre-processed varchar column in the database along with the original one

I have a big table with names and surnames. People search this database via a Web interface. PHP code queries the table with LOWER(#name) = LOWER(name). In order to make the search faster, I want to make a derived column in the table named lower_name and always LOWER() the names before storing. But when it comes to send the results to web interface, I want to show the original ones. However, I don't want to change the table structure. Is there some kind of Index that automatically does the trick or an option to create an "invisible column" in SQL Server or something like that?
You can create a presisted computed column with an index on it:
ALTER TABLE YourTable ADD lower_name AS LOWER(name) PERSISTED
go
CREATE NONCLUSTERED INDEX IX_YourTable_lower_name
ON YourTable (lower_name)
go
You do not INSERT or UPDATE this column, the DB will do it for you and always keep it in sync with your "name" column.
If you don't want to use a computed column you could create a view, that has the LOWER(name) AS lower_name column in it and put an index on that column:
http://msdn.microsoft.com/en-us/library/cc917715.aspx
You have various options:
declare the column Name case-insensitive using the COLLATE clause
compare Name and #Name using the COLLATE clause
create a computed column LowerName AS LOWER(Name) PERSISTED and index this computed column
PHP code queries the table with
LOWER(#name) = LOWER(name)
Thanks in a world of pain. LOWER(name) triggers a table scan. This is not making it faster, it is - brutally - a beginner mistake that kills performance. You should never compare against a calculation of the table fields, unless you ahve the result in a precaculated index (as you can do in SQL Server 2008, for example).
What about
WHERE name LIKE #name
which should ignore case... plus an Index on the name field.

how to convert result of an select sql query into a new table in ms access

how to convert result of an select sql query into a new table in msaccess ?
You can use sub queries
SELECT a,b,c INTO NewTable
FROM (SELECT a,b,c
FROM TheTable
WHERE a Is Null)
Like so:
SELECT *
INTO NewTable
FROM OldTable
First, create a table with the required keys, constraints, domain checking, references, etc. Then use an INSERT INTO..SELECT construct to populate it.
Do not be tempted by SELECT..INTO..FROM constructs. The resulting table will have no keys, therefore will not actually be a table at all. Better to start with a proper table then add the data e.g. it will be easier to trap bad data.
For an example of how things can go wrong with an SELECT..INTO clause: it can result in a column that includes the NULL value and while after the event you can change the column to NOT NULL the engine will not replace the NULLs, therefore you will end up with a NOT NULL column containing NULLs!
Also consider creating a 'viewed' table e.g. using CREATE VIEW SQL DDL rather than a base table.
If you want to do it through the user interface, you can also:
A) Create and test the select query. Save it.
B) Create a make table query. When asked what tables to show, select the query tab and your saved query.
C) Tell it the name of the table you want to create.
D) Go make coffee (depending on taste and size of table)
Select *
Into newtable
From somequery