2013-11-23 99 views
1

我在高速缓存中进行设计,它看起来像它不允许多个插入,即SQL插入不起作用

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

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

所以我决定创建方法执行插入。问题 - 这是行不通的。每个插入都很好。删除也起作用,但不能插入。没有错误,只是空表。

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 
} 

任何想法?

+0

“每个插入都很好,删除也可以,但不能插入”?你的意思是单独的插入语句工作正常,但[表值构造函数](http://technet.microsoft.com/en-us/library/dd776382.aspx)方法不起作用? –

+0

嗯,是的。单独的插入工作正常,如果通过门户执行(这是phpmyadmin模拟)。两个和更多的插入不能在这个门户网站(因为语法是错误的,所以我认为这不支持)。如果从Object Method中调用,则不插入任何工作。没有错误。 我不知道表值构造函数,但不认为它是它。我不创建新的表格,我使用现有的表格。 现在我试图做ClassMethod,非对象..可能会工作。 (PS如果你知道如何在Cache中调用ClassMethod,请告诉他们的文档是非常有用的。 – Tigran

+0

write ## class(Ruler).Fill() - 这是ClassMethod调用的方法,结果为0(不再插入) – Tigran

回答

3

作为一个起点,缓存不支持在单个语句中插入多个数据。

要回答你关于失败的问题,我怀疑你被阻止执行插入操作,因为默认情况下,如果ID被自动分配,Cache不允许插入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代码的样本。

+0

谢谢你,但我发现用简单的对象解决方案...... 集O = ##级(统治者)。%新() 集o.nameRuler = “费利克斯堡” 集o.idRuler = 9 做o。%Save() – Tigran

+0

我将使用我的版本,但可能会有人在将来会使用SQL。 – Tigran

+0

另外,还有很多很好的代码行...比官方文档更好:) – Tigran