0

-> 我想编写一个存储过程,它从 2 个选择语句中输出列

-> 一个 select 语句的列将是第二个 select 语句的输入

create procedure sp_proc
@inputId int
As 
Begin 

Select t1.ticketId AS ticket, 
t2.name AS NAME,
t3.status AS STATUS, 
t2.id ID
from table1 t1
Inner JOIN table2 t2 ON  t1.ticketId= t2.ticketId
INNER JOIN table3 t3 on t3.name = t2.name
WHERE t1.ticketId IS NOT NULL
AND t2.id = @inputId 


Select t1.ticketId, t2.name , D.desc, t2.id
from table1 t1
Inner JOIN table2 t2 ON  t1.ticketId= t2.ticketId
INNER JOIN( SELECt TOP 1 from table4 t4 ORDER BY t4.date  where t4.ticketId = ticket)AS D
ON t4.ticketId = t2.ticketId
WHERE t4.ticketId =ticket

我想要ticket, NAME,STATUS, ID , desc用户何时提供@inputIdproc

4

3 回答 3

0

我可以通过单个查询来实现它。

创建过程 sp_proc @inputId int As

select t1.ticketId AS ticket, t2.name AS NAME, t3.status AS STATUS, t2.id ID, t4Description.[desc] from table1 t1

内部 JOIN table2 t2 ON t1.ticketId= t2.ticketId

INNER JOIN table3 t3 on t3.name = t2.name

外用(

              SELECT TOP 1 [desc]
              FROM table4 t4
              WHERE t4.ticketId = t1.ticketId
              ORDER BY t4.[date] DESC

        ) t4Description

其中 t1.ticketId 不为空且 t2.id = @inputId

于 2013-05-05T22:20:08.607 回答
0

我会尝试将所有内容合并到一个查询中。这是我的猜测,因为您似乎错过了一些东西:

SELECT
  t1.ticketId AS ticket, 
  t2.name AS NAME,
  t3.status AS STATUS, 
  t2.id AS ID,
  (SELECT TOP 1 desc FROM table4 WHERE t1.ticketId=table4.ticketId ORDER BY DESC) AS DESC
FROM table1 t1
INNER JOIN table2 t2 ON  t1.ticketId= t2.ticketId
INNER JOIN table2 t4 ON  t1.ticketId= t4.ticketId
INNER JOIN table3 t3 ON t3.name = t2.name

WHERE t1.ticketId IS NOT NULL AND t2.id = @inputId

但我不确定你想去哪里:

INNER JOIN( SELECt TOP 1 from table4 t4 ORDER BY t4.date  where t4.ticketId = ticket)AS desc
ON t4.ticketId = t2.ticketId

我猜你是在 table4 中为给定的ticketId 寻找最新的东西......所以我根据这个假设进行了查询。

于 2013-05-02T11:30:52.080 回答
0

内联标量查询将起作用

create procedure sp_proc
@inputId int
As 

Select t1.ticketId AS ticket, 
t2.name AS NAME,
t3.status AS STATUS, 
t2.id ID,
[DESC] = (SELECT TOP(1) [DESC]
          from table4 t4
          where t4.ticketId = t1.ticketId
          ORDER BY t4.date)
from table1 t1
Inner JOIN table2 t2 ON  t1.ticketId= t2.ticketId
INNER JOIN table3 t3 on t3.name = t2.name
WHERE t1.ticketId IS NOT NULL
AND t2.id = @inputId;
GO
于 2013-05-02T11:31:43.973 回答