简体   繁体   English

如何用视图创建存储过程?

[英]How to create stored procedure with view?

I tried to execute this code and it worked ok without the the stored procedure and with it, it made an error. 我试图执行此代码,但没有存储过程,也可以正常工作,并且出错了。

the error is: Msg 102, Level 15, State 1, Procedure UV_MTBF, Line 251 Incorrect syntax near 'Event_'. 错误是:消息102,级别15,状态1,过程UV_MTBF,第251行'Event_'附近的语法不正确。

Is the stored procedure have a limitation in length? 存储过程的长度是否有限制? can someone help me with my code? 有人可以帮助我处理我的代码吗?

*****edit****** my problem is with ' + QUOTENAME(@category,N'''') + N' i want to add an integer from a variable that i received in the stored procedure. *****编辑******我的问题是'+ QUOTENAME(@ category,N'''')+ N'我想从存储过程中收到的变量中添加一个整数。 how can i do it? 我该怎么做?

enter code here: 在这里输入代码:

 CREATE PROCEDURE dbo.MTBFCalculation @Category int, @Action bit, @relateToParent bit
    as 

    IF EXISTS (SELECT 1 FROM sys.objects WHERE [name] = '[dbo].[UV_MTBF]')
            DROP VIEW [dbo].[UV_MTBF];

        DECLARE @Event nvarchar(MAX) = N'

    CREATE VIEW [dbo].[UV_MTBF]
    as
    with failureReportTable as (SELECT [ID] as failure_id
                                      ,[Login_ID]
                                      ,[Event_ID]
                                      ,[StartDate]
                                      ,[EndDate]
                                      ,DATEDIFF(Hour,[StartDate],[EndDate]) as eventDurationMin
                                      ,[IsRelevantForBI]
                                      ,[IsParallelReport]
                                      ,[ParentReportID]
                                      ,[IsPausedEvent]
                                      ,Case 
                                            When ParentReportID>0 Then 1       --Chiled
                                            When IsParallelReport=1 Then 2     --Parent
                                            Else 3                             --not Parallel
                                            End as ParallelStatus
                                  FROM [TDM_Analysis].[dbo].[FailureReports]),

            fullFailure as (select *, ROW_NUMBER() OVER (ORDER BY [StartDate] ) AS IDrow
                            from failureReportTable join [TDM_Analysis].[dbo].[UV_filteredLogins] as viewLogins on failureReportTable.Login_ID=viewLogins.ID
                            WHERE event_id IN (SELECT ID  FROM [TDM_Analysis].[dbo].[Events] where EventCategory_ID=' + QUOTENAME(@category,N'''') + N')
                                and (ParallelStatus=3 or ParallelStatus=(case when ' + QUOTENAME(@relateToParent,N'''') + N'=1 then 2 else 1 end))),


    --------------create first failure table------------------

            failure_Event_1 as (select f1.failure_id as Event_1_Failure_ID
                                      ,f1.[Login_ID] as Event_1_Login_ID
                                      ,f1.[Event_ID] as Event_1_Event_ID
                                      ,f1.[StartDate] as Event_1_StartDate
                                      ,f1.[EndDate] as Event_1_EndDate
                                      ,f1.eventDurationMin as Event_1_eventDurationMin
                                      --,f1.[IsRelevantForBI] as Event_1_IsRelevantForBI
                                      --,f1.[IsParallelReport] as Event_1_IsParallelReport
                                     -- ,f1.[ParentReportID] as Event_1_ParentReportID
                                     -- ,f1.[IsPausedEvent] as Event_1_IsPausedEvent
                                      ,f1.[Test_Name] as Event_1_TestName
                                      ,f1.Phase_Name as Event_1_PhaseName
                                      ,f1.PressName as Event_1_PressName
                                      ,f1.PressType as Event_1_PressType
                                      --,f1.[Operator] as Event_1_Operator
                                      ,f1.[LoginDate] as Event_1_LoginDate
                                      ,f1.[LogoutDate] as Event_1_LogoutDate
                                      ,f1.TimeDiff as Event_1_LoginDuration
                                      ,f1.IDrow+1 as row1
                                      from fullFailure as f1),

    --------------create second failure table------------------

             failure_Event_2 as (select f1.failure_id as Event_2_Failure_ID
                                      ,f1.[Login_ID] as Event_2_Login_ID
                                      ,f1.[Event_ID] as Event_2_Event_ID
                                      ,f1.[StartDate] as Event_2_StartDate
                                      ,f1.[EndDate] as Event_2_EndDate
                                      ,f1.eventDurationMin as Event_2_eventDurationMin
                                    --  ,f1.[IsRelevantForBI] as Event_2_IsRelevantForBI
                                     -- ,f1.[IsParallelReport] as Event_2_IsParallelReport
                                     -- ,f1.[ParentReportID] as Event_2_ParentReportID
                                     -- ,f1.[IsPausedEvent] as Event_2_IsPausedEvent
                                      ,f1.[Test_Name] as Event_2_TestName
                                      ,f1.Phase_Name as Event_2_PhaseName
                                      ,f1.PressName as Event_2_PressName
                                      ,f1.PressType as Event_2_PressType
                                    --  ,f1.[Operator] as Event_2_Operator
                                      ,f1.[LoginDate] as Event_2_LoginDate
                                      ,f1.[LogoutDate] as Event_2_LogoutDate
                                      ,f1.TimeDiff as Event_2_LoginDuration
                                      ,f1.IDrow as row2
                                      from fullFailure as f1),

    ------------- join two failure tabels and calculating MTTR-mean time to repair (duration of failue), MTTF-mean time to failue( end of one until start of a new one), MTBF-mean time between failue (from start of a failure to start of a new one)--------------------

            joinFailures as (select *, Event_1_eventDurationMin as MTTR
                                      ,CASE
                                            When isnull(f2.row2,0)=0 then DATEDIFF(HOUR,f1.Event_1_EndDate,f1.Event_1_LogoutDate) 
                                            WHEN f1.Event_1_Login_ID=f2.Event_2_Login_ID THEN DATEDIFF(HOUR,f1.Event_1_EndDate,f2.Event_2_StartDate) 
                                            When (select TOP 1 sum(timediff) 
                                                            from [TDM_Analysis].[dbo].[UV_filteredLogins]
                                                            where logindate>f1.Event_1_LogoutDate and logindate<f2.Event_2_LoginDate) is null then DATEDIFF(HOUR,f1.Event_1_EndDate,f1.Event_1_LogoutDate)+DATEDIFF(HOUR,f2.Event_2_LoginDate, f2.Event_2_StartDate) 
                                            ELSE 
                                                     (select TOP 1 sum(timediff)+DATEDIFF(HOUR,f1.Event_1_EndDate,f1.Event_1_LogoutDate)+DATEDIFF(HOUR,f2.Event_2_LoginDate, f2.Event_2_StartDate)
                                                            from [TDM_Analysis].[dbo].[UV_filteredLogins]
                                                            where logindate>f1.Event_1_LogoutDate and logindate<f2.Event_2_LoginDate)

                                        END AS MTTF
                            from failure_Event_1 as f1 left join failure_Event_2 as f2 on f1.row1=f2.row2),

            positiveJoinFailure as (select * from joinFailures where MTTF>=0)


    ---- select calculation table order by ascending time----------

    select * --Event_1_Failure_ID,Event_2_Failure_ID,MTTR,MTTF, MTTR+MTTF as MTFB
    from positiveJoinFailure
    --order by row1     
    ';

    --------------------------------------------------------Action------------------------------------------------------------------------------


    if @Action=1
    begin
    EXEC sp_executesql @Event;
    end

for this part of your query 对于您的查询的这一部分

where EventCategory_ID=' + QUOTENAME(@category,N'''') + N')

2 option here, you convert the value of @category to string and then concatenate with the dynamic query 2选项,您将@category的值转换为字符串,然后与动态查询连接

where EventCategory_ID=' + convert(varchar(10), @category)

OR, you pass the value in as a parameter. 或者,您将值作为参数传递。

for this option, you specify @category in the dynamic query 对于此选项,请在动态查询中指定@category

where EventCategory_ID= @category
and (ParallelStatus=3 ....

and you pass the value in at sp_executesql 然后在sp_executesql传递值

EXEC sp_executesql @Event, N'@category int', @category

By the way, Option 2 is the preferred method when using dynamic query 顺便说一句,使用动态查询时,选项2是首选方法

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

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