如何使sql存储过程返回两个或多个表的值

时间:2017-07-27 07:10:21

标签: sql stored-procedures

我有一个存储过程来检索顾问列表。 Advisers表具有外部键入Users.UserId表的“CreatedById”列。

CREATE PROCEDURE dbo.GetAdvisers
    -- Add the parameters for the stored procedure here
    @AdviserId int = 0
    ,@EmployeeNo nvarchar(100) = ''
    ,@LastName nvarchar(100) = ''
    ,@FirstName nvarchar(100) = ''
    ,@MiddleName nvarchar(100) = ''

AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT ISNULL(I.AdviserId,0) as AdviserId
      ,ISNULL(I.EmployeeNo, '') as EmployeeNo
      ,ISNULL(I.LastName, '') as LastName
      ,ISNULL(I.FirstName, '') as FirstName
      ,ISNULL(I.MiddleName, '') as MiddleName
      ,ISNULL(I.StatId, 0) as StatId
      ,ISNULL(I.CreatedById, 0) as CreatedById
      ,ISNULL(U.UserName, '') as CreatedByName
      ,ISNULL(I.CreatedOn, null) as CreatedOn

    FROM Advisers I

        LEFT JOIN Users U ON U.UserId = I.CreatedById

    WHERE (I.AdviserId = @AdviserId OR @AdviserId = 0)
      AND (I.EmployeeNo LIKE '%' + @EmployeeNo +'%' OR @EmployeeNo = '')     
      AND I.StatId <> 2 

END

GO

但在我的代码后面,User属性为null。如何使SQL返回用户?

这是我的Adviser类(从实体框架自动生成)

public partial class Adviser
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Adviser()
        {
            this.GradeSections = new HashSet<GradeSection>();
        }

        public int AdviserId { get; set; }
        public string EmployeeNo { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public Nullable<int> StatId { get; set; }
        public Nullable<int> CreatedById { get; set; }
        public Nullable<System.DateTime> CreatedOn { get; set; }

        public virtual User User { get; set; }
        public virtual Stat Stat { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<GradeSection> GradeSections { get; set; }
    }

这是我的顾问数据访问层:

public class AdviserDb 
    {
        public List<Adviser> GetList(AdviserP p, ref string msgLog)
        {
            ProcessLog processLog;
            var command = new SqlCommand();
            List<Adviser> items = new List<Adviser>();
            msgLog = "";
            try
            {
                command.CommandText = "GetAdvisers";
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@AdviserId", p.AdviserId).Direction = ParameterDirection.Input;
                command.Parameters.AddWithValue("@EmployeeNo", p.EmployeeNo).Direction = ParameterDirection.Input;
                command.Parameters.AddWithValue("@LastName", p.LastName).Direction = ParameterDirection.Input;
                command.Parameters.AddWithValue("@FirstName", p.FirstName).Direction = ParameterDirection.Input;
                command.Parameters.AddWithValue("@MiddleName", p.MiddleName).Direction = ParameterDirection.Input;

                DataTable dt = SqlHelper.GetData(command);

                if (dt.Rows.Count <= 0) return items;

                foreach (DataRow row in dt.Rows)
                {
                    Adviser item = new Adviser();
                    item.User = new User();

                    item.AdviserId = row["AdviserId"].GetInt();
                    item.EmployeeNo = row["EmployeeNo"].GetString();
                    item.LastName = row["LastName"].GetString();
                    item.FirstName = row["FirstName"].GetString();
                    item.MiddleName = row["MiddleName"].GetString();
                    item.StatId = row["StatId"].GetInt();
                    item.Stat.Name = row["StatName"].GetString(); // this is also null
                    item.CreatedById = row["CreatedById"].GetInt(); 
                    item.User.UserName = row["CreatedByName"].GetString(); // why is this null?
                    item.CreatedOn = row["CreatedOn"].GetDbDate();

                    items.Add(item);
                }
            }
            catch (SqlException s)
            {
                processLog = new ProcessLog(new SqlErrorLogger());
                msgLog = processLog.GenerateProcessLog(s.Message);
            }

            return items;
        }

    }
}

0 个答案:

没有答案