• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i want query for inserting two input parameters and remaining fields from other table in single quer

 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want query for inserting two input parameters and remaining fields from other table in single query.
example :
INSERT INTO Members ( memberName, userID)
VALUES('MYNAME', (SELECT userID FROM USERS WHERE userUNIQUEID = '123'));

i need to insert a row into member table.
i want to pass 'MYNAME' as parameter and UserId will be from select statement.

can any one please give me correct syntax of the query.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should work:
Add as many constants to the SELECT clause as you need (number of values in the SELECT clause must match the number of columns being inserted, of course).

Two notes:

Is userUNIQUEID really a VARCHAR? If it is a NUMBER, you should use 123 and not '123'.

If you plan to run this statement from Java, use PreparedStatement and replace literal values with binds.
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi thank you for the reply.
that was a example query.

i have found the solution
i need to give my parameter value in select clause.
INSERT INTO Members ( userID,memberName) SELECT userID,'MYNAME' FROM USERS WHERE userUNIQUEID = '123';

thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic