您的位置:首页 > 数据库

使用SQL语句进行数据迁移(by quqi99)

2007-11-12 11:37 274 查看
使用SQL语句进行数据迁移(by quqi99)



作者:张华 发表于:2007-12-03 ( http://blog.csdn.net/quqi99 )

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明。




1、例如有表:td_question(question_title,question_time,........),现有假设通过前一部分的工作已经将时间放入了标题字段的末尾,即例如在question_title字段中的值为“外商投资企业应在什么期限内办理所得税汇算清缴?2007/11/15”。我们现在需要将时间2007/11/15提出来拼成20071115000000的格式置入question_time字段中?

答:

update consultor.td_questions set question_time=replace(substring(question_title,len(question_title)-9,len(question_title)),'/','') + '000000'

在导入过程中,我遇到如下问题,

首先,我开始通过right(question_title,patindex(question_title,'%200_/__/__%'))函数来在标题字段中找到2007/11/15,可是发现patindex函数定位不准,最后只好改用substring替换。

其次,在找到2007/11/15之后,本来我开始是通过convert(varch(8),right(question_title,patindex(question_title,'200_/__/__')),112)函数将其将化为20071115格式,但是不行,后来改为通过replace替换

2、例如:td_answer表中也有answer_time字段需要设置同样的值时,

update td_answer set td_answer.answer_time=q.question_time from td_questions q where td_answer.answer_question=q.question_code

注意:td_answer 后面不能跟别名,不然报错

3、例如,如有表t_person(id,name)与表jobreport(id,name,age),要将t_person中的name字段迁移到jobreport表中的name字段中去。

insert into jobreport(name) (select name from t_person)

4、将同一个表如t1(id,a,b,c)的a字段更新到b字段中去。

得用别名,

update t1 z1 set a=(select b rom t1 z2 where z1.id=z2.id)

commit();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: