Solved

Inserting values into Table columns

Posted on 2012-04-07
5
281 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
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 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