0

I am struggling to generate a 'notifications' query to work correctly at the moment.

I'm working on a query that will add a string to a table in which will add multiple rows at once, with only one column of which will be changed in this 'bulk insert':

I currently have:

    public static void addNotification(string notification)
    {
        SqlConnection scon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);

        using (SqlCommand scmd = new SqlCommand())
        {
            scmd.Connection = scon;
            scmd.CommandType = CommandType.Text;
            scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged) VALUES ((SELECT Email FROM AspNetUsers), '" + notification + "' ,0)";
            scon.Open();
            int status = scmd.ExecuteNonQuery();
            scon.Close();
        }
    }

However, on passing this a string like: "this is a notification", it comes up with the following error:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. The statement has been terminated.

So, could someone possibly explain how this query should be run?

The table has the columns:

enter image description here

I understand the 'PK1' isn't the best of practises, but this is only a table for testing purposes (it's set to auto increment)

So, the isAcknowledged should be set to 0, the notification should be this string, and the email should be selected from each row of the table AspNetUsers?

Would anyone e able to explain to me how I should go about adding this data into my db?

1

4 Answers 4

2

Something like this should work:

scmd.CommandText = "INSERT INTO notificationAck (Email, 
                                                 Notification, 
                                                 isAcknowledged) 
                    SELECT Email, '" + notification + "', 0
                    FROM AspNetUsers";

INSERT INTO SELECT does not require VALUES. Hence, you only have to use a SELECT statement that retrieves Email field from AspNetUsers plus two constant values.

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

3 Comments

Thank you for your explanation. Reiterating what I've also commented, I didn't realise I had to place the table name at the end of the statement, not within it
@jbutler483 You' re welcome. Please also note the you should use an SqlParameter object in place of notification variable. Your code is prone to SQL Injection.
Admin controls only here :)
2

You don't want to use VALUES when using a query to generate your results. You can change the INSERT query to this:

INSERT INTO notificationAck (Email, Notification, isAcknowledged) 
SELECT Email, 'SOME STRING', 0 FROM AspNetUsers

Which translated in to your code would look like:

scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged) 
                    SELECT Email, '" + notification + "', 0 FROM AspNetUsers";

1 Comment

Thank you for your prompt response. I didn't realise I had to place the table name at the end, which was causing an issue as well. So thank you :)
0

Select Top 1 in Sub Query if you want to insert only 1 row

try

INSERT INTO notificationAck (Email, Notification, isAcknowledged) 
VALUES ((SELECT top 1 Email FROM AspNetUsers), '" + notification + "' ,0)

if you want to insert all rows from AspNetUsers then use

 INSERT INTO notificationAck(Email,Notification, isAcknowledged) select 
 Email,''+notification +"',1 from AspNetUsers

1 Comment

thank you for your answer. However, I needed to insert for each email returned.
0

Your query

SELECT Email FROM AspNetUsers

returns more than one row as your error says. You need to change it so that it returns only one row, for example:

SELECT TOP 1 Email FROM AspNetUsers

Then change your command as below

scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged) SELECT TOP 1 Email " + notification + "' ,0 FROM AspNetUsers'";

if you want to insert more than one row, the remove top 1 from query.

scmd.CommandText = "INSERT INTO notificationAck (Email, Notification, isAcknowledged) SELECT Email " + notification + "' ,0 FROM AspNetUsers'";

2 Comments

thank you for your answer. However, I needed to insert for each email returned.
Okay. It seems that I misunderstood your question. You are right of course.

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.