2009-09-10 60 views
3

有沒有辦法在精簡版中執行條件插入? 我已經試過兩種方式,我認爲對SqlServer的將工作:SqlServer精簡版中的條件插入

INSERT INTO CUSTQUOTE (QTE_ID) VALUES (1) 
WHERE EXISTS(SELECT * FROM JOB WHERE JOB_NUMBER = 'EW090800345') 
There was an error parsing the query. [ Token line number = 2,Token line offset = 1,Token in error = WHERE ] 

IF EXISTS(SELECT * FROM JOB WHERE JOB_NUMBER = 'EW090800345') 
BEGIN 
    INSERT INTO CUSTQUOTE (QTE_ID) VALUES (1) 
END 
There was an error parsing the query. [ Token line number = 1,Token line offset = 1,Token in error = IF ] 

回答

2

爲什麼不:

INSERT INTO CUSTQUOTE (QTE_ID) 
SELECT 1 
FROM JOB WHERE JOB_NUMBER = 'EW090800345' 

或TOP 1,如果JOB_NUMBER不是唯一

+0

是的,這樣的作品,直到你嘗試將參數放入其中: INSERT INTO CUSTQUOTE(QTE_ID)SELECT @qte_id FROM JOB WHERE JOB_NUMBER ='EW090800345' 此位置不允許有參數。確保'@'符號位於有效位置,或者該SQL語句中的參數有效。 任何解決方法? Colin – Colin 2009-09-11 08:42:42

+0

只有當表中沒有任何具有這樣和那樣的PK值的行時,你將如何插入一行? :)不支持'IF'的精簡版令人驚訝。我只是偶然發現了這一點。 – 2012-03-21 15:11:11