--Chain Alim (143, 303, 302)
--Chain Não Aliment (401, 306, 600, 330, 740, 500, 301)
DECLARE @ChainId varchar(18) = '306'
DECLARE @RetailStoreId varchar(5)
DECLARE @RetailStoreNumber int
DECLARE @LabelId bigint
DECLARE @LabelRetailStore_Count int
SET @RetailStoreNumber = 0
SELECT TOP 1 @RetailStoreId = RetailStoreId
FROM ra.RetailStore
WHERE LEFT(RetailStoreId, 1) = 'S' AND ChainId = @ChainId
ORDER BY CONVERT(int, REPLACE(RetailStoreId, 'S', ''))
WHILE (@RetailStoreId <> '')
BEGIN
SET @RetailStoreId = ''
SELECT TOP 1 @RetailStoreId = RetailStoreId
FROM ra.RetailStore
WHERE LEFT(RetailStoreId, 1) = 'S' AND ChainId = @ChainId
AND CONVERT(int, REPLACE(RetailStoreId, 'S', '')) > @RetailStoreNumber
ORDER BY CONVERT(int, REPLACE(RetailStoreId, 'S', ''))
IF (@RetailStoreId <> '')
BEGIN
DECLARE CUR_Label CURSOR FOR
SELECT LabelId FROM ra.Label WHERE LabelId IN (
89
)
OPEN CUR_Label
FETCH CUR_Label INTO @LabelId
WHILE @@FETCH_STATUS=0
BEGIN
IF (@LabelId > 0)
BEGIN
SELECT @LabelRetailStore_Count = COUNT(1)
FROM ra.LabelRetailStore
WHERE RetailStoreId = @RetailStoreId AND LabelId = @LabelId
IF (@LabelRetailStore_Count = 0)
INSERT INTO ra.LabelRetailStore (RetailStoreId, LabelId, LastUpdateUser, LastUpdateDate, IsMarkdownDefault)
VALUES (@RetailStoreId, @LabelId, 'MRS', GETDATE(), 0)
END
FETCH CUR_Label INTO @LabelId
END
SET @RetailStoreNumber = CONVERT(int, REPLACE(@RetailStoreId, 'S', ''))
CLOSE CUR_Label
DEALLOCATE CUR_Label
END
END