MSSQL loop over table

Date: 2020-12-03
DELETE FROM [CompanySetting]; -- empty table

DECLARE @CompanyId uniqueidentifier;         
-- SELECT TOP 1 @CompanyId = [Id] FROM [Company];

DECLARE CompanyCursor CURSOR FOR SELECT [Id] FROM [Company];
OPEN CompanyCursor 
FETCH NEXT FROM CompanyCursor INTO @CompanyId

WHILE @@FETCH_STATUS = 0 
BEGIN 
	insert into [CompanySetting] values
	(newId(), @CompanyId, 'LdapUrl', 'ldap-server'),
	(newId(), @CompanyId, 'LdapPort', '389'),
	(newId(), @CompanyId, 'LdapUseSsl', 'false');
	FETCH NEXT FROM CompanyCursor INTO @CompanyId
END

CLOSE CompanyCursor
DEALLOCATE CompanyCursor
GO
43460cookie-checkMSSQL loop over table