Coldfusion:无法从查询结果集中引用 var

发布于 2024-11-24 14:40:03 字数 1122 浏览 0 评论 0 原文

使用 cfscript,尝试设置新插入问题的 ID,以便我可以在答案插入中使用它来构建关系。我已经在 cfscript 之外这样做了一百万次。 setName 似乎是调用来创建查询名称的正确方法。

我收到错误:qryQuestion 中不存在“theQuestionID”

i = 1; 
while ( structKeyExists( form, "question" & i ) ) 
{

    q = new Query();
    q.setDatasource("kSurvey");
    q.setName("qryQuestion");
    q.setSQL("
              set nocount on 
              insert into question (question) 
              values('#form["question#i#"]#')
              select @@IDENTITY AS theQuestionID
              set NOCOUNT off
              ");
    q.execute();

    writeOutput("Question"&i&"<br>");
    j = 1;
        while ( structKeyExists( form, "question" & i & "_answer" & j) ) {

            q = new Query();
            q.setDatasource("kSurvey");
            q.setSQL("
                      insert into answer (answer,questionid) 
                      values('#form["question#i#_answer#j#"]#',#qryQuestion.theQuestionID#)
                      ");
            q.execute();

            writeOutput("Answer"&j&"<br>");
            j++;
        }

i++; 
}

Using cfscript, trying to set the ID of the newly inserted question so that I can use it in my answer insert to build the relationship. I've done this a million times outside of cfscript. setName seems to be the proper method to call to create the query name.

I'm receiving the error that "theQuestionID" does not exist in qryQuestion

i = 1; 
while ( structKeyExists( form, "question" & i ) ) 
{

    q = new Query();
    q.setDatasource("kSurvey");
    q.setName("qryQuestion");
    q.setSQL("
              set nocount on 
              insert into question (question) 
              values('#form["question#i#"]#')
              select @@IDENTITY AS theQuestionID
              set NOCOUNT off
              ");
    q.execute();

    writeOutput("Question"&i&"<br>");
    j = 1;
        while ( structKeyExists( form, "question" & i & "_answer" & j) ) {

            q = new Query();
            q.setDatasource("kSurvey");
            q.setSQL("
                      insert into answer (answer,questionid) 
                      values('#form["question#i#_answer#j#"]#',#qryQuestion.theQuestionID#)
                      ");
            q.execute();

            writeOutput("Answer"&j&"<br>");
            j++;
        }

i++; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书信已泛黄 2024-12-01 14:40:03

有一种更好的方法可以完成此任务,而无需选择@@identity(这本身并不是从sql server获取它的最佳方法,使用scope_identity是在sql server中执行此操作的最佳实践方法。 http://msdn.microsoft.com/en-us/library/ms190315.aspx

幸运的是,ColdFusion 使这变得更加容易:

<cfscript>
    insertQuery = new query();

    insertQuery.setDatasource("datasourcename");

    insertQuery.setSql("insert into contact(firstname, lastname) 
           values('ryan','anklam')");

    result = insertQuery.Execute();

    theKey = result.getPrefix().generatedkey;
 </cfscript>

Theres a better way to accomplish this without having to select @@identity (which in itself isn't the best way to get it from sql server, using scope_identity is the best practice way to do this in sql server. http://msdn.microsoft.com/en-us/library/ms190315.aspx

Fortunately ColdFusion makes this even easier:

<cfscript>
    insertQuery = new query();

    insertQuery.setDatasource("datasourcename");

    insertQuery.setSql("insert into contact(firstname, lastname) 
           values('ryan','anklam')");

    result = insertQuery.Execute();

    theKey = result.getPrefix().generatedkey;
 </cfscript>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文