开发者

Can the Table Valued function that I wrote be turned into a View in Sql Server 2005

I am still so new to all this and I think I may have not done this the best way. I have a Table Valued function that I wrote, but I think that it could be written as a view.

The big catch as to why I used a table val function is that if the select query returns no results then I wanted to return a "default" row that showed empty values along with the timestamp and I didn't know how to do that in a view.

I betting the experts here know how to. Here's the function:

alter FUNCTION [dbo].[GetCurrentRTBindingConstraints]()
RETURNS 
    @CurrentBindingConstraints table (
     CONSTRAINTNAME [nvarchar] (120),
     MKTHOUR_EST [dateTime],
     MARGINALVALUE  [nvarchar] (20)
    )
AS
BEGIN
    INSERT INTO @CurrentBindingConstraints 
select * from 
OPENQUERY(UDS9, 'select
 CONSTRAINTNAME, MKTHOUR -(5/24) as MKTHOUR_EST,MARGINALVALUE 
from UDS9.MKTPLANCONSTRAINT mpc
where MARGINALVALUE != 0.00 and mpc.caseid=(SELECT caseid FROM uds9.MktCase
WHERE casestartinterval=(SELECT MAX(casestartinterval) FROM uds9.MktCase WHERE casestate=5 AND studymodeid=5)
AND casestate=5 AND studymodeid=5)')
    DECLARE @cnt INT
    SELECT @cnt = COUNT(*) FROM 开发者_StackOverflow@CurrentBindingConstraints  
    IF @cnt = 0
        INSERT INTO @CurrentBindingConstraints (
            [CONSTRAINTNAME],
            [MKTHOUR_EST],
            [MARGINALVALUE])
        VALUES ('None',dbo.RoundTime(dbo.GetGMTtoEST(getutcdate())),'None') 
    RETURN
END


You can use a common table expression (CTE) and a ranking function as follows:

;with Defaulted as (
    select 'none' as Col1,CURRENT_TIMESTAMP as Col2,'none' as Col3,1 as init -- This is your default row
    union all
    select name,DATEADD(day,-1,CURRENT_TIMESTAMP),name,0 from sys.objects -- This is where you query for real rows
), Ranked as (
    select Col1,Col2,Col3,RANK() OVER (ORDER BY init) as rnk from Defaulted
)
select * from Ranked where rnk = 1

The above is just an example - you'd need to replace the two selects inside the first CTE with your real queries, and should use column names rather than select *. It works because the ranking function (RANK()) is able to assess the result set as a whole.


Edit - trying with your actual queries:

create view CurrentRTBindingConstraints
as
;with Defaulted as (
    select CONSTRAINTNAME,MKTHOUR_EST,MARGINALVALUE,0 as init from 
OPENQUERY(UDS9, 'select
 CONSTRAINTNAME, MKTHOUR -(5/24) as MKTHOUR_EST,MARGINALVALUE 
from UDS9.MKTPLANCONSTRAINT mpc
where MARGINALVALUE != 0.00 and mpc.caseid=(SELECT caseid FROM uds9.MktCase
WHERE casestartinterval=(SELECT MAX(casestartinterval) FROM uds9.MktCase WHERE casestate=5 AND studymodeid=5)
AND casestate=5 AND studymodeid=5)')
    union all
    select 'None',dbo.RoundTime(dbo.GetGMTtoEST(getutcdate())),'None',1
), Ranked as (
    select CONSTRAINTNAME,MKTHOUR_EST,MARGINALVALUE,RANK() OVER (ORDER BY init) as rnk from Defaulted
)
select CONSTRAINTNAME,MKTHOUR_EST,MARGINALVALUE from Ranked where rnk = 1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