這不是很清楚究竟你需要的,但看看到SQL:
create table test_columns
(
coso1 char(3),
c45 int,
coso2 char(3),
b63 int
);
insert into test_columns values ('b63', 1, 'd28', 5)
DECLARE @column_id INT,
@name varchar(200),
@query varchar(max),
@next_column varchar(200)
DECLARE @cursor CURSOR
SET @cursor = CURSOR FOR
select name, column_id from sys.columns where object_name(object_id) = 'test_columns' and name like 'coso%'
OPEN @cursor
FETCH NEXT
FROM @cursor INTO @name, @column_id
WHILE @@FETCH_STATUS = 0
BEGIN
SET @query = 'SELECT 1 FROM dbo.test_columns HAVING COUNT(distinct ' + @name + ') = 1';
EXEC (@query);
IF (@@ROWCOUNT = 1)
BEGIN
SELECT @next_column = name FROM sys.columns where object_name(object_id) = 'test_columns' and column_id = @column_id + 1;
SET @query = 'SELECT ' + @next_column + ', ' + @name + ' FROM dbo.test_columns';
EXEC (@query);
END
FETCH NEXT
FROM @cursor INTO @name, @column_id
END
CLOSE @cursor
DEALLOCATE @cursor
爲什麼列名更改?似乎是一個糟糕的主意。 – jarlh
以下是控制數據庫,添加新列和修改所有數據的人員,例如現在的列'b67',擁有'c45'的東西, – KMG104
下一個列名的確切定義是什麼? – gotqn