Solved

Inserting values into Table columns

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

In this article I will describe the Backup & Restore 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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

772 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