Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Inserting values into Table columns

Posted on 2012-04-07
5
Medium Priority
?
287 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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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