Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 INSERT query with VALUES and SELECT

Author  Topic 

chayahd99
Starting Member

1 Post

Posted - 2006-08-22 : 04:34:10
Hi Folks,

I'm having trouble phrasing an SQL query for my C# app.
The query is INSERT and uses a list of values (from textBoxes). Only one value needs to be retrieved by a query from a different table. How do I combine the two?

Here is what I have:

string iSQL = "INSERT INTO recipes (recipe_name, catagory_id, prep_time, instructions, comment) " +
"VALUES ( '" + form2.tbAddName.Text + "', " + qSQL + ", '" + form2.tbAddTime.Text + "', '" + form2.tbAddIns.Text + "', '" + form2.tbAddComment.Text + "')";

qSQL should be the following query except this is not legal code:

qSQL = "SELECT catagory_id from catagories WHERE catagory_name = '" + form2.cbCatagory.SelectedItem.ToString() + "'";

How do I phrase the query properly?

Thanks.

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-08-22 : 04:39:19
somthing like this

string iSQL = "INSERT INTO recipes (recipe_name, catagory_id, prep_time, instructions, comment) " +
"Select Top 1 '" + form2.tbAddName.Text + "', catagory_id, " + qSQL + ", '" + form2.tbAddTime.Text + "',
'" + form2.tbAddIns.Text + "', '" + form2.tbAddComment.Text + "'," from catagories WHERE catagory_name = '" +
form2.cbCatagory.SelectedItem.ToString() + "'"


I think there is some issues with the formating, but i hope you got the idea how to do it.

you need to do select query, and specify all the values of the textbox , in that select query and it should work fine.

Chirag
Go to Top of Page
   

- Advertisement -