USE [Darmangah]
GO
/****** Object:  StoredProcedure [dbo].[_9223_triana_bimaran_to_tb_study]    Script Date: 2020/02/01 14:35:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[_9223_triana_bimaran_to_tb_study]
AS
BEGIN

  DECLARE @code_bimar int
  DECLARE @name nvarchar(50)
  DECLARE @famil nvarchar(50)
  DECLARE @patint_number_transferred int = 0

  IF EXISTS (SELECT TOP 1
      code_bimar
    FROM bimaran
    WHERE transferred = 0
    OR transferred IS NULL)
  BEGIN
    DECLARE cursor_bimaran CURSOR FAST_FORWARD FOR
    SELECT
      code_bimar,
      name,
      famil
    FROM bimaran
    WHERE transferred = 0
    OR transferred IS NULL
    ORDER BY code_bimar

    OPEN cursor_bimaran
    FETCH NEXT FROM cursor_bimaran INTO @code_bimar, @name, @famil

    WHILE @@FETCH_STATUS = 0
    BEGIN


      IF NOT EXISTS (SELECT
          id
        FROM [db_triana].[dbo].[tb_study]
        WHERE id = @code_bimar)
      BEGIN


        INSERT INTO db_triana.dbo.tb_study (ID,
        OtherID,
        Name
        , BirthDate
        , Age
        , [GUID]
        , Tempate_PA
        , Tempate_CP
        , Tempate_OX
        , Tempate_OV
        , Tempate_DC
        , CountPX
        , CountCX
        , CountOX
        , CountOV
        , CountDC
        , CountCT
        , LastName
        , Email
        , Address1
        , Address2
        , HomePhone
        , MobilePhone
        , Fax
        , OtherName
        , CountTOMO)
          VALUES (@code_bimar, @code_bimar, @name + ' ' + @famil, 'Birth', 'Age', 'GUID', 'Tempate_PA', 'Tempate_CP', 'Tempate_OX', 'Tempate_OV', 'Tempate_DC', '1', '1', '1', '1', '1', '1', 'LastName', 'Email', 'Address1', 'Address2', 'HomePhone', 'MobilePhone', 'Fax', 'OtherName', '1')

      END

      IF @@ROWCOUNT = 1
      BEGIN
        UPDATE bimaran
        SET transferred = 1
        WHERE code_bimar = @code_bimar
        SET @patint_number_transferred = @patint_number_transferred + 1
      END



      FETCH NEXT FROM cursor_bimaran INTO @code_bimar, @name, @famil
    END
    CLOSE cursor_bimaran
    DEALLOCATE cursor_bimaran



  END
  SELECT
    @patint_number_transferred AS s_result,
    'Number Of Inserted=' + STR(@patint_number_transferred) + ' with sp  ' AS s_message,
    'param ' AS s_param



END

/*

--  Token = newid(), LastLogin = GetDate()



INSERT INTO tb_study
           (
            ID,
			OtherID,
		    Name
           ,BirthDate
           ,Age
           ,[GUID]
           ,Tempate_PA
           ,Tempate_CP
           ,Tempate_OX
           ,Tempate_OV
           ,Tempate_DC
           ,CountPX
           ,CountCX
           ,CountOX
           ,CountOV
           ,CountDC
           ,CountCT
           ,LastName
           ,Email
           ,Address1
           ,Address2
           ,HomePhone
           ,MobilePhone
           ,Fax
           ,OtherName
           ,CountTOMO
		   )
     VALUES
           ('id','OtherID','name','Birth','Age','GUID','Tempate_PA',
		   'Tempate_CP','Tempate_OX','Tempate_OV','Tempate_DC','1',
		   '1','1','1','1','1','LastName',
		   'Email','Address1','Address2','HomePhone','MobilePhone','Fax','OtherName','1')

*/
--  tb_order  save image tbale


/*


  insert into [sqlauth_pg].[E2].[public].[e2_pat] 
  (pat_id,
  pat_chartno,
  pat_lastname,
  pat_firstname
  )
 (select code_bimar as pat_id ,code_bimar as pat_chartno,famil as pat_lastname,name as pat_firstname from bimaran)

 بیشتر از 100 تا ثبت نمیشه توی  پست گرس 


*/

