1
 SqlConnection connection = new SqlConnection(connString.ToString());
    string select = "SELECT (CASE WHEN MAX(page_no) IS NULL  THEN 1 ELSE MAX(page_no)+1 END) FROM dbo.BOOK";
    string insert = "INSERT INTO dbo.BOOK (book_id,select) VALUES (121,4)";
    SqlCommand sqlCommand = new SqlCommand(insert,connection);
    insert.ExecuteNonQuery();

Here I got exception where the insert contains invalid string select. Please tell me how assign sub query within the insert?

5
  • Why you are using select query inside insert query? Commented Sep 15, 2012 at 5:28
  • did you try your query on SQL Query browser? If not try it once. If it works there it will definitely work with my code Commented Sep 15, 2012 at 5:38
  • I think now you would understood why I'm using select statement. (I had edited the select statement). Commented Sep 15, 2012 at 5:41
  • What are you trying to achieve with sub query inside Insert query Commented Sep 15, 2012 at 5:47
  • exactly. insert = "INSERT INTO dbo.BOOK (book_id,SELECT (CASE WHEN MAX(page_no) IS NULL THEN 1 ELSE MAX(page_no)+1 END) FROM dbo.BOOK) VALUES (121,4)" ------ this works but through select variable I cant. I need Help. Commented Sep 15, 2012 at 5:49

3 Answers 3

1

you cannot use a select statement like this if you want to use a sub query it has to be in single statement

but in above statement you write in different different statement for selection and insert query .

so cmd.ExecuteNonquery() execute only insert text statement so SQL engine unable to find SELECT(and SELECT is a Reserved keyword) so it gives you a error

if you go with subquery try this

SqlConnection connection = new SqlConnection(connString.ToString());

string select = "SELECT 121, (CASE WHEN MAX(page_no) IS NULL  THEN 1 ELSE MAX(page_no)+1 END) FROM dbo.BOOK";
string insert = "INSERT INTO dbo.BOOK (book_id,[select]) "+select;

SqlCommand sqlCommand = new SqlCommand(insert,connection);

sqlCommand.ExecuteNonQuery();

Sign up to request clarification or add additional context in comments.

5 Comments

How can I do the select within an insert query using c#?
so you want select and insert in single query ?
insert = "INSERT INTO dbo.BOOK (book_id,SELECT (CASE WHEN MAX(page_no) IS NULL THEN 1 ELSE MAX(page_no)+1 END) FROM dbo.BOOK) VALUES (121,4)" ------ this works but through select variable I cant. I need Help.
the code that you have give showed an exception : 'Invalid cloumnname select'
insert.ExecuteQuery()-- wrong. It should be sqlCommand.ExecuteQuery(). --please edit your answer. I changed it but showed exception.
1

Your query results will return a DataTable. So use a DatAdapter to fill a DataTable.

Comments

1

You are doing it wrong, you have to Execute query on SQLCommand object not on string object try this

using(SqlConnection connection = new SqlConnection(connString.ToString())){
string insert = "Insert Query";
using (SqlCommand sqlCommand = new SqlCommand(insert,connection))
{
 con.Open();
 int i = sqlCommand.ExecuteNonQuery();
}
}

Update:

 var selectQuery = "SELECT (CASE WHEN MAX(page_no) IS NULL THEN 1 ELSE MAX(page_no)+1 END) FROM dbo.BOOK";
 var insertQuery = string.format("INSERT INTO dbo.BOOK (book_id,{0}) VALUES (121,4)",selectQuery);

12 Comments

can you explain why to have new variable i?
That's your wish actually you can use it or not, that will have the return value of ExecuteNonQuery() method.
@AnantDabhi Why you are getting that doubt
the updated ans showed error :"Index (zero based) must be greater than or equal to zero and less than the size of the argument list".
you will get that error if you did something wrong in string.format() can you show your string.format() code
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.