简体   繁体   English

尝试查找特定值时,SQL查询返回多行

[英]SQL query returns multiple rows when trying to find specific value

I have 2 tables. 我有2张桌子。 One is called "Tasks" and the other one is called "TaskDescription" in my "Task" the setup looks like this: "taskID(primary)","FileID","TaskTypeID" and a bunch of other columns irrelevant. 在我的“任务”中,一个被称为“任务”,另一个被称为“ TaskDescription”,设置如下所示:“ taskID(primary)”,“ FileID”,“ TaskTypeID”和其他一系列无关的列。

Then in my "TaskDescription", the setup looks like: "TaskTypeID", "TaskTypeDesc" 然后在我的“ TaskDescription”中,设置如下:“ TaskTypeID”,“ TaskTypeDesc”

so for example if TaskTypeID is 1 , then the description would be"admin" or if TaskTypeID is 2, then TaskTypeDesc would be "Employee" etc. 因此,例如,如果TaskTypeID为1,则说明将为“ admin”;如果TaskTypeID为2,则TaskTypeDesc将为“ Employee”,依此类推。

The two tables have a relationship on the primary/foreign key "TaskTypeID". 这两个表在主键/外键“ TaskTypeID”上具有关系。

What I am trying to do is get a task id, and the TaskDesc where the FileID matches the @fileID(which I pass in as a param). 我想做的是获取一个任务ID,以及TaskDesc,其中FileID与@fileID(我作为参数传入)相匹配。 However in my query I get multiple rows returned instead of a single row when trying to obtain the description. 但是,在我的查询中,尝试获取描述时,我返回的是多行而不是单行。

this is my query: 这是我的查询:

SELECT taskid,
       ( 'Task ID: '
         + Cast(cf.taskid AS NVARCHAR(15)) + ' - '
         + Cast((SELECT DISTINCT td.tasktypedesc FROM casefiletaskdescriptions
         td JOIN
         casefiletasks cft ON td.tasktypeid=cft.tasktypeid WHERE cft.taskid =
         1841 )AS
         NVARCHAR(100))
         + ' - Investigator : ' + ( Cast(i.fname AS NVARCHAR(20)) + ' '
                                    + Cast(i.lname AS NVARCHAR(20)) ) ) AS
       'Display'
FROM   casefiletasks [cf]
       JOIN investigators i
         ON CF.taskasgnto = i.investigatorid
WHERE  cf.fileid = 2011630988
       AND cf.concluded = 0
       AND cf.progressflag != 'Conclude' 

I am trying to get the output to look like "Task ID: 1234 - Admin - Investigator : John Doe". 我正在尝试使输出看起来像“任务ID:1234-管理-调查员:约翰·多伊”。 However I am having trouble on this part: 但是我在这方面遇到了麻烦:

CAST((select DISTINCT td.TaskTypeDesc from CaseFileTaskDescriptions td 
JOIN CaseFileTasks cft ON td.TaskTypeID=cft.TaskTypeID 
where cft.TaskID =1841 )as nvarchar(100))

This seems to work but the problem is I have to hard code the value "1841" to make it work. 这似乎可行,但问题是我必须对值“ 1841”进行硬编码才能使其正常工作。 Is there a way to assign a "taskID" variable with the values being returned from the TaskID select query, or will it not work since I think sql runs everything at once instead of line by line. 有没有一种方法可以分配一个带有从TaskID选择查询返回的值的“ taskID”变量,否则它将不起作用,因为我认为sql一次运行所有内容,而不是逐行运行。

EDIT-this is in Microsoft SQL Server Management Studio 2008 编辑-这在Microsoft SQL Server Management Studio 2008中

You can dynamically reference a column that exists in your FROM set. 您可以动态引用FROM集中存在的列。 In this case, it would be any column from casefiletasks or investigators . 在这种情况下,它将是casefiletasksinvestigators任何列。 You would replace 1841 with the table.column reference. 您将用table.column引用替换1841

Update 更新

Replacing your static integer with the column reference, your query would look like: 用列引用替换您的静态整数,查询将类似于:

SELECT taskid,
       ( 'Task ID: '
         + Cast(cf.taskid AS NVARCHAR(15)) + ' - '
         + Cast((SELECT DISTINCT td.tasktypedesc FROM casefiletaskdescriptions
         td JOIN
         casefiletasks cft ON td.tasktypeid=cft.tasktypeid WHERE cft.taskid =
         cf.taskid )AS
         NVARCHAR(100))
         + ' - Investigator : ' + ( Cast(i.fname AS NVARCHAR(20)) + ' '
                                    + Cast(i.lname AS NVARCHAR(20)) ) ) AS
       'Display'
FROM   casefiletasks [cf]
       JOIN investigators i
         ON CF.taskasgnto = i.investigatorid
WHERE  cf.fileid = 2011630988
       AND cf.concluded = 0
       AND cf.progressflag != 'Conclude' 

Would this work as your inner query? 这可以作为您的内部查询吗?

SELECT DISTINCT td.TaskTypeDesc FROM CaseFileTaskDescriptions td 
JOIN CaseFileTasks cft ON td.TaskTypeID = cft.TaskTypeID 
WHERE cft.TaskID = cf.TaskID

Why not just do another join instead of a subquery? 为什么不只是做另一个联接而不是子查询呢?

SELECT taskid,
       ( 'Task ID: '
         + Cast(cf.taskid AS NVARCHAR(15)) + ' - '
         + Cast(td.tasktypedesc AS NVARCHAR(100))
         + ' - Investigator : ' + ( Cast(i.fname AS NVARCHAR(20)) + ' '
                                    + Cast(i.lname AS NVARCHAR(20)) ) ) AS
       'Display'
FROM   casefiletasks [cf]
       JOIN investigators i
         ON CF.taskasgnto = i.investigatorid
       JOIN casefiletaskdescriptions td
         ON td.tasktypeid = cf.tasktypeid
WHERE  cf.fileid = 2011630988
       AND cf.concluded = 0
       AND cf.progressflag != 'Conclude' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM