Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Everyone..
I am querying data in Sql Server and i have face an error i mean a very awkward response.
i have 200 Records in my table and the Table id is in between 1-200. Now the problem is when i enter the Id = 332 it will also show me record even i don't have any id 332

What I have tried:

Here is my Query
Select MOSQUEID,NAME,CITY,POSTCODE,PHONE from tbl_mosque Where MOSQUEID=332
Posted
Updated 29-Apr-17 7:41am
Comments
Dave Kreskowiak 29-Apr-17 13:53pm    
We can't see the rest of your code that is executing the query, nor the data in the table, so it's impossible to tell you what's going on here.

Open SQL Server Manager and verify that the ID number does NOT exist in the table.

Then run your code under the debugger, set a breakpoint at the start of your datbaase code that executes this query and single step through the code, examining the contents of variables to make sure they contain what you expect them to, and follow the logic.
Muhammd Aamir 29-Apr-17 13:59pm    
Thankx Dave got the actual Issue :)

1 solution

If this query:
SQL
Select MOSQUEID,NAME,CITY,POSTCODE,PHONE from tbl_mosque Where MOSQUEID=332
is returning any records, then you have one or more rows in your table which have a MosqueId of 332 - it will not invent such info!

So start by looking at the row(s) the query returns and see exactly what it is returning. Compare that to what is in the table (using SSMS to view the contents).
If the row doesn't exist, check your connection string and which table you are using.

It may be that you are using an IDENTITY column and assuming that having 200 entries implies that the IDENTITY values will always range from 1 to 200 - but SQL doesn't "reuse" values, so if you DELETE rows, and INSERT them again they will not get the same ID value.
 
Share this answer
 
Comments
Muhammd Aamir 29-Apr-17 13:45pm    
Thankx Sir, I am using IDENTITY Column so what i will have to do avoid this
OriginalGriff 29-Apr-17 13:59pm    
Don't - never try to mess with IDENTITY values, it causes more problems than it solves (and nasty ones to fix, as well).
Instead, use ROWNUMBER to return a sequence ordered by the IDENTITY column if you absolutely must have sequential numbers:

https://docs.microsoft.com/en-us/sql/t-sql/functions/row-number-transact-sql
Muhammd Aamir 29-Apr-17 14:11pm    
Thankx Sir OrigunalGriff got the solution
OriginalGriff 30-Apr-17 3:29am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900