----------------------- Please Update resspective values below --------------------
DECLARE @PortalId INT = 0 -- current portal id
DECLARE @MasterTabId INT = 24 -- source page tabid, on which modul exists, and needs to be shown on all pages
DECLARE @MasterModuleId INT = 5472 -- module id which needs to be shown on all pages



----------------------- DO NOT EDIT BELOW THIS LINE -------------------------------


--remove existing copies
DELETE FROM TabModules WHERE ModuleID = @MasterModuleId AND TabID <> @MasterTabId
DECLARE @MasterTabVersion INT = (SELECT TOP 1 TabVersionId FROM TabVersions WHERE TabId = @MasterTabId ORDER BY CreatedOnDate DESC)
DELETE FROM TabVersionDetails WHERE ModuleId = @MasterModuleId AND TabVersionId <> @MasterTabVersion

UPDATE Modules SET AllTabs = 1 WHERE ModuleID = @MasterModuleId

-- auto variables

DECLARE @ModuleTitle nvarchar(256) = (SELECT ModuleTitle FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @ModuleOrder int = (SELECT ModuleOrder FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @PaneName nvarchar(50) = (SELECT PaneName FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @CacheTime int = (SELECT CacheTime FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @CacheMethod varchar(50) = (SELECT CacheMethod FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @Alignment nvarchar(10) = (SELECT Alignment FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @Color nvarchar(20) = (SELECT Color FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @Border nvarchar(1) = (SELECT Border FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @IconFile nvarchar(100) = (SELECT IconFile FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @Visibility int = (SELECT Visibility FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @ContainerSrc nvarchar(200) = (SELECT ContainerSrc FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @DisplayTitle bit = (SELECT DisplayTitle FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @DisplayPrint bit = (SELECT DisplayPrint FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @DisplaySyndicate bit = (SELECT DisplaySyndicate FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @IsWebSlice bit = (SELECT IsWebSlice FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @WebSliceTitle nvarchar(256) = (SELECT WebSliceTitle FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @WebSliceExpiryDate datetime = (SELECT WebSliceExpiryDate FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @WebSliceTTL int = (SELECT WebSliceTTL FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @CultureCode nvarchar(10) = (SELECT CultureCode FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @CreatedByUserID int = (SELECT CreatedByUserID FROM TabModules WHERE ModuleId = @MasterModuleId AND TabID = @MasterTabId)
DECLARE @DefaultLanguageGuid uniqueidentifier = null

DECLARE @CurrentTab INT



DECLARE db_cursor CURSOR FOR 
SELECT TabId
FROM Tabs
WHERE PortalId = @PortalId
AND TabId <> @MasterTabId

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @CurrentTab  

WHILE @@FETCH_STATUS = 0  
BEGIN  
		DECLARE @RC int
		DECLARE @UniqueId uniqueidentifier = NEWID()
		DECLARE @VersionGuid uniqueidentifier = NEWID()
		DECLARE @LocalizedVersionGuid uniqueidentifier = NEWID()

		EXECUTE @RC = [dbo].[AddTabModule] 
		   @CurrentTab
		  ,@MasterModuleId
		  ,@ModuleTitle
		  ,null --header
		  ,null --footer
		  ,@ModuleOrder
		  ,@PaneName
		  ,@CacheTime
		  ,@CacheMethod
		  ,@Alignment
		  ,@Color
		  ,@Border
		  ,@IconFile
		  ,@Visibility
		  ,@ContainerSrc
		  ,@DisplayTitle
		  ,@DisplayPrint
		  ,@DisplaySyndicate
		  ,@IsWebSlice
		  ,@WebSliceTitle
		  ,@WebSliceExpiryDate
		  ,@WebSliceTTL
		  ,@UniqueId
		  ,@VersionGuid
		  ,@DefaultLanguageGuid
		  ,@LocalizedVersionGuid
		  ,@CultureCode
		  ,@CreatedByUserID

		IF EXISTS (SELECT TabVersionId FROM TabVersions WHERE TabId = @CurrentTab) BEGIN
		
		DECLARE @Id int = NULL
		DECLARE @TabVersionId int = (SELECT TOP 1 TabVersionId FROM TabVersions WHERE TabId = @CurrentTab ORDER BY CreatedOnDate DESC)
		DECLARE @ModuleVersion int = (SELECT TOP 1 ModuleVersion FROM TabVersionDetails WHERE TabVersionId = @MasterTabVersion)
		DECLARE @Action int = 0 -- add module
		DECLARE @LastModifiedByUserID int = -1 -- null user

		EXECUTE @RC = [dbo].[SaveTabVersionDetail] 
		   @Id
		  ,@TabVersionId
		  ,@MasterModuleId
		  ,@ModuleVersion
		  ,@PaneName
		  ,@ModuleOrder
		  ,@Action
		  ,@CreatedByUserID
		  ,@LastModifiedByUserID
		
		END

      FETCH NEXT FROM db_cursor INTO @CurrentTab
END 

CLOSE db_cursor  
DEALLOCATE db_cursor 