Can we pass parameters to a view in SQL?

前端 未结 20 2376
庸人自扰
庸人自扰 2020-11-29 18:29

Can we pass a parameter to a view in Microsoft SQL Server?

I tried to create view in the following way, but it doesn\'t work:

create o         


        
20条回答
  •  旧时难觅i
    2020-11-29 18:42

    we can write a stored procedure with input parameters and then use that stored procedure to get a result set from the view. see example below.

    the stored procedure is

    CREATE PROCEDURE [dbo].[sp_Report_LoginSuccess] -- [sp_Report_LoginSuccess] '01/01/2010','01/30/2010'
    @fromDate datetime,
    @toDate datetime,
    @RoleName varchar(50),
    @Success int
    as
    If @RoleName != 'All'
    Begin
       If @Success!=2
       Begin
       --fetch based on true or false
      Select * from vw_Report_LoginSuccess
      where logindatetime between  dbo.DateFloor(@fromDate) and dbo.DateSieling(@toDate)
      And RTrim(Upper(RoleName)) = RTrim(Upper(@RoleName)) and Success=@Success
       End
       Else
       Begin
        -- fetch all
      Select * from vw_Report_LoginSuccess
      where logindatetime between  dbo.DateFloor(@fromDate) and dbo.DateSieling(@toDate)
      And RTrim(Upper(RoleName)) = RTrim(Upper(@RoleName))
       End
    
    End
    Else
    Begin
       If @Success!=2
       Begin
      Select * from vw_Report_LoginSuccess
      where logindatetime between  dbo.DateFloor(@fromDate) and dbo.DateSieling(@toDate)
      and Success=@Success
     End
     Else
     Begin
      Select * from vw_Report_LoginSuccess
      where logindatetime between  dbo.DateFloor(@fromDate) and dbo.DateSieling(@toDate)
     End
    
    End
    

    and the view from which we can get the result set is

    CREATE VIEW [dbo].[vw_Report_LoginSuccess]
    AS
    SELECT     '3' AS UserDetailID, dbo.tblLoginStatusDetail.Success, CONVERT(varchar, dbo.tblLoginStatusDetail.LoginDateTime, 101) AS LoginDateTime,
                          CONVERT(varchar, dbo.tblLoginStatusDetail.LogoutDateTime, 101) AS LogoutDateTime, dbo.tblLoginStatusDetail.TokenID,
                          dbo.tblUserDetail.SubscriberID, dbo.aspnet_Roles.RoleId, dbo.aspnet_Roles.RoleName
    FROM         dbo.tblLoginStatusDetail INNER JOIN
                          dbo.tblUserDetail ON dbo.tblLoginStatusDetail.UserDetailID = dbo.tblUserDetail.UserDetailID INNER JOIN
                          dbo.aspnet_UsersInRoles ON dbo.tblUserDetail.UserID = dbo.aspnet_UsersInRoles.UserId INNER JOIN
                          dbo.aspnet_Roles ON dbo.aspnet_UsersInRoles.RoleId = dbo.aspnet_Roles.RoleId
    WHERE     (dbo.tblLoginStatusDetail.Success = 0)
    UNION all
    SELECT     dbo.tblLoginStatusDetail.UserDetailID, dbo.tblLoginStatusDetail.Success, CONVERT(varchar, dbo.tblLoginStatusDetail.LoginDateTime, 101)
                          AS LoginDateTime, CONVERT(varchar, dbo.tblLoginStatusDetail.LogoutDateTime, 101) AS LogoutDateTime, dbo.tblLoginStatusDetail.TokenID,
                          dbo.tblUserDetail.SubscriberID, dbo.aspnet_Roles.RoleId, dbo.aspnet_Roles.RoleName
    FROM         dbo.tblLoginStatusDetail INNER JOIN
                          dbo.tblUserDetail ON dbo.tblLoginStatusDetail.UserDetailID = dbo.tblUserDetail.UserDetailID INNER JOIN
                          dbo.aspnet_UsersInRoles ON dbo.tblUserDetail.UserID = dbo.aspnet_UsersInRoles.UserId INNER JOIN
                          dbo.aspnet_Roles ON dbo.aspnet_UsersInRoles.RoleId = dbo.aspnet_Roles.RoleId
    WHERE     (dbo.tblLoginStatusDetail.Success = 1) AND (dbo.tblUserDetail.SubscriberID LIKE N'P%')  
    

提交回复
热议问题