SUB SELECT QUERY WITH INNER JOIN给出意想不到的结果

时间:2016-03-05 17:21:52

标签: sql sql-server subquery inner-join select-query

这是我在存储过程中的查询

DECLARE @branch uniqueidentifier

SELECT @branch = Branch_Id 
FROM Student_Master 
WHERE Student_Id = @studentid

SELECT
    CompanyMaster.unique_id_company, 
    Job_College.College_Id,
    Job_Branch.Branch_Id,
    CompanyMaster.name,
    Job_Master.Job_Post_Date, Job_Master.Job_Title,
    Job_Master.Salary, Job_Master.Job_Location,
    Department_Master.Department_Name 
FROM
    Job_Master
INNER JOIN
    Company_Master ON Job_Master.Company_ID = CompanyMaster.unique_id_company 
INNER JOIN
    Department_Master ON Department_Master.Department_Id = Job_Master.Department_Id 
INNER JOIN
    Job_College ON Job_Master.Job_ID = Job_College.Job_Id 
INNER JOIN
    Job_Branch ON Job_Branch.Job_College_ID = Job_College.Job_College_ID
WHERE
    Job_Branch.Branch_Id = @branch

逻辑查询是正确的,并且如果我将@branch的值直接传递给它(而不是在开头使用SELECT查询),则产生预期的结果但是通过填充值的代码显示此方法@branch在开头使用选择查询时,此查询会产生0结果,尽管@branch正在从中获取值。

我尝试过的事情

  1. 直接在WHERE子句
  2. 中使用SELECT查询
  3. 我已经看到PRINT @branch给出正确的输出所以它应该在WHERE子句中起作用但是它不起作用
  4. 请帮忙!!我被困了好几个小时

0 个答案:

没有答案