You must Sign In to post a response.
  • Category: SQL Server

    How get assing farmer according user login

    Hi,

    I a trying to display inforation according to user passsing id. @USER_ID is my user id.
    If i passed @USER_ID = 1 then display all record of farmer is user has not assin
    the farmer because user id 1 is admin. and if passed user id 8 or 9 or other no. then it user id
    which has assign in assign table.
    My query is not getting proper result.


    FARMER_MASTER (TABLE)
    ID NAME
    13 Farmer 02
    14 Farmer 03
    15 Farmer 04
    16 Farmer 05
    17 Farmer 06
    18 FARMER
    19 FARMER TEST


    ASSIGN_FARMER (TABLE)

    FARMER_ID ASSIGNED_TO
    13 8
    14 8
    15 9
    16 9
    18 9
    17 8


    Declare @USER_ID int
    Set @USER_ID = 18

    Select FM.ID, FM.NAME From FARMER_MASTER FM
    Left Outer Join ASSIGN_FARMER ANM On ANM.FARMER_ID = FM.ID
    And ANM.ASSIGNED_TO = (Case When @USER_ID = 1 Then ANM.ASSIGNED_TO Else @USER_ID End)
  • #767516
    Hi,

    If you want to get data based on Farmer Id then why are you using Left outer join?

    Do you know what is the use of Left Outer Join?

    It's fetches match records in both the tables as well as all the rows in left table respective column in right table filed with null values.

    But in your case you need not all the entries you want only matching records.

    So, you just use Simple join and filter the records as you want.


    select a.col1,b.col2
    from table1 a,
    table2 b
    where a.id=b.id


    This is for example I gave you, you can refer that and implement in your case.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments