[英]How to create just like array in SQL Server 2005/2000?
I know we can not create array in SQL Server. 我知道我们无法在SQL Server中创建数组。 There are 25 different tables in my database.
我的数据库中有25个不同的表。 I want to add extra column to those tables.
我想向这些表添加额外的列。 So I wanted to have an array so I can store my table name into.
所以我想有一个数组,以便可以将表名存储到其中。 One by one I can access them and insert new column.
我可以一一访问它们并插入新列。 In SQL Server there is no for loop, I can use while loop.
在SQL Server中没有for循环,我可以使用while循环。
You can easily store your table names into a table variable and then also have a Done
column in that table variable - something like: 您可以轻松地将表名存储到一个表变量中 ,然后在该表变量中还有一个
Done
列-类似于:
DECLARE @AllMyTables TABLE (TableName VARCHAR(200), Done BIT)
and then insert your tables into that table variable: 然后将表插入该表变量:
INSERT INTO @AllMyTables(TableName, Done)
SELECT name, 0
FROM sys.tables
Now, you can easily fetch one table at a time and do something with it: 现在,您可以轻松地一次获取一个表并对其进行处理:
DECLARE @TableName VARCHAR(200)
SELECT TOP (1) @TableName = TableName FROM @AllMyTables WHERE Done = 0
and now add your column, update the Done
column to indicate you're done, and on you go.... 然后添加您的列,更新
Done
列以表明您已完成,然后就可以继续...。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.