1

I perform design in cache and it looks like it does not allow multiple insert, i.e.

insert into Ruler (...) values (...), values().... 

or

insert into Ruler (...) values (...), (....)

So I decided to create method to perform insert. The problem - it does not work. Each insert is fine. Delete also worked, but not insert. No error, just empty table.

Method Fill() As %Integer
{
    &sql(insert into Ruler (nameRuler, biography, idRuler) 
        values  ('Peter the Great','Born in Moscow, Russia on June 9, 1672, Peter the Great was a Russian czar in the late 17th century who is best known for his extensive reforms in an attempt to establish Russia as a great nation. He created a strong navy, reorganized his army according to Western standards, secularized schools, administered greater control over the reactionary Orthodox Church, and introduced new administrative and territorial divisions of the country.', 1)
        )
    &sql(insert into Ruler (nameRuler, biography, idRuler) values ('Boris Godunov','was de facto regent of Russia from c. 1585 to 1598 and then the first non-Rurikid tsar from 1598 to 1605. The end of his reign saw Russia descend into the Time of Troubles.', 2))

    //&sql(delete from Ruler)   
    &sql(SELECT COUNT(*) INTO :count 
        FROM Ruler)

    Quit "Total: "_count
}

Any ideas???

4

1 回答 1

3

作为起点,Cache 不支持在单个语句中进行多次插入。

为了回答您关于失败的问题,我怀疑您被阻止执行插入,因为默认情况下,如果 ID 是自动分配的,缓存不允许插入 ID。您的代码没有对 SQLCODE 进行任何检查,因此要确认是否是这种情况非常棘手。

我强烈建议您的目标代码使用动态 SQL 来执行插入,因为这样更易于维护和执行错误检查。因此,您的代码可能如下所示:

ClassMethod Fill(Output pErrorMessage As %String) As %Integer
{
    Set pErrorMessage = ""
    Set tCount = 0
    Set tStatement = ##class(%SQL.Statement).%New()
    // If you want to use unqualified schema names then update the schema path
    Set tStatement.%SchemaPath = "MySchema,DEFAULT_SCHEMA"
    Set tStatus = tStatement.%Prepare("INSERT INTO Ruler (nameRuler, biography, idRuler) VALUES(?,?,?)")
    If $system.Status.IsError(tStatus) {
        Set pErrorMessage = $system.Status.GetErrorText(tStatus)
        Quit tCount
    }
    Set tRS1 = tStatement.%Execute("Peter the Great", "Born ...", 1)
    If (tRS1.%SQLCODE = 0) { // no logic for SQLCODE = 100 as this is an INSERT
        Set tCount = tCount + tRS1.%ROWCOUNT
    }
    Else {
        // Return an error
        Set pErrorMessage = "SQLCODE = " _ tRS1.%SQLCODE _ "; Message = " _ tRS1.%Message
        Quit tCount
    }
    // Repeat for subsequent rows
    // ...
    Quit tCount
}

以上内容非常冗长,但如果您愿意,我可以为您提供使用嵌入式 SQL 检查插入的 SQL 代码的示例。

于 2013-11-23T16:55:24.597 回答