Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用tsql将行拆分为多行_Tsql - Fatal编程技术网

使用tsql将行拆分为多行

使用tsql将行拆分为多行,tsql,Tsql,我希望编写一个脚本,将表1更改为表2 表1 AccountNo Name其他字段 1 Mr T and Mrs M Lambert xxx 1 a Mr T Lambert xxx 1 b Mrs M Lambert xxx 我需要把这个改写成 表2 AccountNo拆分名称其他字段 1 Mr T and Mrs M Lambert

我希望编写一个脚本,将表1更改为表2

表1

AccountNo Name其他字段

   1      Mr T and Mrs M Lambert      xxx
   1       a          Mr T Lambert             xxx
   1       b          Mrs M Lambert            xxx
我需要把这个改写成

表2

AccountNo拆分名称其他字段

   1      Mr T and Mrs M Lambert      xxx
   1       a          Mr T Lambert             xxx
   1       b          Mrs M Lambert            xxx

如果我是你,我会选择一些脚本语言,并在其帮助下编写转换器。在我看来,Perl或Ruby非常适合这个任务

例如,在Ruby中,这可能类似于:

require 'active_record'

ActiveRecord::Base.establish_connection('postgresql://localhost/db')
sql = ActiveRecord::Base.connection

sql.begin_db_transaction

# fetch the data from the source table
sql.execute('SELECT * FROM source_table').each do |source_row|
  # source_row is a hash now of the following form:
  # { 'col_name_1' => 'col_value_1', 'col_name_2' => ... }

  # prepare transformed rows array that will result in the destination table
  transformed_rows = [ ]

  # make up all the transformed rows you need, based on the source fields
  transformed_rows << {
    'another_col_1' => source_row['col_name_1'],
    # ...
  }

  transformed_rows.each do |transformed_row|
    # generate and execute the insert statement for every transformed_row
    sql.execute("INSERT INTO destination_table(...) VALUES(...)")
  end
end

sql.commit_db_transaction
毫无疑问,它可以在SQL中实现,特别是在更丰富的方言中,如PL/SQL,但是文本解析显然不是SQL的强项。因此,您将花费大量时间在不太适合字符串操作的语言中找出字符串操作


希望有帮助

您所要做的就是将from子句更改为表名,它应该可以工作

SELECT  AccountNo,
        person,
        other
FROM (VALUES(1,'Mr T and Mrs M Lambert','xxx')) AS yourTable (AccountNo,Name,Other)
CROSS APPLY (SELECT REVERSE(SUBSTRING(REVERSE(Name),0,CHARINDEX(' ',REVERSE(name))))) CA(lastName)
CROSS APPLY (
                SELECT PARSENAME(REPLACE(name,' and ','.'),1) person --first person
                UNION ALL 
                SELECT PARSENAME(REPLACE(name,' and ','.'),2) + ' ' +  lastName --second person
             ) CA2
结果:

AccountNo   person                                                                                                                                                  other
----------- ------------------------------------------------------------------------------------------------------------------------------------------------------- -----
1           Mrs M Lambert                                                                                                                                           xxx
1           Mr T Lambert                                                                                                                                            xxx

对不起,您的更正过于简化了名称字段是一个自由文本字段,可能包含麦当劳先生和夫人琼斯先生史密斯夫人皮博迪先生L托马斯和夫人P史密斯公司名称琼斯和史密斯我需要将两个人分成一个记录,但不包括像琼斯和史密斯这样的公司名称公司这样做可能是一场噩梦,但我正在寻求任何帮助…………到目前为止,我什么都没有…………仍在努力改变我的想法。我的建议是请一家数据清理公司为您做这件事,除非您的表足够小,可以手工完成,或者结果的质量并不重要。你将如何判断你是否真的在与T先生打交道?谢谢大家的评论,我使用了Stephans的评论,效果非常好…………它让我走上了一条可能解决我问题的道路