2013-11-23 98 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