Solved

Inserting values into Table columns

Posted on 2012-04-07
5
273 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
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

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

Accepted Solution

by:
Anuroopsundd earned 500 total points
Comment Utility
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
Comment Utility
but this would leave the column blank, it doesn't allow for the default to be entered
0
 

Author Closing Comment

by:adamtrask
Comment Utility
Thank you
0
 
LVL 3

Expert Comment

by:taz8020
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article I will describe the Detach & Attach 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.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

728 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

11 Experts available now in Live!

Get 1:1 Help Now