Queries in MS-Access:formatting a field in the middle of an sql UPDATE code - sql

I am having yet another problem with my data in ms-access.
Basically, what i'm doing is using multiple sql statements to pull, sort, and staight up change/manipulate data. The problem that im having currently is that i am trying to use a sql code like this:
UPDATE analyzedCopy2 SET analyzedCopy2.DateRange = #4/21/2009# to #4/29/2009#
but i can't figure out how to reformat DateRange (which is currently a date) to Text in the sql statement. I can't simply set the format manually because the sql code before this one is pulling in all of the data from another table and when that happens it changes the format of DateRange to date automattically since that is the type it is where it is coming from.
If anyone knows how to format a column with vba code then that would be fine too since i'm creating a vba code to run the queries in the order i want.
If anyone needs a better explanation of anything just let me know.

You can use SQL (DDL) to change some aspects of a table:
ALTER TABLE TableName ALTER COLUMN ColumnName Text(50)

Related

Best way to duplicate data in the same table and updating a column at the same time

I'm trying to find the best way to duplicate all the rows in a table.By this i mean inserting them again in the same table, but i need to update a single column only on the values that were inserted.
This is to help me write a script so I can automate some work on some clients.
I can't use select * as it will throw an error because of the identity columns but at the same time i don't want to be manually writting all the column names for several tables.
Is there a simple way to translate this into SQL server?
Sorry for not showing a piece of code, but i have nothing at the moment and i'm not really fluent in SQL.
EDIT: I have ended up following the advice of JamieD77 in the comments below this post by moving everything to a table, drop the id column, updating what i need and then moving back as it seems to be the most effiecient.
In SQL Server Management Studio you can drag the "Columns" folder under a table and drop it on the query window and it will paste a comma-delimited list of all the columns.
Or run a query like:
select string_agg(quotename(name),', ')
from sys.columns
where object_id = object_id('MyTable')

Rename columns in MS Access using SQL

