Can we pass parameters to a view in SQL?

前端 未结 20 2327
庸人自扰
庸人自扰 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条回答
  •  暖寄归人
    2020-11-29 18:47

    If you don't want to use a function, you can use something like this

    -- VIEW
    CREATE VIEW [dbo].[vwPharmacyProducts]
    AS
    SELECT     PharmacyId, ProductId
    FROM         dbo.Stock
    WHERE     (TotalQty > 0)
    
    -- Use of view inside a stored procedure
    CREATE PROCEDURE [dbo].[usp_GetProductByFilter]
    (   @pPharmacyId int ) AS
    
    IF @pPharmacyId = 0 BEGIN SET @pPharmacyId = NULL END
    
    SELECT  P.[ProductId], P.[strDisplayAs] FROM [Product] P
    WHERE (P.[bDeleted] = 0)
        AND (P.[ProductId] IN (Select vPP.ProductId From vwPharmacyProducts vPP
                               Where vPP.PharmacyId = @pPharmacyId)
                           OR @pPharmacyId IS NULL
            )
    

    Hope it will help

提交回复
热议问题