Solved

Add Fields in a table

Posted on 2002-06-11
22
377 Views
Last Modified: 2007-11-27

How to check if a field(column) exists in the table?
Also, How to add the column if it does not exist in the table?

Thank you
0
Comment
Question by:Budenk
  • 10
  • 6
  • 3
  • +2
22 Comments
 
LVL 18

Expert Comment

by:mgfranz
ID: 7070258
ALTER TABLE table {ADD {COLUMN field type[(size)] [NOT NULL][CONSTRAINT index] | CONSTRAINT multifieldindex} |DROP {COLUMN field I CONSTRAINT indexname} }

Description
 
table
 The name of the table to be altered.
 
field
 The name of the field to be added to or deleted from table.
 
type
 The data type of field.
 
size
 The field size in characters (Text and Binary fields only).
 
index
 The index for field. See the CONSTRAINT clause topic for more information on how to construct this index.
 
multifieldindex
 The definition of a multiple-field index to be added to table. See the CONSTRAINT clause topic for more information on how to construct this clause.
 
indexname
 The name of the multiple-field index to be removed.
 
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 7070260
Use ADD COLUMN to add a new field to the table. You specify the field name, data type, and (for Text and Binary fields) an optional size. For example, the following statement adds a 25-character Text field called Notes to the Employees table:
ALTER TABLE Employees ADD COLUMN Notes TEXT(25)
You can also define an index on that field. For more information on single-field indexes, see the CONSTRAINT clause topic.

If you specify NOT NULL for a field, then new records are required to have valid data in that field.
0
 

Author Comment

by:Budenk
ID: 7070435
So, I just need the command object to run that query?
Is there any way that I can use Field Coleections to append the data.
I tried using field collections, but I get this erroe messsage
****************
ADODB.Fields error '800a0c93'

Operation is not allowed in this context.

/fiber/addfield.asp, line 18
*****************

Line 18 : rsX.Fields.Append "Node2", adVariant
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 7070450
"Field Collections to append the data"?  You lost me...

What are you trying to do?
0
 

Author Comment

by:Budenk
ID: 7070464
So, I just need the command object to run that query?
Is there any way that I can use Field Coleections to append the data.
I tried using field collections, but I get this erroe messsage
****************
ADODB.Fields error '800a0c93'

Operation is not allowed in this context.

/fiber/addfield.asp, line 18
*****************

Line 18 : rsX.Fields.Append "Node2", adVariant
0
 

Author Comment

by:Budenk
ID: 7070476
First, I would like to check if the field exists.
I use field collections for doing this check.
Then, if the field does not exist, I add a new field.
I am trying to use field collection also for this action.
I use append method as I typed above.
Am I using wrong way?
0
 

Author Comment

by:Budenk
ID: 7070557
First, I would like to check if the field exists.
I use field collections for doing this check.
Then, if the field does not exist, I add a new field.
I am trying to use field collection also for this action.
I use append method as I typed above.
Am I using wrong way?
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 7070571
You mean like this?

RS.CursorLocation = adUseClient
RS.Fields.Append "Name", adBSTR
RS.Fields.Append "City", adBSTR
RS.Open
RS.AddNew
0
 

Author Comment

by:Budenk
ID: 7070580
First, I would like to check if the field exists.
I use field collections for doing this check.
Then, if the field does not exist, I add a new field.
I am trying to use field collection also for this action.
I use append method as I typed above.
Am I using wrong way?
0
 
LVL 18

Accepted Solution

by:
mgfranz earned 50 total points
ID: 7070588
I found some good reading, its a little long, but if you capture it and resize the font it's easier to read; http://www.ee.iitb.ac.in/tilak/tutorials/vb6/BBASIC20.html

Lot's of information on ADO and CREATE, APPEND, ALTER, etc...
0
 

Author Comment

by:Budenk
ID: 7070631
First, I would like to check if the field exists.
I use field collections for doing this check.
Then, if the field does not exist, I add a new field.
I am trying to use field collection also for this action.
I use append method as I typed above.
Am I using wrong way?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Budenk
ID: 7070645
MGFranz,

That's what I meant with Field Collections.
Does that method work?

I already tried using Alter Table as you suggested, but I got this error:

************
Microsoft OLE DB Provider for SQL Server error '80040e14'

User does not have permission to perform this operation on table 'PCLinks'.
*************

I gave full permission to Public in SQL Server, but I still have the same error message.
What's wrong?

Thank you
0
 
LVL 18

Expert Comment

by:mgfranz
ID: 7070652
Hmm...
0
 

Author Comment

by:Budenk
ID: 7070657
MGFranz,

That's what I meant with Field Collections.
Does that method work?

I already tried using Alter Table as you suggested, but I got this error:

************
Microsoft OLE DB Provider for SQL Server error '80040e14'

User does not have permission to perform this operation on table 'PCLinks'.
*************

I gave full permission to Public in SQL Server, but I still have the same error message.
What's wrong?

Thank you
0
 

Author Comment

by:Budenk
ID: 7070667
MGFranz,

That's what I meant with Field Collections.
Does that method work?

I already tried using Alter Table as you suggested, but I got this error:

************
Microsoft OLE DB Provider for SQL Server error '80040e14'

User does not have permission to perform this operation on table 'PCLinks'.
*************

I gave full permission to Public in SQL Server, but I still have the same error message.
What's wrong?

Thank you
0
 

Author Comment

by:Budenk
ID: 7070672
MGFranz,

That's what I meant with Field Collections.
Does that method work?

I already tried using Alter Table as you suggested, but I got this error:

************
Microsoft OLE DB Provider for SQL Server error '80040e14'

User does not have permission to perform this operation on table 'PCLinks'.
*************

I gave full permission to Public in SQL Server, but I still have the same error message.
What's wrong?

Thank you
0
 
LVL 6

Expert Comment

by:Nitin Sontakke
ID: 7071461
Please use 'Reload Question' from top right corner and don't keep pressing F5/Refresh.

Fields collection cannot be appended as you are trying to do.

You might be connecting to a SQL server as a different user from Query Analyzer and yet another user when connecting from ASP. See your connect string for details.
0
 
LVL 33

Expert Comment

by:hongjun
ID: 7516161
This question appears to have been abandoned. Please come back to this question and do the necessary evaluation or feedback. I will invite a Moderator to close this question if there's no reply from you after seven days.

I suggest
<points to mgfranz>
for his efforts

** PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER **

hongjun
0
 

Expert Comment

by:modulo
ID: 7528512
Dear expert(s),

A request has been made to delete this Q in CS:
http://www.experts-exchange.com/Community_Support/CleanUp/Q_20411933.html

Without a response in 72 hrs, a moderator will finalize this question by:

 - assigning the points to mgfranz

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
0
 
LVL 33

Expert Comment

by:hongjun
ID: 7535310
I guess it is only fair to assign points to mgfranz if Budenk fail to reply within the 72 hours grace time.

hongjun
0
 

Expert Comment

by:modulo
ID: 7542332
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0
 
LVL 33

Expert Comment

by:hongjun
ID: 7549127
Great
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now