替换视图定义中的文本
Replace text from VIEW Definition
我有以下VIEW
定义:
create view v_1
as
select * from t1
where [date] between '2010-01-01' and '2019-01-01'
union all
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
union all
select * from t4
where [date] between '2014-01-01' and '2019-01-01'
场景 1:想要删除(或替换为空字符串)table t1
select 语句。
视图应如下所示:
create view v_1
as
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
union all
select * from t4
where [date] between '2014-01-01' and '2019-01-01'
场景 2:想要删除 table t4
select 语句。
视图应如下所示:
create view v_1
as
select * from t1
where [date] between '2010-01-01' and '2019-01-01'
union all
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
注意:根据要求可能需要从 VIEW 定义中删除任何 select 语句。
SQL 服务器使您能够在编辑器中使用正则表达式作为搜索模式,因此您需要做的就是:
按 Ctrl+F
启用正则表达式
作为搜索模式使用 select.+from t1(.*\n)+?select
,其中 t1
可以替换为任何 table 名称
作为替换模式使用 select
见下图(替换后):
创建内联 table 函数而不是视图,也称为带参数的视图。例如:
create function v_1
(
@HasQuery1 BIT
,@HasQuery2 BIT
,@HasQuery3 BIT
,@HasQuery4 BIT
)
RETURNS TABLE
as
RETURN
(
select * from t1
where [date] between '2010-01-01' and '2019-01-01'
AND @HasQuery1 = 1
union all
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
AND @HasQuery2 = 1
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
AND @HasQuery3 = 1
union all
select * from t4
where [date] between '2014-01-01' and '2019-01-01'
AND @HasQuery4 = 1
)
您可以像视图一样在连接中使用该函数,但您可以传递要返回的数据。
例如:
SELECT *
FROM T1 A
INNER JOIN V_1(1, 0, 0, 1) B
ON ....
我喜欢@gotqn 的回答,但是,如果它必须是一个视图,我会这样做:
--Sample Tables:
CREATE TABLE dbo.t1(id INT IDENTITY PRIMARY KEY, col1 INT);
CREATE TABLE dbo.t2(id INT IDENTITY PRIMARY KEY, col1 INT);
CREATE TABLE dbo.t3(id INT IDENTITY PRIMARY KEY, col1 INT);
CREATE TABLE dbo.t4(id INT IDENTITY PRIMARY KEY, col1 INT);
INSERT dbo.t1(col1) VALUES (10),(20),(30);
INSERT dbo.t2(col1) VALUES (60),(620),(630);
INSERT dbo.t3(col1) VALUES (0);
INSERT dbo.t4(col1) VALUES (0);
GO
--Solution
CREATE VIEW dbo.yourView AS
SELECT t.*,show = 1 FROM dbo.t1 AS t UNION ALL
SELECT t.*,show = 2 FROM dbo.t2 AS t UNION ALL
SELECT t.*,show = 3 FROM dbo.t3 AS t UNION ALL
SELECT t.*,show = 4 FROM dbo.t4 AS t
GO
这里的好处是优化器足够聪明,可以完全忽略并避免接触包含未包含在最终结果集中的数据的表。
注意这些查询和执行计划:
SELECT v.* FROM dbo.yourView AS v;
SELECT v.* FROM dbo.yourView AS v WHERE v.show = 1;
SELECT v.* FROM dbo.yourView AS v WHERE v.show IN (1,3);
SELECT v.* FROM dbo.yourView AS v WHERE v.show <> 1;
我有以下VIEW
定义:
create view v_1
as
select * from t1
where [date] between '2010-01-01' and '2019-01-01'
union all
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
union all
select * from t4
where [date] between '2014-01-01' and '2019-01-01'
场景 1:想要删除(或替换为空字符串)table t1
select 语句。
视图应如下所示:
create view v_1
as
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
union all
select * from t4
where [date] between '2014-01-01' and '2019-01-01'
场景 2:想要删除 table t4
select 语句。
视图应如下所示:
create view v_1
as
select * from t1
where [date] between '2010-01-01' and '2019-01-01'
union all
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
注意:根据要求可能需要从 VIEW 定义中删除任何 select 语句。
SQL 服务器使您能够在编辑器中使用正则表达式作为搜索模式,因此您需要做的就是:
按 Ctrl+F
启用正则表达式
作为搜索模式使用
select.+from t1(.*\n)+?select
,其中t1
可以替换为任何 table 名称作为替换模式使用
select
见下图(替换后):
创建内联 table 函数而不是视图,也称为带参数的视图。例如:
create function v_1
(
@HasQuery1 BIT
,@HasQuery2 BIT
,@HasQuery3 BIT
,@HasQuery4 BIT
)
RETURNS TABLE
as
RETURN
(
select * from t1
where [date] between '2010-01-01' and '2019-01-01'
AND @HasQuery1 = 1
union all
select * from t2
where [date] between '2012-01-01' and '2019-01-01'
AND @HasQuery2 = 1
union all
select * from t3
where [date] between '2013-01-01' and '2019-01-01'
AND @HasQuery3 = 1
union all
select * from t4
where [date] between '2014-01-01' and '2019-01-01'
AND @HasQuery4 = 1
)
您可以像视图一样在连接中使用该函数,但您可以传递要返回的数据。 例如:
SELECT *
FROM T1 A
INNER JOIN V_1(1, 0, 0, 1) B
ON ....
我喜欢@gotqn 的回答,但是,如果它必须是一个视图,我会这样做:
--Sample Tables:
CREATE TABLE dbo.t1(id INT IDENTITY PRIMARY KEY, col1 INT);
CREATE TABLE dbo.t2(id INT IDENTITY PRIMARY KEY, col1 INT);
CREATE TABLE dbo.t3(id INT IDENTITY PRIMARY KEY, col1 INT);
CREATE TABLE dbo.t4(id INT IDENTITY PRIMARY KEY, col1 INT);
INSERT dbo.t1(col1) VALUES (10),(20),(30);
INSERT dbo.t2(col1) VALUES (60),(620),(630);
INSERT dbo.t3(col1) VALUES (0);
INSERT dbo.t4(col1) VALUES (0);
GO
--Solution
CREATE VIEW dbo.yourView AS
SELECT t.*,show = 1 FROM dbo.t1 AS t UNION ALL
SELECT t.*,show = 2 FROM dbo.t2 AS t UNION ALL
SELECT t.*,show = 3 FROM dbo.t3 AS t UNION ALL
SELECT t.*,show = 4 FROM dbo.t4 AS t
GO
这里的好处是优化器足够聪明,可以完全忽略并避免接触包含未包含在最终结果集中的数据的表。
注意这些查询和执行计划:
SELECT v.* FROM dbo.yourView AS v;
SELECT v.* FROM dbo.yourView AS v WHERE v.show = 1;
SELECT v.* FROM dbo.yourView AS v WHERE v.show IN (1,3);
SELECT v.* FROM dbo.yourView AS v WHERE v.show <> 1;