SQL循环添加表中的字段
程序员文章站
2022-05-07 20:17:33
USE BookDiscuss
DECLARE @num INT
DECLARE @numdiff INT
DECLARE @t...
USE BookDiscuss DECLARE @num INT DECLARE @numdiff INT DECLARE @table VARCHAR(50) DECLARE @sql VARCHAR(8000) SET @numdiff=3 SET @num=0 WHILE @num<@numdiff BEGIN SET @table='DiscussPost_'+CAST(@num AS VARCHAR(10)); SET @sql= 'ALTER TABLE '+@table+' ADD PostFatherId INT DEFAULT 0; ALTER TABLE '+@table+' ADD PostReplyUserId INT DEFAULT 0;' EXEC(@sql) SET @num=@num+1 END