I am writing store procedure for searching by UserType,EmailId and by First name from tblUser
I written the following code but i am not getting aspected result please check the where clause and also inner join because when i am changing the where clause conditions its giving result but throwing sql msg==>StateName property is not found i did not get it please check both.
ALTER Procedure [dbo].[Usp_SearchUserDetails]
(
@UserId int=NULL,
@UserType nvarchar(50)=NULL,
@FirstName nvarchar(50)=NULL,
@EmailId nvarchar(50)=NULL,
@StateId nvarchar(10)=NULL,
@DistrictId nvarchar(10)=NULL,
@SqlMsg nvarchar(Max)output
)
As
BEGIN
SET @SqlMsg=''
DECLARE @strQuery nvarchar(max)
BEGIN TRY
SET @strQuery='ROW_NUMBER()Over(Order By U.AddDate Desc) as SerialNo,
U.UserType,
U.EmailId,
U.FirstName,
U.LastName,
U.PhoneNo,
U.MobileNo,
U.[Address],
U.PinCode,
U.Company,
S.StateName,
D.DistrictName,
[Password],
CONVERT(Varchar,U.AddDate,103) as AddDate
from tblUser as U
inner join tblState as S
On U.StateId=S.StateId
Inner Join tblDistrict as D
on U.DistrictId=D.DistrictId
Order by U.AddDate Desc
Where UserType=@UserType And EmailId=@EmailId'
IF(@StateId !='0' And @StateId IS Not Null)
Begin
SET @strQuery= @strQuery +' and U.StateId = '''+ @StateId +''' '
End
IF(@DistrictId != '0' And @DistrictId IS Not Null)
Begin
SET @strQuery=@strQuery + ' and U.DistrictId = ''' +@DistrictId+''' '
End
Begin
SET @strQuery=@strQuery + ' Order By U.UserId Desc'
end
print (@strQuery)
Exec(@strQuery)
END TRY
BEGIN CATCH
SET @SqlMsg=ERROR_MESSAGE()
END CATCH
END


Iftikar HussainRank #423 · 5.9K reputationPosted Aug 16, 2013, 1:12 AM EDT
Regards,
Iftikar
Posted Aug 14, 2013, 3:20 AM EDT
I hope the districts are associated with state table.
and S.StateId = D.StateId
Abhimanyu Singh104 reputationPosted Aug 14, 2013, 3:36 AM EDT
No, Actually Before applying of inner join code it was searching/Fetching some but it was showing Sql query msg==> StateName Property is not found so, then applied the above inner join code with help of goooled....but now its not working showing record not found please check the where condition because it wsa same befor apply the inner join code..may be it need to change...and i dont have concept of join..Please Help
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 14, 2013, 3:40 AM EDT
Is StateName is exist in tblState? Can you check?
If possible provide all there table schema.
Regards,
Iftikar
Abhimanyu Singh104 reputationPosted Aug 16, 2013, 1:07 AM EDT
tblState:
tblUser:
Rest thing(Search procedure) mentioned in asked question above....
Abhimanyu Singh104 reputationPosted Aug 16, 2013, 3:07 AM EDT
Its working almost with searching keywords Email_Id or with First Name Also but...Check the Where condition below...because While i am selecting UserType in Dropdown list its showing
Empty Grid.."No Record found" this is first keywords after this as i selected EmailId and clicked on Search button Its showing result now.
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 16, 2013, 6:23 AM EDT
Can you debug and check
Regards,
Iftikar