I am facing some strange issue and want to understand the reason behind this.
We have two database servers Say A and B. On both of these servers we have our application database (Same schema but different records)
Problem : We have a SqlQuery
Select * from Person where memberId=123456
This query runs perfectly and return the rows selected on server - A. But the same query on a different server-B doesnt return any records.
But if i modify my query to
Select * from Person where memberId='123456'
( notice the single quotes)
Now it returns me proper records.
DataType of memberId is nchar(100) . Technically i understand that i should compare it using the single quotes.
But just want to understand why is this happening??
Update : 1) Both have exactly the same schema. 2) Both have same records
Actual Code :
Actually this query is a dynamically created and then executed using
declare @sql varchar(2000)
set @sql = 'SELECT * FROM PersonTrivia where memberId='+ @MemberId
print @sql
exec (@sql)
and this parameter @MemberId is varchar(250)
nchar(100)to store an int?nchartype in SQL Server 2005? In from MSDN, looks like it is not msdn.microsoft.com/tr-tr/library/ms182673%28v=sql.100%29.aspxncharcolumn to anintliteral the column should get implicitly cast to anint, can't think of a way that these results can happen.