SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Update three tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

JazziBear
Starting Member

New Zealand
1 Posts

Posted - 05/05/2012 :  19:02:00  Show Profile  Reply with Quote
Hi Guy's (Newbee first post)

I am trying to update 3 tables using 1 form

My DB is in Access

I have SQLServer 2012

I am writing code in VisualStidio 2010

All table keys are auto incremented so i am thinking I dont need to include the tble Id's in my queries

(If I do then how as numbers are already uniquely generated)

The code I am using is as follows



insertCommand.CommandText = "INSERT INTO Owner VALUES ('" & tbxFirstName.Text & "', '" & tbxLastName.Text & "')"

            insertCommand.CommandText &= "INSERT INTO CD VALUES ('" & tbxArtist.Text & "', '" & tbxAlbum.Text & "')"

            insertCommand.CommandText &= "INSERT INTO CDTracks VALUES ('" & tbxTrackName.Text & "')"




When I run this code I get the following error

SqlException was unhandled

Column name or number of supplied values does not match table definition

Then when I check my DB I find that the first table has been updated

any help wolud be massively appreciated

visakh16
Very Important crosS Applying yaK Herder

India
47084 Posts

Posted - 05/05/2012 :  19:10:50  Show Profile  Reply with Quote
you should include column list also for insert

like:-

]
insertCommand.CommandText = "INSERT INTO Owner (Col1Name,Col2Name) VALUES ('" & tbxFirstName.Text & "', '" & tbxLastName.Text & "')"

            insertCommand.CommandText &= "INSERT INTO CD (Col1Name,Col2Name) VALUES ('" & tbxArtist.Text & "', '" & tbxAlbum.Text & "')"

            insertCommand.CommandText &= "INSERT INTO CDTracks (ColName) VALUES ('" & tbxTrackName.Text & "')"



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 05/05/2012 :  20:59:51  Show Profile  Reply with Quote
quote:
Then when I check my DB I find that the first table has been updated

If the values you provide exactly matches the number and ordinal positions of the columns, only then can you omit the column names. So in your case, the Owner table probably has two columns; so that statemetn succeeded. The CD table has more than two columns (or only one column) and hence the error message. For example:
CREATE TABLE #tmp1(id1 INT, id2 INT);

-- I can do this - because I am supplying id1 and id2.
INSERT INTO #tmp1 VALUES (1,1);

-- But I cannot do this - because I am supplying only one value.
INSERT INTO #tmp1 VALUES (2);

-- Instead, I would do this IF I want id2 to be null in this insert.
INSERT INTO #tmp1 (id1) VALUES (2);

-- 
DROP TABLE #tmp1;
If you have autoincremented columns, leave them out (from the column list and the values list).
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000