2011-11-16 84 views
0

我只是試圖截斷Subjects.subjectid中varchar字段的前兩個字符作爲指定的記錄子集,但它不起作用。我不能發現我的代碼有什麼問題(postgresql):字符串命令在SQL中失敗?

UPDATE subjects 
SET 
    subjectid = substring(S.subjectid from 2) 
FROM 
    ibg_studies ST,subjects S,dnasample D 
WHERE 
    D.studyindex=ST.studyindex 
    AND ST.studyabrv='CONGER' 
    AND D.subjectidkey=S.id 
    AND D.projectindex IS NULL 

任何想法讚賞。

+1

運行以下命令,告訴我們發生了什麼:我通過添加一列到表中,更新與字符串函數這個新列,然後複製該新列值到原來的目標字段,這樣克服的侷限性'SELECT S.subjectid FROM ibg_studies ST,受試者S,dnasample d WHERE D.studyindex = ST.studyindex AND ST.studyabrv = '海鰻' AND D.subjectidkey = S.id AND D.projectindex IS NULL ' –

回答

1

您的子查詢與正在更新的表不相關(不相關)。 我不知道你的意圖是什麼,但也許你想這(只是猜測):

UPDATE subjects sj 
SET 
    subjectid = substring(S.subjectid from 2) -- << what is this? 
FROM 
    ibg_studies st 
    -- ,subjects s 
    ,dnasample d 
WHERE d.studyindex = st.studyindex 
    AND st.studyabrv = 'CONGER' 
    AND d.subjectidkey = sj.id -- changed from s to sj? 
    AND d.projectindex IS NULL 
    ; 
0

的問題似乎是,SQL有更新領域,而調用同一字段中輸入字符串函數問題。

-- 6: copy modified subjectid to temp 
--UPDATE dnasample D 
--SET 
-- temp = substring(S.subjectid from 3) 
--FROM 
-- ibg_studies ST,subjects S 
--WHERE 
-- D.studyindex=ST.studyindex 
-- AND ST.studyabrv='CONGER' 
-- AND D.subjectidkey=S.id 
-- AND D.projectindex IS NULL 

-- 7: copy temp to subjectid 
--UPDATE subjects S 
--SET 
-- subjectid = D.temp 
--FROM 
-- ibg_studies ST,dnasample D 
--WHERE 
-- D.studyindex=ST.studyindex 
-- AND ST.studyabrv='CONGER' 
-- AND D.subjectidkey=S.id 
-- AND D.projectindex IS NULL 

-- 8: Remove the temp column 
ALTER TABLE dnasample DROP COLUMN temp 
+0

注意:我會手動添加或刪除SQL註釋操作符 - 然後重新運行該腳本。我只需要一次運行一個SQL命令,以便我可以檢查每個命令的結果。 – rixter