views:

34

answers:

3

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?

+1  A: 

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));
Martin Smith
you mean so: cmd.CommandText = @"UPDATE Users " + " SET ContentType =" + null + ", " + "ImageData =" + null + " " + " WHERE UserName = @UserName "; ???
Ragims
it doesnt work:( maybe syntax is wrong
Ragims
@Ragmis See edit for C# version. If it doesn't work you need to give more details (does it give you an error message - If so what?)
Martin Smith
@Ragims, what error message do you get?
Nathan Koop
nathan i solved my problem , thanks!!
Ragims
+2  A: 

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";
Lieven
greate!!! thanks, that works!
Ragims
+1  A: 

you could update the row:

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