advanced TSQL

  • I've records and I need create a new table with this. But is not simple, in one field of the each record in my source table I've many informations that needed be extracted and inserted in the destination table. So, how to do this quick...I think in a cursor, but, is not work well.

    If someone can help me, I thank´s

    Marcelo Roza

  • Maybe you can give us an example of some records in your table.

    Try using functions like RIGHT, LEFT, SUBSTRING in a select query instead of the cursor, but still it may be necesary to use a cursor.

  • I'll second racosta.

    Without having some sample data and further explanation on what you want, it will be difficult to help

    Frank

    http://www.insidesql.de

    http://www.familienzirkus.de

    --
    Frank Kalis
    Microsoft SQL Server MVP
    Webmaster: http://www.insidesql.org/blogs
    My blog: http://www.insidesql.org/blogs/frankkalis/[/url]

  • I hate cursors. when I'm forced to I'll use a while loop and control it through a select and an incremented controler variable.

    if object_ID('tempdb..#ExSource') is not null drop table #ExSource

    create table #ExSource (RecordID int identity, CompKey varchar(10))

    Insert #ExSource Values ('ABCDEFG')

    Insert #ExSource Values ('HI')

    Insert #ExSource Values ('JKL')

    Insert #ExSource Values ('MNOP')

    if object_ID('tempdb..#ExDest') is not null drop table #ExDest

    create table #ExDest

    (RecordID int, parsedKey char(1), position int)

    declare @RecordID int,

    @CompKey varchar(10),

    @Len int--controller variable

    While 1 = 1--constant

    begin

    select top 1

    @RecordID = RecordID,

    @CompKey= CompKey,

    @len= 1--re-initialize controller variable

    from #ExSource

    Where RecordID > isnull(@RecordID, 0)

    IF @@RowCount = 0 Break--when no more records in source stop

    While 1 = 1--again, constant PS: avoid loops in loops if possible

    begin

    Insert #ExDest (RecordID, ParsedKey, position)

    select @RecordID, substring(@CompKey, @len, 1), @len

    IF len(@CompKey) = @len break --If whole key parsed then exit

    select@len = @len + 1--increment controller variable

    end

    end

    select * from #ExDest--parsed data

    Signature is NULL

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply