Results 1 to 6 of 6

Thread: VB Give integer value to column based on sort

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    142

    VB Give integer value to column based on sort

    I am building a VB.Net applications using SQL server and cannot figure the following out :

    I need to give a sequential integer (starting from 1) to a column in the database table (column called Prioriteit) based on a sort that is as follows :

    First column kultivar (i.e. string Ascending), then GroepperingsGroep (i.e. string Ascending), Then LaasteOntledingsdatum (i.e. date oldest to newest) and lastly Laastesuiker (i.e. real largest to smallest). According to this sort integer values should be given to Prioriteit for each GroepperingsGroep. I am attaching a picture to explain the end result needed

    Name:  Sorteer.jpg
Views: 557
Size:  46.1 KB.


    Regards

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

    Re: VB Give integer value to column based on sort

    Execute a query against the database using an appropriate ORDER BY clause. Use a data adapter to populate a DataTable. Use a For loop to iterate over the Rows of that DataTable and set that Prioriteit column based on the loop counter. When done, use the same data adapter to save the changes. Note that the only columns you need to retrieve are the primary key and the one you want to modify.
    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
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    142

    Re: VB Give integer value to column based on sort

    Thank you jmcilhinney. Came up with the following :

    SQL :

    Code:
    ;WITH CTE AS 
    (
        SELECT ROW_NUMBER() OVER (PARTITION BY kultivar,GroepperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) NewPrioriteit,
               *
          FROM myTable
    )
    UPDATE CTE
       SET CTE.Prioriteit = CTE.NewPrioriteit


    VB.Net :

    vb.net Code:
    1. Dim konneksie As New SqlConnection
    2.         Dim opdragprioriteit As New SqlCommand
    3.         konneksie.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Administrator\Desktop\SKEDULERING\Skedulering6\Skedulering6\SkeduleringsDatabasis6.mdf;Integrated Security=True"
    4.         konneksie.Open()
    5.         opdragprioriteit.Connection = konneksie
    6.         opdragprioriteit.CommandText = "WITH CTE AS " &
    7.         "  (Select Case ROW_NUMBER() OVER (PARTITION BY kultivar,GroeperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) NewPrioriteit, * " &
    8.         " From skedulering2)" &
    9.         " Update CTE" &
    10.         " SET CTE.Prioriteit = CTE.NewPrioriteit "
    11.  
    12.  
    13.         opdragprioriteit.ExecuteNonQuery()
    14.         konneksie.Close()


    Working in SQL, however I get a error message (Incorrect syntax near 'NewPrioriteit'). What syntax am I missing?

    Regards and thank you.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    142

    Re: VB Give integer value to column based on sort

    Thank you jmcilhinney. Came up with the following :

    SQL :

    Code:
    ;WITH CTE AS 
    (
        SELECT ROW_NUMBER() OVER (PARTITION BY kultivar,GroepperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) NewPrioriteit,
               *
          FROM skedulering2
    )
    UPDATE CTE
       SET CTE.Prioriteit = CTE.NewPrioriteit


    VB.Net :

    vb.net Code:
    1. Dim konneksie As New SqlConnection
    2.         Dim opdragprioriteit As New SqlCommand
    3.         konneksie.ConnectionString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Administrator\Desktop\SKEDULERING\Skedulering6\Skedulering6\SkeduleringsDatabasis6.mdf;Integrated Security=True"
    4.         konneksie.Open()
    5.         opdragprioriteit.Connection = konneksie
    6.         opdragprioriteit.CommandText = ";WITH CTE AS " &
    7.         "  (Select Case ROW_NUMBER() OVER (PARTITION BY kultivar,GroeperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) NewPrioriteit, * " &
    8.         " From skedulering2)" &
    9.         " Update CTE" &
    10.         " SET CTE.Prioriteit = CTE.NewPrioriteit "
    11.  
    12.  
    13.         opdragprioriteit.ExecuteNonQuery()
    14.         konneksie.Close()


    Working in SQL, however I get a error message in VB.Net (Incorrect syntax near 'NewPrioriteit'). What syntax am I missing?

    Regards and thank you.
    Last edited by GideonE; Nov 25th, 2018 at 11:39 PM.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: VB Give integer value to column based on sort

    What you're doing wrong is that you can't create a CTE and update... because you can't update a CTE... I'm not even sure why you created the CTE in the first place...
    Code:
    Select Case ROW_NUMBER() OVER (PARTITION BY kultivar,GroeperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) NewPrioriteit, * " &
            " From skedulering2
    And as I paste that, I see that you used a CASE statement... ????? why? No...

    Code:
    Select 
        kultivar,
        LaasteOntledingsdatum,
        Laastesuiker,
        GroepperingsGroep,
        ROW_NUMBER() OVER (PARTITION BY kultivar, GroeperingsGroep ORDER BY LaasteOntledingsdatum Asc, Laastesuiker Desc) Prioriteit
    From skedulering2
    There... nothing complicated about it...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    142

    Re: VB Give integer value to column based on sort

    Thank you technome. Your code works well to select, but how do then do I update skedulering2?

Tags for this Thread

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