简体   繁体   中英

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. 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. 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. 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.

If you CAN use DTS or SSIS, or can use a PC programming language to process your records and execute insert statements, do so. 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. 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:

  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. (Look it up: the syntax is a bit a of departure from most T-SQL functions.)

  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:

    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

    We'll call your second temp table @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. This is where you'll put all your parsing logic. Let's suppose your function is called 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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