How to Add Long Text Column to Access Table Via Query - sql

I am updating a table in a .mdb format Access database with Access 2013. I want to add a new field, lets say a Description field, to an existing table.
I can add a text column using the following query
ALTER TABLE TestTable ADD Description TEXT(255);
Alternatively I could use
ALTER TABLE TestTable ADD Description varchar(255);
This works fine, and adds a column to TestTable called Description that is limited to 255 characters. If I open the table in Design View, I can see that the type of Description is listed as "Short Text". However, there is an option there to have the field be of type "Long Text" which as far as I can tell doesn't have a character limit. It is easy to manually change the type from the Design View, but I want to know if I can do this via a query.
I tried increasing the character count in my original query like so
ALTER TABLE TestTable ADD Description TEXT(300);
But then I recieve the error "Size of field 'Description' is too long."
What I want to know is can I add the column via a query such that it has a character limit greater than 255? This query is run as part of a macro that is run automatically, so I don't want to have to change it manually. My attempts at searching for a solution via Google have so far come up empty.

Found the answer shortly after posting the question. Using the query
ALTER TABLE TestTable ADD Description LONGTEXT;
creates a new column of type "Long Text". It should be noted that a character count was not necessary for this type.

Related

Cannot set default value of a field in an Access Table

I am trying to set a default value of a field in an access table in Table Design. The test I entered is:
=DLookUp([Tbl::VII::A::03::b_stkIden].[stkTersNam]","[Tbl::VII::A::03::b_stkIden]","[stkIdx]= [stkIdenOld]") where
In the different attempts the text was typed (did not work), copied and pasted (did not work). Keeps saying Default value or Validation rule is invalid.
stkIden is the source table containing the value I need
stkTersNam is the field which contains the value I need
stkIdx is the index field of my target table in which I want to insert the lookup value
stkIdenOld is the index field in the source table …stkIden.
It seems that default value attribute of tables in Access 2019 no longer accepts any references to any Table in the current database. It seems strange the Ms would remove this essential functionality. Is this just a quirk of the Jet Engine.
Please help. I am desperate. Should I consider abandoning Access for some other more user friendly database.
Should not have 'where' at end of expression. Don't put parameter within quote marks - concatenate dynamic parameter. Missing quote mark for the field argument.
=DLookUp("[stkTersNam]", "[Tbl::VII::A::03::b_stkIden]", "[stkIdx]=" & [stkIdenOld])
If stkIdx is a text field, use apostrophe delimiters.
=DLookUp("[stkTersNam]", "[Tbl::VII::A::03::b_stkIden]", "[stkIdx]='" & [stkIdenOld] & "'")
However, this won't work as a DefaultValue (won't in any version of Access) and really makes no sense to pull related info via DefaultValue property anyhow. Options:
Query joining tables.
Multi-column combobox on form.
DLookup in textbox ControlSource.

Multiplying 2 value in database

I have a database. I created it with HeidiSQL. Its look like this.
I enter the value-1 and value-2.
Is there a way to enter a formula to Result column like " =Value-1 * Value-2 " ? I want my database to calculate the Result when I enter my values to other cells.
A trigger is one way to achieve automated column content.
A second one is a view, which you can create additionally to the table. That view could contain SQL which generates the result:
SELECT value1, value2, value1*value2 AS result
A third (more modern) alternative is adding a virtual column in your existing table. You can do that with HeidiSQL's table editor, like shown in the screenshot. Just add a new column with INT data type, and set its Virtuality to "VIRTUAL", and Expression to "value-1 * value-2". That's it.
I'm not familiar with HeidiSQL, but it appears to be a front end? What RDBMS are you using, for example SQL Server allows a computed column.
SQL
ALTER TABLE YourTable
ADD Result AS ([Value-1] * [Value-2])
Right click your database name in the folder structure, go to --> create new then -->Trigger
Then you can create a trigger that when entering data, will be activated on the entire column like this:
But you will need to know how to write the actual query and function. This requires basic knowledge that is generally generic and consistent of most all SQL languages.

Crystal reports linking issue

