2011-11-16 83 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