?
Solved

ADD FIELD SET A DEFAULT VALUE SQL

Posted on 2008-06-12
10
Medium Priority
?
5,813 Views
Last Modified: 2013-11-23
Hi Geniuses, Gurus, Experts, et al.,
I want to make sure I understand. There was a question answered before about setting default values when creating a table. It was closed and the expert said there was a M$ bug. Are you saying there is no way to create a column that contains a default value when programming in Delphi and working with an Access 2000 database? I tried the following and it didn't work:

with MyQuery do
begin
    Close;
    SQL.Clear;
    SQL.Add('ALTER TABLE ' + TableName + ' ');
    SQL.Add('ADD COLUMN Deleted CHAR(1) DEFAULT ''N''');
    ExecSQL;
  end;

Help please.
0
Comment
Question by:reforest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 18

Expert Comment

by:Johnjces
ID: 21776092
One problem as I see it is you should be using an ADOCommand. Works with Access fine.

Here is an example...

 ADOCommand1.CommandText := 'Alter Table CDLib ADD CDKey Text(50)';
 ADOCommand1.Execute;
 ADOCommand1.CommandText := 'Alter Table CDLib ADD Comments Memo';
 ADOCommand1.Execute;

You can then add your default text as you did above.

I also would use quoted string. I feel it makes quoted strings in strings easier and more fool proff.:

SQL.Add('ADD COLUMN Deleted CHAR(1) DEFAULT ' + QuotedStr('N'));

Hope this helped.

John
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 21777331
"I tried it and it didn't work"
-please provide details as to in what way it did not work, such as any error messages or the results.

Just a minor note about the field "Deleted" - I don't think it's a reserve word but just in case it is you may want to put it in quotes or show it as a descendant of a table
Example
SELECT MyTable.Deleted  FROM MyTable
0
 
LVL 6

Expert Comment

by:bokist
ID: 21777353
Hi,

Try this way :
SQL.Add('ADD COLUMN Deleted CHAR(1) DEFAULT ''N'' with values');
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:reforest
ID: 21780335
The following code created the column, but did not put any values into rows that already contained data, and the last row (when viewed in Access) had #Error in the Deleted column:

DataMod.CommandQuery.CommandText := 'ALTER TABLE Chemicals ADD COLUMN Deleted CHAR(1) DEFAULT ' + QuotedStr('N');
DataMod.CommandQuery.Execute;

The following code caused an error that said 'Syntax error in ALTER TABLE statement':
with MyQuery do
  begin
    Close;
    SQL.Clear;
    SQL.Add('ALTER TABLE Chemicals ADD COLUMN Deleted CHAR(1) DEFAULT ''N'' with values');
    ExecSQL;
  end

So, still nothing that adds 'N' to my new column. Thanks for the help, but more ideas needed.
0
 
LVL 54

Accepted Solution

by:
nico5038 earned 750 total points
ID: 21786997
I'm afraid that the CREATE TABLE statement of the Jet-engine (the access database engine) doesn't support default values.
When you're able to open the Access Tables collection from Delphi with code, then a default value can be added. I used VBA code in the past to manipulate table field properties, but that's "within" Access and not in the database engine....

Nic;o)
0
 

Author Closing Comment

by:reforest
ID: 31466820
Thanks for the help. It always stinks when the answer is, 'You can't do it the easy way'. Oh well. I've figured out a workaround now.
0
 
LVL 54

Expert Comment

by:nico5038
ID: 21807159
Glad I could help, but now I'm curious how your work around is implemented....
I guess you use an UPDATE query or add the Default value to the form field before the INSERT.

Nic;o)
0
 

Author Comment

by:reforest
ID: 21809846
Actually, it's not anything interesting. I found a way to do what I wanted to do without entering a default value at all. I'm such a beginner that I was having trouble trying to select rows where MyTextField = ''. I thought if I added a default, then I could SELECT * from MyTable WHERE MyTextFIeld = 'Default'. I now know that I need to SELECT * from MyTable WHERE MyTextFIeld Is Null.

BTW, I still have no idea how to rename columns or copy data from one column to another within the same table...want me to submit a new question?
0
 
LVL 54

Expert Comment

by:nico5038
ID: 21817963
No need for a new question, as I'm not "in for the points" :-)

To copy data from column to column use:

        UPDATE tblA SET fieldA = FieldB

Without a WHERE clause all rows will be effected. (Always try first on a backup copy of the table when manipulating data in tables!)

Renaming is done with the "AS" predicate like:

        SELECT fieldA AS Column1 from tblA

This will give one column named "Column1" in the output of the query.

Success with your application !

Nic;o)
0
 

Author Comment

by:reforest
ID: 21819115
Thanks for the help, but I'm still having trouble.

Before you sent this reply I opened a question titled 'UPDATE table SET col1 = col2 not working', because I found something on the internet about copying columns in SQL. The question is still open, though, because it's not working in Delphi. It works if I open my Access database and run the UPDATE query, but not when I try to do it in code from Delphi.

I have successfully run other types of queries (SELECT INTO, ALTER TABLE, ALTER COLUMN, etc.), so I know how it works most of the time. It's just not working for my UPDATE query.

Do you program in Delphi?
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question