我们可以将参数传递给Microsoft SQL Server中的视图吗?
我尝试create view
通过以下方式进行操作,但不起作用:
create or replace view v_emp(eno number) as select * from emp where emp_id=&eno;
Parameters are out of the discussion
过于大胆的声明。反例
我们可以将参数传递给Microsoft SQL Server中的视图吗?
我尝试create view
通过以下方式进行操作,但不起作用:
create or replace view v_emp(eno number) as select * from emp where emp_id=&eno;
Parameters are out of the discussion
过于大胆的声明。反例
Answers:
如前所述,您不能。
一种可能的解决方案是实现存储的功能,例如:
CREATE FUNCTION v_emp (@pintEno INT)
RETURNS TABLE
AS
RETURN
SELECT * FROM emp WHERE emp_id=@pintEno;
这使您可以通过以下方式将其用作普通视图:
SELECT * FROM v_emp(10)
不幸的是,有两种方法可以使用视图来实现。
您可以创建一个表值用户定义的函数,该函数采用所需的参数并返回查询结果
或者,您可以做几乎相同的事情,但是创建一个存储过程而不是用户定义的函数。
例如
存储过程看起来像
CREATE PROCEDURE s_emp
(
@enoNumber INT
)
AS
SELECT
*
FROM
emp
WHERE
emp_id=@enoNumber
否则用户定义的函数看起来像
CREATE FUNCTION u_emp
(
@enoNumber INT
)
RETURNS TABLE
AS
RETURN
(
SELECT
*
FROM
emp
WHERE
emp_id=@enoNumber
)
通常,视图未参数化。但是您总是可以注入一些参数。例如,使用会话上下文:
CREATE VIEW my_view
AS
SELECT *
FROM tab
WHERE num = SESSION_CONTEXT(N'my_num');
调用:
EXEC sp_set_session_context 'my_num', 1;
SELECT * FROM my_view;
还有一个:
EXEC sp_set_session_context 'my_num', 2;
SELECT * FROM my_view;
对于Oracle同样适用(当然,上下文函数的语法是不同的)。
为什么需要视图中的参数?您可能只使用WHERE
子句。
create view v_emp as select * from emp ;
和您的查询应该做的工作:
select * from v_emp where emp_id=&eno;
WHERE
对于表而不是WHERE
视图,将有很大的性能改进。
一种不使用存储过程或函数的简单方法是在数据库中创建一个包含ID,Param1,Param2等列的设置表。在该表中插入一行,其中包含值Id = 1,Param1 = 0,Param2 = 0,依此类推。然后,您可以在视图中向该表添加联接以创建所需的效果,并在运行视图之前更新设置表。如果有多个用户更新设置表并同时运行视图,则可能会出错,但否则应该可以。就像是:
CREATE VIEW v_emp
AS
SELECT *
FROM emp E
INNER JOIN settings S
ON S.Id = 1 AND E.emp_id = S.Param1
没有。如果必须然后使用可以将参数传递到的用户定义函数。
我们可以编写一个具有输入参数的存储过程,然后使用该存储过程从视图中获取结果集。请参阅下面的示例。
存储过程是
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
从中我们可以得到结果集的视图是
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%')
据我所知,视图可以像选择命令一样。您还可以在以下语句中向此选择添加参数,例如:
WHERE (exam_id = @var)
如果您不想使用函数,可以使用类似这样的东西
-- 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
)
希望对你有帮助
您的视图可以引用一些包含您的参数的外部表。
就像其他人提到的那样,SQL Server中的视图不能具有外部输入参数。但是,您可以使用CTE在视图中轻松伪造变量。您可以在您的SQL Server版本中对其进行测试运行。
CREATE VIEW vwImportant_Users AS
WITH params AS (
SELECT
varType='%Admin%',
varMinStatus=1)
SELECT status, name
FROM sys.sysusers, params
WHERE status > varMinStatus OR name LIKE varType
SELECT * FROM vwImportant_Users
产生输出:
status name
12 dbo
0 db_accessadmin
0 db_securityadmin
0 db_ddladmin
也通过 JOIN
WITH params AS ( SELECT varType='%Admin%', varMinStatus=1)
SELECT status, name
FROM sys.sysusers INNER JOIN params ON 1=1
WHERE status > varMinStatus OR name LIKE varType
也通过 CROSS APPLY
WITH params AS ( SELECT varType='%Admin%', varMinStatus=1)
SELECT status, name
FROM sys.sysusers CROSS APPLY params
WHERE status > varMinStatus OR name LIKE varType
我有一个尚未尝试的想法。你可以做:
CREATE VIEW updated_customers AS
SELECT * FROM customer as aa
LEFT JOIN customer_rec as bb
ON aa.id = bb.customer_id
WHERE aa.updated_at between (SELECT start_date FROM config WHERE active = 1)
and (SELECT end_date FROM config WHERE active = 1)
您的参数将保存并在Config表中更改。
我根据我的需要实现了以下任务
set nocount on;
declare @ToDate date = dateadd(month,datediff(month,0,getdate())-1,0)
declare @year varchar(4) = year(@ToDate)
declare @month varchar(2) = month(@ToDate)
declare @sql nvarchar(max)
set @sql = N'
create or alter view dbo.wTempLogs
as
select * from dbo.y2019
where
year(LogDate) = ''_year_''
and
month(LogDate) = ''_month_'' '
select @sql = replace(replace(@sql,'_year_',@year),'_month_',@month)
execute sp_executesql @sql
declare @errmsg nvarchar(max)
set @errMsg = @sql
raiserror (@errMsg, 0,1) with nowait