I have 2 columns, they are both patient numbers. One is defined as a string
and the other is defined as Float, null. So the link is not working. What must I do to the one defined as number? It comes from an excel. I have changed the cells there to text, it's showing this change when I upload to SQL Server, but Crystal reports sees it as a number and it won't link on the string column.
I thought to add a column to the table then copy in the contents, is that the way to go?
ALTER TABLE [Programmer].[dbo].['Preventive Care-Colon Cancer Sc']
ADD PatNum datatype nvarchar(50)

Renaming column to previously existing column with different type won't let me update

Background: In a Java application I'm working on, I'm doing a refactoring of the storage of enum values. Previously, these were stored as integers, and mapped through enum values with a helper method in the enum. I would like to utilize the #EnumType.STRING capabilities of JPA to make the database more readable.
So, what I'm basically trying to do is change the type (as well as the values) of a column. For example, I had this table definition to begin with:
table Something (
id int,
source int,
[more columns]
)
I wanted to change the source-column into a VARCHAR(100) column instead, and here is how I did that:
Introduce a new column, called source_new with VARCHAR(100).
Populate the new column with mapped values based on the values of the old column (so each row with value 1 in the source column get's the value 'SomeSource' in source_new, each row with value 2 in source gets 'OtherSource', and so on
Drop the source-column
Rename the source_new column to source (using sp_rename)
My problem is this: Once this is done, I can't update the now newly defined source-column, because it still insists that it's an int column, and not a varchar column!
So a query like this:
update Something set source = 'SomeSource' where id = 1;
fails with this:
Error: Conversion failed when converting the varchar value 'SomeSource' to data type int.
SQLState: 22018
ErrorCode: 245
At the same time, sp_help of the table shows that the column is defined as varchar(100), and not int! Also, the column holds numerous varchar values from the original datamigration (from before the rename).
Is this a bug, or am I doing something wrong by renaming a column to a column name that was previously used with another type? (And as I'm typing the last question, it just sounds absurd to me, when I drop a column I expect to disappear, not to leave traces and in effect not allowing me to reuse the column name at any time in the future..)
SQLFiddle to illustrate (sp_rename doesn't work with SQLFiddle it seems): http://sqlfiddle.com/#!3/0380f/3
I have found the culprit, and it's name is trigger!
Some genious decided to put a check if the value updated is a valid source (checking against another table) in a trigger, so much for trusting your own code..
I spit on the shadow of people who hide functionality in database triggers, pfoy! Go back to the 80's where you belong! :p

Add a field description to a DB2/400 file

I have a set of files in a library on an AS/400/iSeries/System-I/whatever-IBM-wants-me-to-call-it-these-days which I do not have DDS for (created in SQL I gather) but to which I would like to add field descriptions. I cannot find a way to do this on the 400.
Does anyone know how I can add field description? Is directly updating SYSIBM.SQLCOLUMNS to set COLUMN_TEXT safe?
(I am not looking for general SQL answers here, only DB2/400 specific answers. Unfortunately given IBMs renamings of this platform and the very un-google-able name they chose, it is almost impossible to find answers (or especially to prove that there is NOT an answer) to such questions without spending ages looking through there voluminous, impenetrable documentation)
Use the LABEL SQL command. If you are using the green-screen STRSQL command, you can prompt it. The LABEL command can be used to set both column text and column headings.
Here is an example of using the LABEL command to give column text for two fields in the file named TESTFILE1. The fields are named FIELD1 and FIELD2 in this example:
LABEL ON COLUMN TESTFILE1
(FIELD1 TEXT IS 'My Field 1 text', FIELD2 TEXT IS 'My Field 2 text')
Here is an example of using the LABEL command to give column headings that would show up in query results:
LABEL ON COLUMN TESTFILE1
(FIELD1 IS 'My field Heading 1',
FIELD2 IS 'My field Heading 2')
When creating column headings, you get 60 characters per field. The first 20 characters are line 1. The second 20 characters are line 2. The third 20 characters are line 3. In the above example, the field headings would look like this:
My field My field
Heading 1 Heading 2
As an additional hint, you can use iSeries Navigator to get the SQL statement for that file. If you fire up this program, log on to the iSeries, go to database, go to schemas, and go to tables, then you can find your file. You'll find the button "generate SQL" somewhere. This creates an exact SQL statement for you to create the table. You can use this SQL statement to work with this table.
For your questions, the "LABEL" from another answer is sufficient. This answer is usefull if you want to do more stuff, that can not be done with ALTER.