1

我想就我的这个小问题寻求帮助。我在我的子查询上做一个查询和一个子查询,我想让它参数化。有没有办法做到这一点?请参阅我的查询脚本。

select sum(issue) as [Issue], sum(nonissue) as [NonIssue]
from
(
AS
    select
        case when isissue = 1 then 1 else 0 end as 'issue',
        case when isissue = 0 then 1 else 0 end as 'nonissue',
        LastTicketStatusID
    from
        vw_Tickets
    where
        LastTicketStatusID = @LastTicketStatusID
) 
as Tickets

我总是得到一个错误必须声明表变量“@LastTicketStatusID”。我应该在哪里声明参数?

谢谢, 诺伊蒂

4

3 回答 3

2

如果这是用于存储过程...

CREATE PROCEDURE [dbo].[ProcedureName]
    @LastTicketStatusID INT
AS 

select
sum(issue) as [Issue], 
sum(nonissue) as [NonIssue]
from (
    select
    case when isissue = 1
        then 1 else 0 end as 'issue',
    case when isissue = 0
        then 1 else 0 end as 'nonissue',
    LastTicketStatusID
from vw_Tickets 
where LastTicketStatusID = @LastTicketStatusID ) as Tickets

否则

DECLARE @LastTicketStatusID INT
SELECT @LastTicketStatusID = yourDesiredID
于 2009-07-08T16:07:24.110 回答
1

与您的问题无关,但假设 vw_Tickets.isissue 是一个位字段(或以其他方式限制为零或一的值)。可以删除内联查询以简化 Launchy 的答案:

select sum(isissue) as [Issue], 
    sum(1 - isissue) as [NonIssue]
from vw_Tickets 
where LastTicketStatusID = @LastTicketStatusID
于 2009-07-08T17:29:55.347 回答
0

在查询的最顶部

Declare @LastTicketStatusID int
set @lastTicketStatusID = ####
于 2009-07-08T16:01:07.757 回答