Jumat, 17 Juli 2020

Insert Menggunakan Variabel - SQL Server


set @newuser='dwi.haryatno' -- user yang baru
set @fromuser='asmadi@gmail.com' -- copy dari user (sama jabatan ( Cnth: admin))
set @Location='10006' -- pilih lokasi user
set @company=1 -- pilih company
set @Aplication=7 --application module
set @domain='@gmail.com'
set @newuser=@newuser+@domain


set @Counter=0;
        Select @UserModulePrivilegeId=max(cast(UserModulePrivilegeId as bigint))+1 from dbo.UserModulePrivilege where SubString(UserModulePrivilegeId,1,8)=CONVERT(varchar(8),getdate(),112);
        if (@UserModulePrivilegeId is null)
           set @UserModulePrivilegeId=CONVERT(varchar(8),getdate(),112)+'000000'
--print @UserModulePrivilegeId


insert into [DBFrameWork].[dbo].[UserCompany]
SELECT distinct @newuser
      ,@company
      ,SUSER_NAME()
      ,getdate()
      ,[AuditActivity]
  FROM [DBFrameWork].[dbo].[UserCompany]
  where UserName=@fromuser


insert into [DBFrameWork].[dbo].[UserModulePrivilege]
SELECT distinct @UserModulePrivilegeId+ROW_NUMBER() OVER (Order by @UserModulePrivilegeId) as ROW
      ,@newuser
      ,[ModuleId]
      ,[PrivilegeId]
      ,SUSER_NAME()
      ,getdate()
      ,[AuditActivity]
  FROM [DBFrameWork].[dbo].[UserModulePrivilege]
  where UserName=@fromuser
  AND PrivilegeId='RP'
  and ModuleId in (select ModuleId from [DBFrameWork].[dbo].[Module] where ApplicationId=@Aplication
  and ModuleId in ('200','201','203','204','205','206','207','208','210'))

insert into [DBFrameWork].[dbo].[UserLocation]
SELECT distinct @newuser
      ,@company
      ,@Location
      ,SUSER_NAME()
      ,getdate()
      ,[AuditActivity]
  FROM [DBFrameWork].[dbo].[UserLocation]
    where UserName=@fromuser

Tidak ada komentar:

Posting Komentar

Dynamic Query - SQL Server

Berikut adalah contoh dari bentuk dari Dynamic Query di SQL Server yang dibungkus ke dalam Store Procedure:  USE [Data00] GO SET ANSI_NULLS ...

Popular Posts