你可以加入存储过程吗?

时间:2022-09-21 00:09:02

I have a bunch of WITH statements:

我有一堆WITH语句:

;with OneAccession as (
        select client_id,COUNT(patient_id) PatientCount from
        (
            select client_id,patient_id
            from F_ACCESSION_DAILY
            group by CLIENT_ID,PATIENT_ID
            having COUNT(ACCESSION_ID)=1
        ) a
        group by CLIENT_ID
    )
    ,

    TwoAccessions as (
    select client_id,COUNT(patient_id) PatientCount from
        (
            select client_id,patient_id
            from F_ACCESSION_DAILY
            group by CLIENT_ID,PATIENT_ID
            having COUNT(ACCESSION_ID)=2
        ) a
    group by client_id
    )

    ,

    ThreeAccessions as (
    select client_id,COUNT(patient_id) PatientCount from
    (
        select client_id,patient_id
        from F_ACCESSION_DAILY
        group by CLIENT_ID,PATIENT_ID
        having COUNT(ACCESSION_ID)=3
    ) a
    group by client_id
    )
etc

And I join these statements on

我加入这些声明

select * from myTable
join OneAccession
on...
join TwoACcessions
on...
join ThreeAccessions

Instead of having all those with statements, can i just create a stored proc? I would just pass the value of having count(accession_id)=**@myParam** and do this:

我可以只创建一个存储过程,而不是拥有所有带语句的那些吗?我只是传递count(accession_id)= ** @ myParam **的值并执行此操作:

select * from myTable
join storedproc(1)
on...
join storedproc(2)
on...
etc...

Is there an issue on joining on a stored Proc? Is my methodology OK?

加入存储过程中是否存在问题?我的方法可以吗?

3 个解决方案

#1


1  

You can not join on a stored procedure, but you can join both a function and a view. Note that a view can not take parameters and that a function may not be as performant as the CTE.

您无法加入存储过程,但可以同时加入函数和视图。请注意,视图不能接受参数,并且函数可能不如CTE那样高效。

Also, looking at your query, it looks like you should look into the new windowing functions and that something like

此外,查看您的查询,看起来您应该查看新的窗口函数和类似的东西

;with cte as (
    select *, count(*) over (partition by client_id, patient_id) patientcount
    from f_accession_daily
)
select * from myTable
     inner join cte on ... and patientCount=1

might help with what you are trying to achieve.

可能有助于你想要实现的目标。

#2


3  

Have a look at APPLY. Using APPLY with table-valued functions seems to be the classic example for using APPLY, and I think it's what you want.

看看APPLY。使用具有表值函数的APPLY似乎是使用APPLY的经典示例,我认为这就是您想要的。

Have a look at this blog post with an example (using AdventureWorks):

看一下这个博客文章的例子(使用AdventureWorks):

select f.FirstName
      ,f.LastName
      ,f.JobTitle
      ,f.ContactType
      ,cc.CardNumber
from Sales.CreditCard cc
join Sales.ContactCreditCard ccc on cc.CreditCardID=ccc.CreditCardID
cross apply dbo.ufnGetContactInformation(ccc.ContactID) f
where cc.ExpYear=2008
  and cc.ExpMonth=6
  and cc.CardType='Vista'

#3


1  

No...you can do this using a table function instead though.

不...你可以使用表函数来做到这一点。

#1


1  

You can not join on a stored procedure, but you can join both a function and a view. Note that a view can not take parameters and that a function may not be as performant as the CTE.

您无法加入存储过程,但可以同时加入函数和视图。请注意,视图不能接受参数,并且函数可能不如CTE那样高效。

Also, looking at your query, it looks like you should look into the new windowing functions and that something like

此外,查看您的查询,看起来您应该查看新的窗口函数和类似的东西

;with cte as (
    select *, count(*) over (partition by client_id, patient_id) patientcount
    from f_accession_daily
)
select * from myTable
     inner join cte on ... and patientCount=1

might help with what you are trying to achieve.

可能有助于你想要实现的目标。

#2


3  

Have a look at APPLY. Using APPLY with table-valued functions seems to be the classic example for using APPLY, and I think it's what you want.

看看APPLY。使用具有表值函数的APPLY似乎是使用APPLY的经典示例,我认为这就是您想要的。

Have a look at this blog post with an example (using AdventureWorks):

看一下这个博客文章的例子(使用AdventureWorks):

select f.FirstName
      ,f.LastName
      ,f.JobTitle
      ,f.ContactType
      ,cc.CardNumber
from Sales.CreditCard cc
join Sales.ContactCreditCard ccc on cc.CreditCardID=ccc.CreditCardID
cross apply dbo.ufnGetContactInformation(ccc.ContactID) f
where cc.ExpYear=2008
  and cc.ExpMonth=6
  and cc.CardType='Vista'

#3


1  

No...you can do this using a table function instead though.

不...你可以使用表函数来做到这一点。