我有一些代码设计在表中插入值,写在SQL Server:转换从SQL Server的功能到MS Access
创建表/建模式:
CREATE TABLE Inter
([id] int, [MarkDate] date, [MaturityDate] date, [ZeroRate] int)
;
INSERT INTO Inter
([id], [MarkDate], [MaturityDate], [ZeroRate])
VALUES
(1, '2015-07-02', '2015-07-02', 1),
(2, '2015-07-02', '2015-07-03', 5),
(3, '2015-07-02', '2015-07-06', 15)
;
CREATE TABLE allDates
([id] int, [MaturityDate] date)
;
INSERT INTO allDates
([id], [MaturityDate])
VALUES
(1, '2015-07-01'),
(2, '2015-07-02'),
(3, '2015-07-03'),
(4, '2015-07-04'),
(5, '2015-07-05'),
(6, '2015-07-06'),
(7, '2015-07-07'),
(8, '2015-07-08'),
(9, '2015-07-09')
;
CREATE TABLE rangesInter
([id] int, [MarkDate] date, [begindate] date, [enddate] date, startRate float, rateChange float);
INSERT INTO rangesInter
SELECT
I1.id,
I1.[MarkDate],
I1.[MaturityDate] begindate,
I2.[MaturityDate] enddate,
I1.[ZeroRate] startRate,
(I2.ZeroRate - I1.ZeroRate) * 1.0/DATEDIFF (day , I1.[MaturityDate], I2.[MaturityDate]) rateChange
FROM Inter I1
inner join Inter I2
on I1.id = I2.id - 1;
插入值:
SELECT
IIF(i.MarkDate IS NULL, r.MarkDate, i.MarkDate) as MarkDate,
a.MaturityDate,
IIF(i.ZeroRate IS NULL,
r.startRate + DATEDIFF (day , r.begindate, a.MaturityDate) * rateChange,
i.ZeroRate) as ZeroRate,
i.*, r.*
FROM
allDates a
LEFT JOIN
Inter I ON a.MaturityDate = I.MaturityDate
CROSS JOIN
(SELECT
MIN(MaturityDate) minDate, MAX(MaturityDate) maxDate
FROM Inter) AS t
LEFT JOIN
(SELECT
I1.id, I1.[MarkDate],
I1.[MaturityDate] begindate, I2.[MaturityDate] enddate,
I1.[ZeroRate] startRate,
(I2.ZeroRate - I1.ZeroRate) * 1.0/DATEDIFF (day , I1.[MaturityDate], I2.[MaturityDate]) rateChange
FROM
Inter I1
INNER JOIN
Inter I2 ON I1.id = I2.id - 1) r ON a.MaturityDate > r.[begindate]
AND a.MaturityDate < r.[enddate]
WHERE
a.MaturityDate >= t.minDate
AND a.MaturityDate <= t.maxDate;
我怎么会去这个代码转换到MS Access VBA?
我不知道如何开始转换代码的“插入值”部分与Access VBA工作。
看起来合法的给我。你真的尝试在Access中运行该代码吗?它给了你什么具体的错误?如果有的话,我会认为它不会反过来工作;我以为你需要在SQL Server中使用CASE WHEN语句而不是IIF。 –
在VBA中编写代码时,您会发现此答案中概述的技术可帮助您简化和在VBA中构建的SQL字符串请参阅:http://stackoverflow.com/questions/31684546/how-to-format-a-sql-string这是一个可读的代码块,它的格式很好 – HarveyFrench