Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am inserting two values to a table 1 from other table and other from form control but i am getting error i am not able to understand where is my query wrong


VB
sqlstr ="INSERT INTO partylinks ( accountid, partyid ) values(Select max(accountsinfo.accid) FROM accountsinfo,2 )""
               cmd = New OleDb.OleDbCommand(sqlstr, con, tra)
               cmd.ExecuteNonQuery()
               tra.Commit()
Posted

When you are inserting then the column count and type should match and the format is INSERT INTO tablename (columns) SELECT (columns) from tablename : http://www.w3schools.com/sql/sql_insert_into_select.asp[^]
try:
SQL
INSERT INTO partylinks ( accountid, partyid ) Select max(accountsinfo.accid),2 FROM accountsinfo -- moved the ,2
 
Share this answer
 
Comments
yash00121 11-May-15 7:43am    
1 value is from other table and other value is from a user input .
INSERT INTO TARGET_TABLE_NAME(Field1, Field2)
values (SELECT Field1
FROM SOURCE_TABLE_NAME ,2ndvalue)
this is not working
The format of INSERT INTO command with select is as follows

SQL
INSERT INTO TARGET_TABLE_NAME(Field1, Field2)
SELECT Field1, Field2
FROM   SOURCE_TABLE_NAME


You need to change your SQL as explained above and try again
 
Share this answer
 
Comments
yash00121 11-May-15 7:43am    
1 value is from other table and other value is from a user input .
INSERT INTO TARGET_TABLE_NAME(Field1, Field2)
values (SELECT Field1
FROM SOURCE_TABLE_NAME ,2ndvalue)
this is not working
_Asif_ 11-May-15 7:53am    
Have you tried the query that Mehdi shown on his answer? Try this!
INSERT INTO partylinks ( accountid, partyid ) Select max(accountsinfo.accid),2 FROM accountsinfo

and don't use values clause, its invalid
yash00121 11-May-15 8:02am    
INSERT INTO TARGET_TABLE_NAME(Field1, Field2)
values (SELECT Field1
FROM SOURCE_TABLE_NAME ,2ndvalue)
syntax is correct i think but not working . check this out

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900