更新表,其中表名和选择查询

时间:2017-05-03 06:03:48

标签: sql postgresql

我必须更新一个表,其中表名和id是select查询的结果。

 (select article.articleId ,'MUSIC' as type ,createddate FROM MTDDiscussion article where  article.status=2  and article.createdBy=101 order by createdDate ASC limit 1) UNION (select article.articleId, 'SPORTS' as type ,createddate  FROM MTDOpinion article where  article.status=2  and article.createdBy=101 order by createdDate ASC limit 1)UNION  (select article.articleId ,'DANCE' as type  ,createddate FROM MTDSurvey article where  article.status=2  and article.createdBy=101 order by createdDate ASC limit 1) UNION  (select article.wikiTopicId as articleId ,'SINGING' as type ,createddate  FROM WikiTopic article where  article.status=2  and article.createdBy=101 order by createdDate ASC limit 1) order by 3 ASC limit 1

有输出

 articleid  type        createddate
    1       MUSIC  2017-04-28 12:53:55.649

现在必须更新MUSIC table where id =1

的satus

1 个答案:

答案 0 :(得分:-1)

将表名,ID和状态值输入某个变量,然后使用以下查询。

  UPDATE_QUERY:='UPDATE '||V_TABLE||
' SET STATUS='||''''||V_STATUS||''''||
' WHERE ID ='''||V_ID||'''';

    EXECUTE IMMEDIATE UPDATE_QUERY;
    COMMIT;