且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动创建具有多个列的SQL表

更新时间:2022-10-22 18:09:53

至少可以生成 ALTER TABLE 脚本,然后运行这些脚本。

  DECLARE @COUNTER INT = 1 
WHILE @COUNTER< 10
BEGIN
PRINT'ALTER TABLE table_name ADD N'+ RIGHT('00'+ CONVERT(NVARCHAR(4),@COUNTER),2)+'bit'
SET @COUNTER + = 1
END


I must create an SQL table with 90+ fields, the majority of them are bit fields like N01, N02, N03 ... N89, N90 is there a fast way of creating multiple fileds or is it possible to have one single field to contain an array of values true/false? I need a solution that can also easily be queried.

At least you can generate ALTER TABLE scripts for bit fields, and then run those scripts.

DECLARE @COUNTER INT = 1
WHILE @COUNTER < 10
BEGIN
    PRINT 'ALTER TABLE table_name ADD N' + RIGHT('00' + CONVERT(NVARCHAR(4), @COUNTER), 2) + ' bit'
    SET @COUNTER += 1
END