如何在 SQL 服务器脚本中使用 temp table?

How to use temp table in SQL Server script?

我必须在 sql 脚本中使用一些硬编码数据,为此我使用临时 table

CREATE TABLE #Temp
(
    [Type] INT,
    StartDate DATETIME,
    EndDate DATETIME,
    InterestRate DOUBLE
);

INSERT INTO #Temp 
VALUES (1, '2015-07-01', '2017-05-01', 27),
       (1, '2017-05-02', '2017-06-30', 25.71)

第 6 行显示错误 Expecting Id

有没有最简单的方法来做到这一点?

sql server 中没有 double 数据类型使用 decimal

CREATE TABLE #Temp
(
    [Type] Int,
    StartDate DateTime,
    EndDate DateTime,
    InterestRate decimal
);

Insert Into #Temp Values 
(1,'2015-07-01','2017-05-01',27),
(1,'2017-05-02','2017-06-30',25.71)

SQL 不支持 DOUBLE ,您可以使用 floatdecimal 代替 DOUBLE.

CREATE TABLE #Temp
(
    [Type] Int,
    StartDate DateTime,
    EndDate DateTime,
    InterestRate decimal
);

Insert Into #Temp Values 
(1,'2015-07-01','2017-05-01',27),
(1,'2017-05-02','2017-06-30',25.71)

你应该使用decimal,但你需要给一个scaleprecision。默认值为 0 。 . .这对利率不是很有用。参见 this example

CREATE TABLE #Temp (
    [Type] INT,
    StartDate DATETIME,
    EndDate DATETIME,
    InterestRate DECIMAL(10, 6)
);

INSERT INTO #Temp 
    VALUES (1, '2015-07-01', '2017-05-01', 27),
           (1, '2017-05-02', '2017-06-30', 25.71);

此外,SQL Server does have floating point types。它们被称为 floatreal。出于财务目的,我认为decimal/numeric是更好的选择。