基于SQL Query将值添加到下拉列表

时间:2016-04-16 08:35:16

标签: sql asp.net sql-server-2005 drop-down-menu

我有dropdownlist,其值由

的查询绑定
select emp_card_no, 
       emp_name + '-' 
                + cast(emp_card_no as varchar) 
                + '(' 
                + datename(MM,a.dt_of_leave) 
                + ' - ' 
                + cast(year(a.dt_of_leave)as varchar)
                +')' emp_name 
from emp_mst a 
where month(a.dt_of_leave) >= month(getdate())-1  
  and year(a.dt_of_leave) = 
        case when month(getdate())=1 then year(getdate())-1 
             else year(getdate()) 
        end
order by emp_name 

其结果看起来像这样。

[![SQL输出] [1]] [1]

现在我想要的是,在Emp_name列中,我希望在PROCESS之后根据下面的查询添加文本PENDING(April - 2016)

select emp_mkey, * from emp_mon_day
where emp_mkey = 312
 and month = 4
 and year = 2016

如果查询返回任何结果,则PROCESS否则PENDING

注意

第一个查询列Emp_card_noEmp_mkey表中的emp_mon_day

另请参阅绑定下拉列表的代码

protected void funfillEmployee()
{
    DataTable DtCombo = new DataTable();
    string strdate = System.DateTime.Now.ToString("dd/MM/yyyy");

    DtCombo = ObjPriDal.ExecuteDataTable("select emp_card_no, emp_name + '-' + cast(emp_card_no as varchar)+ '(' + datename(MM,a.dt_of_leave)  + ' - ' + cast(year(a.dt_of_leave)as varchar)+')' emp_name  " +
                         "  from emp_mst a where month(a.dt_of_leave) >= month(getdate())-1  and  year(a.dt_of_leave)= case " +
                         "   when  month(getdate())=1 then year(getdate())-1 else year(getdate()) end order by emp_name ");

    cmbEmp_Name.DataTextField = "emp_name";
    cmbEmp_Name.DataValueField = "emp_card_no";
    cmbEmp_Name.DataSource = DtCombo;
    cmbEmp_Name.DataBind();
    cmbEmp_Name.Items.Insert(0, new ListItem("--Select--", "0"));
    DtCombo.Clear();
}

让我知道如何做到这一点。

我正在使用SQL-server-2005

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

select a.emp_card_no, 
       a.emp_name + '-' 
                  + cast(a.emp_card_no as varchar) 
                  + '(' 
                  + datename(MM,a.dt_of_leave) 
                  + ' - ' 
                  + cast(year(a.dt_of_leave)as varchar)
                  +') '
                  +                
                    case when m.emp_mkey IS NULL 
                         then 'PENDING' 
                         else 'PROCESS' 
                    end                  
                  emp_name 
from emp_mst a 
LEFT JOIN emp_mon_day m ON m.Emp_mkey = a.Emp_card_no 
                        AND m.month = month(a.dt_of_leave) 
                        AND  m.year = year(a.dt_of_leave)
where month(a.dt_of_leave) >= month(getdate())-1  
  and year(a.dt_of_leave) = 
        case when month(getdate())=1 then year(getdate())-1 
             else year(getdate()) 
        end
order by emp_name 

但你可能已经适应了那部分

AND m.month = month(a.dt_of_leave) AND  m.year = year(a.dt_of_leave)

因为月份和年份是保留字,我怀疑你是否这样命名你的列,但你没有指定其他名称