我有一个链接到Oracle 12.1数据库的SQL Server 12.0数据库.
我想在SQL Server数据库中创建一个视图,该视图从按日期过滤的Oracle表中返回数据. Oracle表在日期列上有一个索引.
成功运行的查询是:
select * from ORADB..SCHEMA.MYTABLE where MYDATE >= '20140701';
然而,这运行得非常缓慢.我假设这是因为比较发生在SQL Server中,所以每行都被返回.
如果我走:
DECLARE @earliest date = '20140701'; select * from ORADB..SCHEMA.MYTABLE where MYDATE >= @earliest;
然后它运行得很快,大概是因为条件被传递给Oracle所以正在使用表上的Oracle索引.
我的问题是我想创建一个视图.我找不到使用第二版代码创建视图的方法.如果我只是这样做:
create myview as select * from ORADB..SCHEMA.MYTABLE where MYDATE >= '20140701';
然后它运行缓慢.
是否存在SQL Server将传递给Oracle的日期文字的另一种格式,还是有另一种解决方案?我还想知道是否与创建Oracle链接时使用的参数有关.供参考,它们是:
USE [master] GO EXEC master.dbo.sp_addlinkedserver @server = N'ORADB',@srvproduct=N'Oracle',@provider=N'OraOLEDB.Oracle',@datasrc=N'DPDB' EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'ORADB',@useself=N'False',@locallogin=NULL,@rmtuser=N'MYUSER',@rmtpassword='#######' GO EXEC master.dbo.sp_serveroption @server=N'ORADB',@optname=N'collation compatible',@optvalue=N'false' GO EXEC master.dbo.sp_serveroption @server=N'ORADB',@optname=N'data access',@optvalue=N'true' GO EXEC master.dbo.sp_serveroption @server=N'ORADB',@optname=N'dist',@optname=N'pub',@optname=N'rpc',@optname=N'rpc out',@optname=N'sub',@optname=N'connect timeout',@optvalue=N'0' GO EXEC master.dbo.sp_serveroption @server=N'ORADB',@optname=N'collation name',@optvalue=null GO EXEC master.dbo.sp_serveroption @server=N'ORADB',@optname=N'lazy schema validation',@optname=N'query timeout',@optname=N'use remote collation',@optname=N'remote proc transaction promotion',@optvalue=N'true' GO
编辑:我刚刚发现了一个非常相似的问题:Forcing a SQL Remote Query to filter remotely instead of locally
我更喜欢ODBC格式:
--DateTime SELECT {ts'2015-09-20 12:30:00'} --Time (however this comes with "today"-time) SELECT {t'12:30:00'} --Date SELECT {d'2015-09-20'} GO
简单的日期文字不是文化独立的……
SET LANGUAGE ENGLISH; SELECT CAST('2014-09-13' AS DATETIME); GO SET LANGUAGE GERMAN; SELECT CAST('2014-09-13' AS DATETIME);--ERROR: there's no month "13" GO
但它有效 – 但是 – 目标类型为DATE(这种差异相当奇怪……):
SET LANGUAGE ENGLISH; SELECT CAST('2014-09-13' AS DATE); GO SET LANGUAGE GERMAN; SELECT CAST('2014-09-13' AS DATE);--ERROR: there's no month "13" GO
Thx to lad2025我想为completness添加“完整”ISO 8601,它工作正常:
SET LANGUAGE ENGLISH; SELECT CAST('2014-09-13T12:30:00' AS DATETIME); GO SET LANGUAGE GERMAN; SELECT CAST('2014-09-13T12:30:00' AS DATETIME); GO