简体   繁体   English

T-SQL:在没有光标的情况下将数据从一个表插入到另一个表

[英]T-SQL: Insert data from one table to another without curosrs

This is the kind of data I have: 这是我拥有的那种数据:

ENT*2*2J*EI*A25530181
NM1*IL*1*DOBIAS*ROSE*M
RMR*AZ*10100314**362.45
DTM*582****RD8*20120301-20120331
ENT*3*2J*EI*A54700554
NM1*IL*1*LOMBARDO*LUIS*J
RMR*AZ*10100314**362.45
DTM*582****RD8*20120301-20120331

The situation is that I need to extract information from each row and populate another table with it. 情况是我需要从每一行中提取信息,并用它填充另一个表。 So like from row beginning with ENT I need the code A25530181 and put it into another table. 因此,就像从以ENT开始的行中一样,我需要代码A25530181并将其放入另一个表中。 I have all the logic down for extracting information. 我有提取信息的全部逻辑。 Just need to figure out how to put it into another table with out cursors. 只需弄清楚如何将其放入没有游标的另一个表中。 Information from first 4 rows from this table will be one row in the new one. 该表的前4行中的信息将是新表中的一行。 So 所以

ENT*2*2J*EI*A25530181
NM1*IL*1*DOBIAS*ROSE*M
RMR*AZ*10100314**362.45
DTM*582****RD8*20120301-20120331

is going to be used to populate one row of the new table with Client Code, dates and amount from the data given above. 将用于使用上面给出的数据用客户代码,日期和金额填充新表的一行。 Row is DTM provides dates, and row with RMR provides the rate. DTM行提供日期,RMR行提供费率。 I have the logic down to extract the info. 我有逻辑提取信息。 Just need help in putting it into the new table without cursor. 只需帮助将其放入没有光标的新表中即可。

Here is the sample logic: 这是示例逻辑:

SELECT @Asterisk1Pos = CHARINDEX(@Delimeter, REVERSE(DATA)) - 1
from @TEMP_TABLE
WHERE LEFT(DATA,3) = 'ENT'

SELECT TOP 1 RIGHT(DATA, @Asterisk1Pos) 
from @TEMP_TABLE 
WHERE LEFT(DATA,3) = 'ENT'

This way I have info for just one row. 这样,我只有一行信息。

You say that you have the extract logic down for parsing data out of the records, so I won't dwell on that. 您说您有提取逻辑以提取记录中的数据,所以我不再赘述。 Your problem appears to be that you have to process each record according to its contents, can't use DTS or SSIS, can use T-SQL, but can't use a cursor. 您的问题似乎是您必须根据其内容处理每个记录,不能使用DTS或SSIS,可以使用T-SQL,但是不能使用游标。

If you CAN use DTS or SSIS, or can use a PC programming language to process your records and execute insert statements, do so. 如果可以使用DTS或SSIS,或者可以使用PC编程语言来处理记录并执行插入语句,请执行此操作。 If this is an ad-hoc thing and you can get away with pre-processing your records in Excel, I'd even do that before trying to achieve this in T-SQL. 如果这是临时性的事情,并且您可以在Excel中对记录进行预处理,那么在尝试在T-SQL中实现此目的之前,我什至会这样做。 But if none of those apply and T-SQL is the only tool you have, here's a description of the steps I'd take if I were in your position: 但是,如果这些都不适用,并且T-SQL是您唯一的工具,那么以下是对我在您担任职务时应该采取的步骤的说明:

  1. Create a temp table with the records in it but an additional column representing the row number. 创建一个临时表,其中包含记录,但还有一列代表行号。 You can get this by using the ROW_NUMBER() function. 您可以使用ROW_NUMBER()函数获得此信息。 (Look it up: the syntax is a bit a of departure from most T-SQL functions.) (查找:语法与大多数T-SQL函数有点偏离。)

  2. Assuming your temp table is called @Temp and your fields are called RecordNumber and Data , create another temp table that joins the four (it's always four, right?) records that belong together into one record, like so: 假设您的临时表称为@Temp而您的字段称为RecordNumberData ,则创建另一个临时表,该临时表将属于同一条记录的四个(始终为四个,对吧?)记录联接在一起,如下所示:

    SELECT T1.RecordNumber, T1.data as Data1, T2.data as Data2, T3.data as Data3, T4.data as Data4 FROM @Temp T1 JOIN @Temp T2 ON T1.RecordNumber = T2.RecordNumber - 1 JOIN @Temp T3 ON T1.RecordNumber = T3.RecordNumber - 2 JOIN @Temp T4 ON T1.RecoreNumber = T4.RecordNumber - 3 SELECT T1.RecordNumber,T1.data作为Data1,T2.data作为Data2,T3.data作为Data3,T4.data作为Data4 FROM @Temp T1 JOIN @Temp T2 ON T1.RecordNumber = T2.RecordNumber-1 JOIN @Temp T3开T1.RecordNumber = T3.RecordNumber-2 JOIN @Temp T4开T1.RecoreNumber = T4.RecordNumber-3

    We'll call your second temp table @Temp2 . 我们将第二个临时表称为@Temp2

  3. Write a table-valued function that takes all 5 fields from @Temp2 as parameters and returns a table-valued function, with the output table format being that of the target table. 编写一个表值函数,该函数将@ Temp2中的所有5个字段用作参数并返回一个表值函数,其输出表格式为目标表的格式。 This is where you'll put all your parsing logic. 这是放置所有解析逻辑的地方。 Let's suppose your function is called dbo.udf_Function1 . 假设您的函数名为dbo.udf_Function1

  4. Do an insert into your target table (we'll call it Target), like so: 在目标表中插入(我们称为目标),如下所示:

    INSERT INTO @Target SELECT F1.* FROM @Temp2 T2 CROSS APPLY dbo.udf_Function1(T2.RecordNumber, T2.Data1, T2.Data2, T2.Data3, T2.Data4) F1 从@ Temp2 T2插入@Target SELECT F1。*交叉应用dbo.udf_Function1(T2.RecordNumber,T2.Data1,T2.Data2,T2.Data3,T2.Data4)F1

This works because of the CROSS APPLY capability introduced in (I think) SQL 2005. It will have the effect of aggregating the single records returned by each call to udf_Function1 into one set of records, which you can select from in an insert. 之所以能够成功,是因为(我认为)SQL 2005中引入了CROSS APPLY功能。它将具有将每次对udf_Function1调用返回的单个记录聚合到一组记录中的效果,您可以从插入记录中进行选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM