CREATE PROCEDURE dynamic_tbl
@ColumnCount int=0
AS
BEGIN
Declare @ColumnName nvarchar(10)='col_'
Declare @count int =1
IF(@ColumnCount>0)
BEGIN
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'dyn_tbl'))
BEGIN
DROP TABLE dyn_tbl
END
CREATE TABLE [dbo].[dyn_tbl]
( col_1 varchar(20) )
SET @count=@count +1;
WHile(@ColumnCount>=@count)
BEGIN
SET @ColumnName='col_'+CONVERT(varchar(2),@count)
EXEC ('ALTER TABLE dyn_tbl ADD ['+@ColumnName +'] varchar(20)')
SET @count=@count +1;
END
END
END