• It has to be said: this is a very weird requirement. I would be interested to know why you are doing things this way – are you able to provide some background? But here goes ...

    Building on my earlier code:

    if object_id('dbo.tab1', 'U') is not null

    drop table dbo.tab1;

    if object_id('dbo.tab2', 'U') is not null

    drop table dbo.tab2;

    create table dbo.tab1

    (

    a int primary key

    ,b text

    );

    insert dbo.tab1

    (a, b)

    values (1, 'Pluto'),

    (2, 'Pippo'),

    (3, 'Rossi');

    select *

    from dbo.tab1 t;

    declare @sql nvarchar(max) = '';

    select @sql = concat(@sql, ',', b, ' int')

    from dbo.tab1 t;

    set @sql = stuff(@sql, 1, 1, ' create table dbo.tab2 (') + ')';

    exec sys.sp_executesql

    @sql;

    --Add another row to tab1

    insert dbo.tab1

    (a, b)

    values (4, 'Spock');

    --Create the ALTER statement

    set @sql = '';

    select @sql = concat(@sql, ',', b, ' int')

    from dbo.tab1 t

    where not exists ( select 1

    from sys.columns c

    where c.object_id = object_id('dbo.tab2')

    and c.name = cast(t.b as sysname) );

    set @sql = stuff(@sql, 1, 1, 'alter table dbo.tab2 add ');

    --Execute the ALTER

    exec sys.sp_executesql

    @sql;

    select *

    from dbo.tab2;

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.