In MS Access, I have a few tables with some column names having spaces in their column names (e.g. Transaction I).
I need to replace the spaces with underscores (e.g. Transaction_ID) using SQL.
Although I'm somewhat familiar with SQL, however, my exposure was in an Oracle environment and all of those queries and functions don't work in MS Access. I am fairly new to MS Access.
I have tried following codes from the posts that I searched, however, to no avail.
alter table EP sp_rename 'Transaction ID' to Transaction_ID
alter table EP rename column 'Transaction ID' to Transaction_ID
Access does not have a built in command "line" option to re-name a column.
(and your example looks to be for SQL Server, and MORE so it looks to be using a library stored procedure function to do this for you. So, that looks to be SQL Server syntax - not MS Access.
If you going to use DDL in Access to re-name a column? You have to create a new column with the desired name, and then move the data to this new column, and then drop the old column.
The so called sql 'ddl' commands in Access actually do quite much follow the SQL standard.
So, before embarking on this road, I would consider to use the built in GUI + table designer. (It will do the dirty work behind the scenes for you).
So, can you use DDL to rename a column? Yes, but you have to do this in 3 steps.
Create the new column.
Copy data from old column to new column
Delete the old column.
Because of indexing, possible relationships and other issues, then one REALLY does want to use the built in table designer + the GUI here.
But, you can use say a procedure (VBA) in Access to do this:
Say we had a REALLY bad column name for City called [The City].
To re-name to City, then we can go:
Sub MyAlter()
' create the new column
CurrentDb.Execute "ALTER TABLE tblHotels ADD COLUMN City TEXT(50)", dbFailOnError
' copy the data
CurrentDb.Execute "UPDATE tblHotels SET City = [The City]", dbFailOnError
' drop the origonal column
CurrentDb.Execute "ALTER TABLE tblHotels DROP COLUMN [The City]", dbFailOnError
End Sub
Just keep in mind that ANY column in Access with spaces (yuk!!) needs to be surrounded with []. This applies to SQL select queries, update queries, insert queries, and of course DDL commands to modify the table structure.
If a one-time deal, then of course simply use access, open the table(s) in question in design mode, and make the changes.
However, if you do for some reason do need to use a procedure, then the above code in a access code module can be used.
Or you could just fire up the query builder, flip to SQL view mode, and type in the above raw sql that way.
(you have to type in each sql command separately - the Access query builder only allows ONE SQL statement at a time.
So you could type in first above SQL, hit "!" to execute, and then do the two additional SQL statements.
MAKE A BACK-UP!!!
And of course any code, any queries, any reports etc. that used the old column name will break. So changing column names in an existing application is a HIGH RISK adventure (you can and will break tons of code, existing forms, and existing reports, and existing SQL queries you have now).
However, if this is a one-time update? Then I would of course just use the table designer. It allows a re-name without a column drop – and it keeps other field settings such as indexes, format etc.
I which above suggested approach makes the most sense will depend on your particular "use" case.
Last but not least? You can use VBA code and the table objects in that code. This approach is probably the best. It does not use SQL DDL, and you have greater control over a lot of features for a given column (required, allow nulls – the list goes on). Most of these settings CAN be set in DDL - but you spend quite a bit of time searching and looking up those settings.
All in all? I would use the table designer if possible here.

Using SSMA to convert from Access to SQL, scripting the fixes

I am using SSMA to convert from an Access db to a SQL 2019 DB.
There are some things I need to fix in the access DB so I am trying to figure out whether or not these things can be done via a query in access or you have to use the goofy UI and do everything manually.
So I had a couple of questions about queries in Microsoft Access:
Can you modify the 'required' attribute on a column within a table by using a query?
Can you configure Index (dupes) on a column by using a query?
Can you change validation rules using a query?
Can you create/delete relationships using a query?
Can you change the field length of a column by using a query?
Any examples of any of these would be helpful, when I google for ms access related things all of the content is either related to Access 2007/2010 or its very UI heavy rather than Query heavy.
I am trying to script this because I may have to do this migration several times.
Update: I was able to get most of what i needed figured out..
ALTER TABLE Users ALTER COLUMN Type CHECK(In ("I","U","") Or Is Null);
Still havent found a way to change the 'ValidationRule'.. trying to change it to
In ("I","U","") Or Is Null
Look into the Data Definition Language section of the MS Access SQL Reference, specifically the ALTER TABLE statement, which will cover the majority of your questions.
For example, in response to:
Can you change the field length of a column by using a query?
ALTER TABLE Table1 ALTER COLUMN Field1 TEXT(100)
The above will change the data type of the field Field1 within table Table1 to a text field accommodating 100 characters.

MS Access SQL (Quickbooks) Update Query

I'm trying to create an Update Query in MS Access (2013) to a QuickBooks Database using QODBC.
I need to update the table PriceLevelPerItem. I am trying to update the field in said table called PriceLevelPerItemCustomprice with a value from another table, QueryThreeTable, and a column titled UpdatedPrice.
I need to update the table PriceLevelPerItem where the PriceLevelPerItemItemRefListID matches the value of ItemID from QueryThreeTable and ListID matches the QueryThreeTable.ItemListID (yes I know these are the wrong way around...)
So far this process has been a very annoying trial of many queries and any help would be greatly appreciated
This is what I've been working with:
UPDATE
PriceLevelPerItem
SET
(PriceLevelPerItemCustomPrice = QueryThreeTable.UpdatedPrice)
FROM
QueryThreeTable, PriceLevelPerItem
WHERE
QueryThreeTable.ItemID = PriceLevelPerItem.PriceLevelPerItemItemRefListID
AND
QueryThreeTable.ItemListID = PriceLevelPerItem.ListID;
I think the problem is that you're trying to use a DAO query inside a QODBC query. I think the two use different Data Access engines.
You're going to need to lookup your UpdatedPrice in your QueryThreeTable using DLookup. Or maybe you need to create a DAO loop using QueryThreeTable that then updates values in your QODBC table from there.
Make your QODBC query work without the use of QueryThreeTable and without any joins. Then come up with a way to dynamically create your query. You're resulting SQL should look something like this:
UPDATE
PriceLevelPerItem
SET
PriceLevelPerItemCustomPrice = 150.16
WHERE
PriceLevelPerItem.ListID = '310000-1146238368';

SSIS - fill unmapped columns in table in OLE DB Destination

As you can see in the image below, I have a table in SQL Server that I am filling via a flat file source. There are two columns in the destination table that I want to update based on the logic listed below:
SessionID - all rows from the first CSV import will have a value of 1; the second import will have a value of 2, and so on.
TimeCreated - datetime value of when the CSV imports happened.
I don't need help with how to write the TSQL code to get this done. Instead, I would like someone to suggest a method to implement this as a Data Flow task within SSIS.
Thank you in advance for your thoughts.
Edit 11/29/2012
Since all answers so far suggested taking care of this on the SQL Server side, I wanted to show you what I had initially tried doing (see image below), but it did not work. The trigger did not fire in SQL Server after SSIS inserted the data into the destination table.
If any of you can explain why the trigger did not fire, that would be great.
If you are able to modify the destination table, you could make the default values for SessionID and TimeCreated do all the work for you. SessionID would be an auto-incremental integer while the default value for TimeCreated would be getdate() or gettime() depending on the data type.
Now, if you truly need it the values to be created as part of your workflow, you can use variables for each.
SessionID would be a package variable which is set by an Execute SQL Task. Just reference the variable in your result set and have your SQL determine the next number to use. There are potential concurrency issues with this, though.
TimeCreated is easily done by creating a Derived Column in your data flow based on the system variable StartTime.
You can use a Derived Column to fill the TimeCreated column, if you want the time of the data flow to happen, you just use the date and time function to get the current datetime. If you want a common timestamp for the whole package (all files) you can use the system variable #[System::StartTime] (or whatitwascalled).
For the CSV looping (i guess), you use a foreach loop container, and map an iterative value to a user variable that you map in the derived column for SessionID as mentioned above.
First, I'd better do it on SQL Server side :)
But if you don't want or cannot to do it on server side you can use this approach:
It is obvious that you need to store SessionID somewhere you can create a txt file for that or better some settings table in SQL Server or there can be other approaches.
To add columns SessionID and TimeCreated to OLE Destination you can use Derived columns