问题描述
我创建了一个查找表,其中包含 insert 语句.值中的某些值带有撇号.
i have created a lookup table which holds insert statements. some of the values in the values have apostrophes in them.
值是这样的
'scrw, pan-hd phil, thr'd: 8-32. l: 3/8""'
整个声明是:
insert into parts_bomd (bom_id, item_rev, item_cn, item_number, itemnumber, findnum, qty, itemdescription, itemrev, itemsize, refdes, bomtext02, itemlist21, summarycompliance, bommultitext30, bomnotes, itemlist10, bomlist01, bomlist03, bomlist02, itemtext22, itemtext23, itemlifecyclephase, itemp2multilist05, itemtext15, rnum) values (2009034062,'31','eco05447','1472096','1422042','100','4','scrw, pan-hd phil, thr'd: 8-32. l: 3/8""','a0 mpl03682','pc','pc','','6 out of 6 compliant','missing info','missing info','','zhlb','','','x','','','arel','yes','0.10220',582272);
这个表有超过 400 万条记录,这个问题经常发生.
this table have more than 4 million records and this issue occurs most of the time.
有什么办法可以把这个撇号改成两个单引号.
is there any way i can change this apostrophe into two single quotes.
我使用的是 sql server 2014.
i am using sql server 2014.
生成这些插入语句的 oracle 脚本:
oracle scripts which generates these insert statements:
set serveroutput on size 100000 set feedback off declare v_table_name varchar2(30) := 'parts_bomd'; -- your tablename v_column_list varchar2(2000); v_insert_list varchar2(2000); v_ref_cur_columns varchar2(4000); v_ref_cur_query varchar2(2000); v_ref_cur_output varchar2(2000); v_column_name varchar2(2000); cursor c1 is select column_name, data_type from user_tab_columns where table_name = v_table_name order by column_id; refcur sys_refcursor; begin for i in c1 loop v_column_list := v_column_list||','||i.column_name; if i.data_type = 'number' then v_column_name := i.column_name; elsif i.data_type = 'date' then v_column_name := chr(39)||'to_date('||chr(39)||'||chr(39)'||'||to_char('||i.column_name||','||chr(39)||'dd/mm/yyyy hh:mi:ss'||chr(39)||')||chr(39)||'||chr(39)||', '||chr(39)||'||chr(39)||'||chr(39)||'dd/mm/rrrr hh:mi:ss'||chr(39)||'||chr(39)||'||chr(39)||')'||chr(39); elsif i.data_type = 'varchar2' then v_column_name := 'chr(39)||'||i.column_name||'||chr(39)'; end if; v_ref_cur_columns := v_ref_cur_columns||'||'||chr(39)||','||chr(39)||'||'||v_column_name; end loop; v_column_list := ltrim(v_column_list,','); v_ref_cur_columns := substr(v_ref_cur_columns,8); v_insert_list := 'insert into '||v_table_name||' ('||v_column_list||') values '; v_ref_cur_query := 'select '||v_ref_cur_columns||' from '||v_table_name; open refcur for v_ref_cur_query; loop fetch refcur into v_ref_cur_output; exit when refcur%notfound; v_ref_cur_output := '('||v_ref_cur_output||');'; v_ref_cur_output := replace(v_ref_cur_output,',,',',null,'); v_ref_cur_output := replace(v_ref_cur_output,'(,','(null,'); v_ref_cur_output := replace(v_ref_cur_output,',,)',',null)'); v_ref_cur_output := replace(v_ref_cur_output,'null,)','null,null)'); v_ref_cur_output := v_insert_list||v_ref_cur_output; --dbms_output.put_line (v_ref_cur_output); insert into bom_ins_lookup(lookup_statment) values (v_ref_cur_output); commit; end loop; end; /
虽然不多,但它可以完成工作.
it ain't much but it does the job.
推荐答案
真的没有一种简单的方法可以通用地解决这个问题.您将如何处理这样的值列表?:'a',',','b' 是一个还是两个或三个值?并且有两种方法可以将其拆分为两个值.
there really isn't an easy way to approach this problem generically. how would you go about processing a list of values like this one?: 'a',',','b' is it one or two or three values? and there are two ways to split it into two values.
您可以通过对格式做出一些假设来采取以下方法.
here's an approach you might take by making some assumptions about the format.
declare @pos int; declare @s varchar(1024); declare mycursor for selectfrom t; open mycursor; while @@fetch_status = 0 begin fetch next from mycursor into @s; set @pos = charindex(@s, '''', @pos); while @pos > 0 begin /* if apostrophe is followed by comma plus apostrophe then assume this is the delimiter */ if substring(@s ',''', @pos 1, 2) <> ',''' set @s = stuff(@s, @pos, 1, ''''''); else set @pos = @pos 2; set @pos = charindex(@s, '''', @pos); end update t set = @s where current of mycursor; end close mycursor; deallocate mycursor;
最好通过在生成 insert 查询时正确引用值来首先避免该问题.有多种方法可以从 oracle 导出数据,sql server 可以轻松获取这些数据.
it would be better to avoid the problem in the first place by properly quoting the values as the insert queries are generated. there are many ways to export data from oracle that can be readily picked up by sql server.