开发者

SQL update and insert

I want to do this

UPDATE CATALOGUE 
SET CATALOGUE.NUMBERINSTOCK=NUMBERINSTOCK-1 
WHERE CATALOGUE.ITEMID =(SELECT ALBUM.ALBUM_ID 
                        开发者_开发百科 FROM ALBUM 
                         WHERE ALBUM.TITLE = '20 People in a Field'
                              AND IS_DISTRIBUTED_AS ='c')

and this

INSERT INTO ORDERELEMENT VALUES (1,10,12,17)

if this is true

(SELECT NUMBERINSTOCK 
 FROM CATALOGUE 
 WHERE CATALOGUE.ITEMID =(SELECT ALBUM.ALBUM_ID 
                          FROM ALBUM 
                          WHERE ALBUM.TITLE = '20 People in a Field'
                                   AND IS_DISTRIBUTED_AS ='c')) > 0 

The problem I have is finding how to do a conditional statement in an SQL query, and also how to do an update and insert as part of one query.

I must do all these things as one query.


You can't update one table and insert into another table in one statement. It is possible to conditionally perform an UPDATE if you specify a WHERE clause. You can acomplish the same thing with insert (details depends on dbms).

You can however create a stored procedure that contains your conditional logic. The invokation of that procedure might count as "one" statement?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