有表DocumentExamine,DocumentApprove.
两表有相同字段
DocumentName,DocumentAuthor,DocumentPath,DocumentTemplateID,
我在DocumentExamine创建了一个触发器ALTER TRIGGER [dbo].[tr_DocumentExamine] 
   ON [dbo].[DocumentExamine]  
   for update
AS 
insert DocumentApprove(DocumentName,DocumentAuthor,DocumentPath,DocumentTemplateID)
select DocumentName,DocumentAuthor,DocumentPath,DocumentTemplateID from inserted where agree=1
DocumentExamine有agree字段.
当字段为1时. 才输入数据到DocumentApprove表.DocumentApprove 表有first_approve,second_approve字段.
当进行了上一步操作. 插入了
DocumentName,DocumentAuthor,DocumentPath,DocumentTemplateID
这四个字段的数据后.
进行DocumentApprove表的first_approve,second_approve字段的数据插入.
通过DocumentApprove的DocumentTemplateID字段引用DocumentFlow表中的first_approve,second_approve的值.
DocumentApprove,DocumentTemplate,DocumentFlow有一下关系.
DocumentTemplate有DocumentTemplateID(主键) DocumentFlowID(外键)
DocumentApprove 有DocumentTemplateID(外键)
DocumentFlow 有 DocumentFlowID(主键) first_approve,second_approve.我在SQL中输入以下查询语句 是能将DocumengFlow的first_approve,second_approve.两个字段的值引用到
DocumentApprove中.update da set
da.first_approve=(select first_approve from DocumentFlow as df
 INNER JOIN DocumentTemplate as dt ON dt.DocumentFlowID=df.DocumentFlowID 
 Where dt.DocumentTemplateID=2),
 da.second_approve=(select second_approve from DocumentFlow as df
 INNER JOIN DocumentTemplate as dt ON dt.DocumentFlowID=df.DocumentFlowID 
  Where dt.DocumentTemplateID=2)
 From DocumentApprove as da where da.DocumentName='数据库名1.doc'
但在VS里写以下代码却不行. StringBuilder strSql = new StringBuilder();
            strSql.Append("update da set");
            strSql.Append("da.first_approve=(select first_approve from DocumentFlow as df ");
            strSql.Append("INNER JOIN DocumentTemplate as dt ON dt.DocumentFlowID=df.DocumentFlowID ");
            strSql.Append("Where dt.DocumentTemplateID=@DocumentTemplateID),");
            strSql.Append("da.second_approve=(select second_approve from DocumentFlow as df");
            strSql.Append("INNER JOIN DocumentTemplate as dt ON dt.DocumentFlowID=df.DocumentFlowID ");
            strSql.Append(" Where dt.DocumentTemplateID=@DocumentTemplateID)");
            strSql.Append("From DocumentApprove as da where da.DocumentName=@DocumentName");
            SqlParameter[] parameters = {
                    new SqlParameter("@DocumentTemplateID", dt.DocumentTemplateID),
                    new SqlParameter("@DocumentName", dt.DocumentName)
                                        };
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); 请教高人..谢谢.

解决方案 »

  1.   


     StringBuilder strSql = new StringBuilder();
                strSql.Append(" update da set");
                strSql.Append(" da.first_approve=(select first_approve from DocumentFlow as df ");
                strSql.Append(" INNER JOIN DocumentTemplate as dt ON dt.DocumentFlowID=df.DocumentFlowID ");
                strSql.Append(" Where dt.DocumentTemplateID=@DocumentTemplateID),");
                strSql.Append(" da.second_approve=(select second_approve from DocumentFlow as df");
                strSql.Append(" INNER JOIN DocumentTemplate as dt ON dt.DocumentFlowID=df.DocumentFlowID ");
                strSql.Append(" Where dt.DocumentTemplateID=@DocumentTemplateID)");
                strSql.Append(" From DocumentApprove as da where da.DocumentName=@DocumentName");
    ""中前都加个空格试试!
      

  2.   


    原来是这个问题.....耗了我好多时间去寻找.
    嗯! 日后用append方法都得更加注意了.!
    感谢你的回答!