Solved

Inserting values into Table columns

Posted on 2012-04-07
5
286 Views
Last Modified: 2012-04-07
Hello experts,

I created a tale in an Sql Server database using the following  code.


Create table FirstTable
(ID int Primary key Identity(1,1),
FName varchar(30) null,
LName varChar(50) null,
Comments varchar(100) null Default 'To be filled later'
)



As you can see the code allows for the column "Comments" to be null. The column also uses a default data which should be inserted automatically if the user doesn't enter his own data.

My problem is this: When I enter data directly (manually) into the table and leave out Column "Comments" the default is entered automatically as expected.

But when I try to insert a new record using code and leave out the  value for the "comments" column Sql Server generates an error telling me: "Column name or number of supplied values does not match table definition." Here is an example of how I tried to insert a new record:

insert into FirstTable
Values('Don','Thomas')
0
Comment
Question by:adamtrask
[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
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37819305
insert into FirstTable
Values('Don','Thomas','')
0
 
LVL 17

Accepted Solution

by:
Anuroopsundd earned 500 total points
ID: 37819315
problem with your command is you are not specifying columns in which the 2 values should go. so your insert command should be like below.

INSERT INTO Firsttable(Fname,Lname)
VALUES ('Don','Thomas')
0
 

Author Comment

by:adamtrask
ID: 37819321
but this would leave the column blank, it doesn't allow for the default to be entered
0
 

Author Closing Comment

by:adamtrask
ID: 37819338
Thank you
0
 
LVL 3

Expert Comment

by:taz8020
ID: 37819870
Hi adamtrask, if you are making a desktop application you would be better doing this at the dataset, as the sql query will not insert the default value.

eg if using vb
Me.MyDataSet.Tables!MyTable.Columns("MyDateColumn").DefaultValue = "what erver the defualt value is"
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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