How to do Sql Server CE table update from another table

吃可爱长大的小学妹 提交于 2019-11-27 15:47:59

Your second attempt doesn't work because, based on the Books On-Line entry for UPDATE, SQL CE does't allow a FROM clause in an update statement.

I don't have SQL Compact Edition to test it on, but this might work:

UPDATE JOBMAKE
SET WIP_STATUS = '10sched1'
WHERE EXISTS (SELECT 1
              FROM JOBVISIT AS JV
              WHERE JV.JBT_TYPE   = JOBMAKE.JBT_TYPE
              AND   JV.JOB_NUMBER = JOBMAKE.JOB_NUMBER
              AND   JV.JVST_ID    = @jvst_id
             )

It may be that you can alias JOBMAKE as JM to make the query slightly shorter.

EDIT

I'm not 100% sure of the limitations of SQL CE as they relate to the question raised in the comments (how to update a value in JOBMAKE using a value from JOBVISIT). Attempting to refer to the contents of the EXISTS clause in the outer query is unsupported in any SQL dialect I've come across, but there is another method you can try. This is untested but may work, since it looks like SQL CE supports correlated subqueries:

UPDATE JOBMAKE 
SET WIP_STATUS = (SELECT JV.RES_CODE 
                  FROM JOBVISIT AS JV 
                  WHERE JV.JBT_TYPE = JOBMAKE.JBT_TYPE 
                  AND   JV.JOB_NUMBER = JOBMAKE.JOB_NUMBER 
                  AND   JV.JVST_ID = 20
                 )

There is a limitation, however. This query will fail if more than one row in JOBVISIT is retuned for each row in JOBMAKE. If this doesn't work (or you cannot straightforwardly limit the inner query to a single row per outer row), it would be possible to carry out a row-by-row update using a cursor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!