Results 1 to 6 of 6

Thread: Running a complex query in VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2011
    Posts
    4

    Question Running a complex query in VB

    Hello,

    I'm trying to find some way to run a complex query (not just a simple select statement) against a database and the output is a table.
    More details:
    I have a sql database that contains two tables Tickets and TicketDetails joined on by the Key TicketNumber. Now, the ticket numbers are not consecutive which means that the Ids in the Tickets table can be "1-2-4-16-20-50" and so on. I need to create a windows form application that can display the missing ticket numbers as well as the existing ticket numbers. So in some way I will display graphically the existing ticket details and next to the missing numbers I will display "Does not exist" for example:
    1 - "Person1 - Age1 "
    2 - "Person2 - Age2 "
    3 - "Does not exist"
    4 - "Person4 - Age3"
    5 - "Does not exist"
    ....etc.
    To do this, I thought of this query:

    Code:
    declare @from int
    declare @to int
    
    SET @from=5000
    SET @to=5100
    
    CREATE TABLE #mytable
    (
    	TicketNo INT
    )
    
    While (@from<=@to)
    BEGIN
    	INSERT INTO #mytable (TicketNo)
    	VALUES (@from)
    
    	set @from=@from+1
    END
    
    SELECT  #mytable.TicketNo, TicketDetails.Person, TicketDetails.Age 
    FROM #mytable
    LEFT JOIN Tickets on #mytable.TicketNo=Tickets.TicketNo
    LEFT JOIN TicketDetails ON Tickets.TicketNo=TicketDetails.TicketNo
    
    DROP TABLE #mytable
    I can't find a way to run this query on my windows form application. I can only run simple select statements to query the database, I also can't create a stored procedure in my database to do this, because this is not a local database, it's a shared database and I can't alter it.

    Is there a way I can run this query on my windows form application and return the result in a grid ?

    Thanks in advance,
    M

  2. #2
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Running a complex query in VB

    Well, since you "DROP TABLE" on the end of the query it will never show any result. But have a look at this: http://www.sourcecodester.com/tutori...basic-net.html


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Running a complex query in VB

    Quote Originally Posted by Malhoosh View Post
    I can't find a way to run this query on my windows form application. I can only run simple select statements to query the database
    Really? What happens if you create a data adapter with that SQL code in the SelectCommand and then call Fill? Are you saying that an exception is thrown?

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Running a complex query in VB

    Quote Originally Posted by Radjesh Klauke View Post
    Well, since you "DROP TABLE" on the end of the query it will never show any result. But have a look at this: http://www.sourcecodester.com/tutori...basic-net.html
    Not true... there's a select just before the drop... common practice... I do that all the time... select from the temp table, then promptly drop it... that's how you should do it.


    wait... hold on... why is there a temp table in the first place? It doesn't seem to serve any purpose at all. Ah... never mind... I see it's a left join... I thought I saw an inner join... OK....

    what DBMS are you using? And what version? There might be a better way to skin this cat.

    -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??? *

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Running a complex query in VB

    I'd be inclined to suck in the data you have, then add in the missing rows on the app side. I don't know which would prove to be more efficient.
    My usual boring signature: Nothing

  6. #6
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Running a complex query in VB

    there's a select just before the drop
    Aaaaah... overlooked that part.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

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