• I can help you get rid of this cursor. It seems that all you actually need is to read up on the output clause. From the original code you posted it looks like you are inserting into INSERT INTO [DI628SW].[NOYAU_PATIENT].[noyau].[TELEPHONE] and then getting the identity value from that insert to use to insert into [DI628SW].[NOYAU_PATIENT].[noyau].[TELEPHONE_PATIENT].

    This one the scenarios that OUTPUT will make your life a lot easier. http://msdn.microsoft.com/en-us/library/ms177564.aspx

    If I am reading your code correctly I think that the following should be pretty close. I can't rest it because we don't have ddl. If you need more specific coding help please take a few minutes and read the link in my signature about best practices.

    INSERT INTO [DI628SW].[NOYAU_PATIENT].[noyau].[TELEPHONE] (

    tel_categorie

    ,tel_numero

    ,tel_envoi_sms

    ,tel_principal

    ,tel_commentaire

    ,tel_administratif

    ,tel_actif

    )

    OUTPUT INSERTED.YourIdentityColumn, INSERTED.easilyPatId

    into [DI628SW].[NOYAU_PATIENT].[noyau].[TELEPHONE_PATIENT] (tel_id, pat_id)

    SELECT @easilyTypeNumTelId

    ,@cnetNumTel

    ,@cnetSMS

    ,@cnetnumeroPrincipal

    ,@cnetCommentaire

    ,0

    ,1

    Also, watch those NOLOCK hints. They are very nasty and can cause some very unexpected results. Here a few articles on the topic.

    http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx

    http://www.jasonstrate.com/2012/06/the-side-effect-of-nolock/[/url]

    http://blogs.msdn.com/b/sqlcat/archive/2007/02/01/previously-committed-rows-might-be-missed-if-nolock-hint-is-used.aspx

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/