• MonsterRocks (11/22/2010)


    is cursor needed for following scenario?

    create table #table1 (col1 int,col2 varchar(30))

    insert into #table1 values (1,'martin')

    insert into #table1 values (2,'tom')

    insert into #table1 values (3,'schrof')

    create table #table2(t_col1 int identity(1,1),t_col2 int)

    create table #table3 (f_col1 int identity(1,1),f_col2 varchar(30),f_col3 int)

    1.i have to select the values from table1 and insert the value of table1.col1 into table2.t_col2

    2. fetch the auto increment value and insert into table3.f_col3 and insert the value of table1.col2 into table3.f_col3

    cursor is necessary to achieve this?.. any suggestion pls?

    You don't need a cursor or a WHILE loop, SQL Server has the OUTPUT clause for this purpose. See BOL, the examples are excellent.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden