SQL Select Where query
-
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!
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.
-
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.
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.
-
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!
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.IdThis 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[^]
-
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...)
Hi, U can use following statment..
SELECT ISNULL(File_Name, '')
FROM Files
WHERE File_Name = @FileName1because is is string so it should return blank
-
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.
ITS BETTER U YO USE THE IMMEDIATE CONSTRAINT DURING THE CODING
-
ITS BETTER U YO USE THE IMMEDIATE CONSTRAINT DURING THE CODING
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
-
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.IdThis 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[^]
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?
-
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?
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[^]
-
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.
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.
-
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...)