r.r
r.r

Reputation: 7173

delete specific entries in table

In my table I have many rows with data. One row has seven columns with values.

Is it possible to delete from one row values of two columns and rest five leave without changes?

Can I to it with SQL delete?

   cmd.CommandText = @"DELETE ImageData,"
                     + " ContentType,  "
                     + " FROM Users "
                     + " WHERE UserName = @UserName";

    cmd.Parameters.Add(new SqlParameter("@UserName", username));
    cmd.Parameters.Add(new SqlParameter("@ImageData", ImageData));
    cmd.Parameters.Add(new SqlParameter("@ContentType", ContentType));

In my code I can't delete like this, is it wrong? Does anyone know how to delete them?

Upvotes: 1

Views: 179

Answers (3)

Brett
Brett

Reputation: 4061

you could update the row:

UPDATE Users
SET ImageData = NULL, ContentType = NULL
WHERE UserName = @UserName

Upvotes: 1

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58491

I assume it's not DELETE you need but UPDATE.

  • DELETE always removes an entire row.
  • UPDATE allows you to change individual columns in a row.

codefragment

@"UPDATE Users "
  + "SET ContentType = NULL, "
  + "    ImageData = NULL "
  + "WHERE Username = @UserName";

Upvotes: 2

Martin Smith
Martin Smith

Reputation: 453920

You need to use an UPDATE statement for this. DELETE is only for deleting whole row(s) at a time.

You can't actually "delete" them as though it was cells in a spreadsheet though. Assuming the columns are nullable you can set them to NULL as below.

cmd.CommandText = @"UPDATE ImageData
                    SET ContentType = NULL, Users = NULL
                    WHERE UserName = @UserName";

            cmd.Parameters.Add(new SqlParameter("@UserName", username));

Upvotes: 1

Related Questions