Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL Select Where query

SQL Select Where query

Scheduled Pinned Locked Moved Database
helpdatabasequestion
21 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Joe Stansfield

    Yea, from reading what you have said, I think my error may be in allowing a user to input a value into the query that is not a valid record (ie one that doesn't exist). So would the best approach be to run a query to test if input value is a record, then if true, proceed to the query which returns the record? The loop error is because of my attempting to sort a datagrid with linked data in it - if a null value is returned (ie no record), it gets caught in a loop and error because it can't sort a null record (along those lines). Thanks for all your help, even though I haven't fixed the problem I am picking up some valuable tips!

    F Offline
    F Offline
    fjdiewornncalwe
    wrote on last edited by
    #12

    You shouldn't need to run 2 queries. You should just need to check the resulting dataset to see if you get any results. (ie. rowcount > 0)

    I wasn't, now I am, then I won't be anymore.

    J 1 Reply Last reply
    0
    • F fjdiewornncalwe

      You shouldn't need to run 2 queries. You should just need to check the resulting dataset to see if you get any results. (ie. rowcount > 0)

      I wasn't, now I am, then I won't be anymore.

      J Offline
      J Offline
      Joe Stansfield
      wrote on last edited by
      #13

      Thanks for all the help guys. After reading what you have all posted and doing some more research I managed to solve the problem I was having. The basic query I was running was sufficient, it turned out the problem was in an automatically created linked query. By fixing that I have got ride of the errors I was getting.

      1 Reply Last reply
      0
      • J Joe Stansfield

        Yea, from reading what you have said, I think my error may be in allowing a user to input a value into the query that is not a valid record (ie one that doesn't exist). So would the best approach be to run a query to test if input value is a record, then if true, proceed to the query which returns the record? The loop error is because of my attempting to sort a datagrid with linked data in it - if a null value is returned (ie no record), it gets caught in a loop and error because it can't sort a null record (along those lines). Thanks for all your help, even though I haven't fixed the problem I am picking up some valuable tips!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #14

        Joe Stansfield wrote:

        So would the best approach be to run a query to test if input value is a record, then if true, proceed to the query which returns the record?

        The best approach is one without loops. Are you displaying the "linked record" in a separate (child) gridview? Usually, we combine the tables in Sql, like this;

        SELECT a.Field1
        ,a.Field2
        ,b.Field1 AS Field3
        FROM Employee AS a
        JOIN Department AS b ON a.DepartmentId = b.Id

        This will generate a single resultset, and depending on the type of join, the database will fetch the employees with, those without a linked record, or both :)

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        J 1 Reply Last reply
        0
        • J Joe Stansfield

          Hi all, Firstly, I must confess to being a very amateur programmer with a lot to learn! However, I'm battling away and am slowly getting my head around things. I have a query I have written which as follows:

          SELECT File_Name
          WHERE (File_Name = @File_Name1)

          File_Name1 is a user input parameter. My problem is that, even if the record does not exist, it returns the record, resulting in an error on the client side. Is it possible to edit that query to return a file_name field value of say, 1, if the searched record doesn't exist? Any help appreciated! (Also for note most of my queries are generated by VStudio, so my understanding of them leaves a lot to be desired...)

          A Offline
          A Offline
          Aadhar Joshi
          wrote on last edited by
          #15

          Hi, U can use following statment..

          SELECT ISNULL(File_Name, '')
          FROM Files
          WHERE File_Name = @FileName1

          because is is string so it should return blank

          1 Reply Last reply
          0
          • J Joe Stansfield

            Yea that will fix up the error from no value being entered. But I'm struggling to understand and make it work if, say, an input of 100 is entered. If 100 isn't a valid ID of a record, it won't return anything. I'd like it to return 1 for ease of avoiding an error and possible loop error (which could also be put down to poor programming by me on the C# side of things). With that said, if 99 is entered and that is a valid record, then 99 would need to be returned by the query. Sorry for not understanding your explanation.

            U Offline
            U Offline
            User 9474132
            wrote on last edited by
            #16

            ITS BETTER U YO USE THE IMMEDIATE CONSTRAINT DURING THE CODING

            S 1 Reply Last reply
            0
            • U User 9474132

              ITS BETTER U YO USE THE IMMEDIATE CONSTRAINT DURING THE CODING

              S Offline
              S Offline
              Simon_Whale
              wrote on last edited by
              #17

              Please don't shout (capital letters are considered shouting!). I would also refrain from using text speak as it upsets a lot of people on these forums.

              Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch

              1 Reply Last reply
              0
              • L Lost User

                Joe Stansfield wrote:

                So would the best approach be to run a query to test if input value is a record, then if true, proceed to the query which returns the record?

                The best approach is one without loops. Are you displaying the "linked record" in a separate (child) gridview? Usually, we combine the tables in Sql, like this;

                SELECT a.Field1
                ,a.Field2
                ,b.Field1 AS Field3
                FROM Employee AS a
                JOIN Department AS b ON a.DepartmentId = b.Id

                This will generate a single resultset, and depending on the type of join, the database will fetch the employees with, those without a linked record, or both :)

                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                J Offline
                J Offline
                Joe Stansfield
                wrote on last edited by
                #18

                Yea I have a linked record showing in a child view. So from what I can gather the first query executes to fill the main record table, and then a second query populates the correct record in the grid view (and a few other text boxes on the form). It is working now - but is it worth the time to write it as it should be? It won't affect any of my datasource bindings by combing it into a single query will it?

                L 1 Reply Last reply
                0
                • J Joe Stansfield

                  Yea I have a linked record showing in a child view. So from what I can gather the first query executes to fill the main record table, and then a second query populates the correct record in the grid view (and a few other text boxes on the form). It is working now - but is it worth the time to write it as it should be? It won't affect any of my datasource bindings by combing it into a single query will it?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #19

                  Joe Stansfield wrote:

                  It is working now - but is it worth the time to write it as it should be?

                  It's already as it should be; if you have a separate grid for the children, you'll need a second query. Also explains why the second grid might get an empty resultset.

                  Joe Stansfield wrote:

                  It won't affect any of my datasource bindings by combing it into a single query will it?

                  It would! The net effect is that you only pull the data over the network-line once. You're using a different concept here - if you'd fetch all tables, you'd probably have fetched a lot of child-records that the user isn't going to use. That means that your current approach might be the more efficient one, depending on what the user does :)

                  Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                  1 Reply Last reply
                  0
                  • J Joe Stansfield

                    Yea that will fix up the error from no value being entered. But I'm struggling to understand and make it work if, say, an input of 100 is entered. If 100 isn't a valid ID of a record, it won't return anything. I'd like it to return 1 for ease of avoiding an error and possible loop error (which could also be put down to poor programming by me on the C# side of things). With that said, if 99 is entered and that is a valid record, then 99 would need to be returned by the query. Sorry for not understanding your explanation.

                    R Offline
                    R Offline
                    Rohit Shrivastava
                    wrote on last edited by
                    #20

                    Hi Joe, You can use following query:

                    SELECT ISNULL( (SELECT File_Name
                    FROM Files
                    WHERE File_Name = @FileName1),1)

                    This would solve your problem, in case there is no result, it will return you 1 otherwise the matching result.

                    1 Reply Last reply
                    0
                    • J Joe Stansfield

                      Hi all, Firstly, I must confess to being a very amateur programmer with a lot to learn! However, I'm battling away and am slowly getting my head around things. I have a query I have written which as follows:

                      SELECT File_Name
                      WHERE (File_Name = @File_Name1)

                      File_Name1 is a user input parameter. My problem is that, even if the record does not exist, it returns the record, resulting in an error on the client side. Is it possible to edit that query to return a file_name field value of say, 1, if the searched record doesn't exist? Any help appreciated! (Also for note most of my queries are generated by VStudio, so my understanding of them leaves a lot to be desired...)

                      H Offline
                      H Offline
                      Hitesh R
                      wrote on last edited by
                      #21

                      Try the following query.

                      SELECT CASE WHEN COUNT(File_Name)=0 THEN 1 ELSE COUNT(File_Name) END as FileName from tblname
                      WHERE (File_Name = @File_Name1)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups