Results 1 to 8 of 8

Thread: [2008] SQL Command, delete all values in a column, in a table

  1. #1

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    [2008] SQL Command, delete all values in a column, in a table

    I have seen this sort of code:

    Dim del As New SqlCommand("DELETE FROM Table1")
    or
    Dim del As New SqlCommand("DELETE FROM Table1 WHERE ID = 3")

    (Delete From is the same as Delete * From as I have read in my tutorial)

    but I want to do this:

    Dim del As New SqlCommand("DELETE FROM Table1 IN COLUMN stringValues")

    So it delete's all items added to stringValues.

    Can anyone help me with this, I have searched for this with no success.

    Cheers

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] SQL Command, delete all values in a column, in a table

    You don't DELETE values from columns. A DELETE statement deletes rows. If you want to change a field value then you UPDATE that row and set that field. If you don't want a field to contain a value then you set it to NULL.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] SQL Command, delete all values in a column, in a table

    So let me get this right?

    DataTable contains rows, and rows contain columns (aka data fields?)

    Do I have that right?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] SQL Command, delete all values in a column, in a table

    I've already explained that in a previous thread of yours. If you don't know how a DataTable works then go to the MSDN Library and read the documentation for the DataTable class.

    That's not necessarily relevant to this question anyway. If you want to execute an UPDATE statement against every and set a particular field to the same value in each then there's no need for a DataTable. You don't need to retrieve any data so what would the DataTable be for?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] SQL Command, delete all values in a column, in a table

    ugh, I have read the documentation for DataTables and stuff.

    I just don't see visually in my head the relationship between table, column, and row.

    To me, Table contains columns, and column contains rows, so when I do

    Dim sqlCommand("SELECT preferences_value FROM myTable")...

    I think in my mind, that that will select all rows in the column preferencs_value, but I don't understand which row is selected?

    Do you see how confused I am.

    If anyone could explain to me how this works in a very basic manner.

    Cheers

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] SQL Command, delete all values in a column, in a table

    What does this even mean?
    I think in my mind, that that will select all rows in the column preferencs_value, but I don't understand which row is selected?
    You say you think that that column of all rows is selected, which is correct. You then say you don't understand which row is selected. You just said yourself that they all are. When that query is executed it retrieves the only the specified column of every row in the table and populates a DataTable with that data. Therefore you will end up with a DataTable with one column and N rows where N is the number of rows in the table in the database. If you'd executed the query you could have examined the DataTable to see how many columns and rows it had, and the properties of each.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2008] SQL Command, delete all values in a column, in a table

    Okay, thanks I see now.

    Cheers

  8. #8
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] SQL Command, delete all values in a column, in a table

    I think in short, the SQL command you're looking for is:

    Dim del As New SqlCommand("UPDATE Table1 SET stringValues = NULL;")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width