0
--Dummy table
create table table1 (
column_order varchar (100)
)

insert into table1 values ('column1')
insert into table1 values ('column2')
insert into table1 values ('column3')
insert into table1 values ('column4')
insert into table1 values ('column5')
insert into table1 values ('column6')

--Start of select

declare @rowsCount INT
declare @i INT = 1
declare @column varchar(1000) = ''

set @rowsCount = (select COUNT(*) from table1)

while @i <= @rowsCount
begin
 set @column = @column + (select column_order from table1 where rowid(table1) = @i) + ', '
 set @i = @i + 1
end
select @column

此代码具有功能 ROWID,即 IQ-Sybase 功能,我不确定其他 DBMS 可以使用它。在上面你有一个例子,我希望我的选择看起来像什么。

我的问题是,您不能将 ROWID 函数与 sys.column 或任何其他 systables 一起使用。有谁知道如何在不使用 ROWID 函数的情况下获得与我相同的选择。

如果您使用的是 IQ,我构建了代码,因此您只需键入 f5 并查看 select 语句,然后删除虚拟表。

4

3 回答 3

1

使用列表()。它适用于 ASA 系统和 IQ 目录。

drop table if exists table1
go

create local temporary table table1 (
column_order varchar (100)
) in system --create table in system

insert into table1 values ('column1')
insert into table1 values ('column2')
insert into table1 values ('column3')
insert into table1 values ('column4')
insert into table1 values ('column5')
insert into table1 values ('column6')

declare @columns varchar(100)

select @columns = list(column_order) from table1

select @columns
go
于 2014-10-15T21:33:01.983 回答
0

我可能不明白你的需要,因为我看不出你为什么需要 rowdid。

通常,在 TSQL 中,我执行以下操作:

declare @someVar as nvarchar(max)

set @someVar = (select 
    '[' + c.name + '],' as 'data()'
from
    sys.columns c
    join sys.tables t on c.object_id = t.object_id
where
    t.name = 'SomeTableName'
for xml path(''))

print Left(@someVar, Len(@someVar) - 1)
于 2014-10-15T14:56:55.160 回答
0

也许您需要使用游标:

-- @MyTableID has to be definded somewhere or replace
DECLARE @columns varchar(32000)
DECLARE my_cursor CURSOR FOR
    SELECT syscolumn.column_name FROM syscolumn WHERE syscolumn.table_id = @MyTableID
OPEN my_cursor
FETCH NEXT my_cursor into @column
WHILE @@FETCH_STATUS = 0
    BEGIN
        -- put your magic here
        -- e.g.
        -- SET @columns = @columns + column
        FETCH NEXT my_cursor_pkey into @column
    END

CLOSE my_cursor
DEALLOCATE my_cursor

尚未测试,但类似的东西应该可以工作。

于 2014-10-15T17:07:41.030 回答