下面是一个代码片段,用于将表作为参数传递给可以在Sql Server 2008中使用的查询。我对“SELECT id.custid FROM @custids id”感到困惑。为什么它使用id.custid和@custids id ...?
private static void datatable_example()
{
string [] custids = {"ALFKI", "BONAP", "CACTU", "FRANK"};
DataTable custid_list = new DataTable();
custid_list.Columns.Add("custid", typeof(String));
foreach (string custid in custids) {
DataRow dr = custid_list.NewRow();
dr["custid"] = custid;
custid_list.Rows.Add(dr);
}
using(SqlConnection cn = setup_connection())
{
using(SqlCommand cmd = cn.CreateCommand())
{
cmd.CommandText =
@"SELECT C.CustomerID, C.CompanyName
FROM Northwind.dbo.Customers C
WHERE C.CustomerID IN (SELECT id.custid FROM @custids id)";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@custids", SqlDbType.Structured);
cmd.Parameters["@custids"].Direction = ParameterDirection.Input;
cmd.Parameters["@custids"].TypeName = "custid_list_tbltype";
cmd.Parameters["@custids"].Value = custid_list;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
using (DataSet ds = new DataSet()) {
da.Fill(ds);
PrintDataSet(ds);
}
}
}
答案 0 :(得分:3)
'id'只是一个别名。
与主要FROM子句中的“C”作为别名并使用“C.CustomerID”的方式相同。
子查询也可以
SELECT custid
FROM @custids