SQL 如何将表中的列作为游标语句列出

发布于 2024-11-05 00:20:21 字数 683 浏览 0 评论 0原文

下面是我能够从数据库中提取表名的唯一方法,将顶部的 1 替换为 2、3、4.....499、500。

问题是没有直接访问对于数据库,必须使用第三方程序(对于律师事务所)来访问数据,这使得可以执行的操作范围有限,因此通常的取回数据的方法通常不起作用。根据使用“for xml auto”时的错误输出,数据似乎是使用游标返回的。

select(
(select min(name) from 
(select top 1 name from sys.Tables order by name desc)
as ax) + ', ' + 
(select min(name) from 
(select top 2 name from sys.Tables order by name desc)
as ax)  + ', ' + 

)

现在我希望重复此过程以返回表中的第二个、第三个、第四个column_name。下面的内容可以检索第一个column_name

SELECT TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = <table name>

,但我尝试重复第一个过程失败了; top 2、top 3 等返回相同的column_name。

帮助会很棒。

谢谢。

below is the only method by which i have been able to pull the table names out of our database, replacing the top 1 for 2, 3, 4..... 499, 500.

The problem is that there isn't direct access to the database, the use of a third party program (for law firms) has to access the data and this gives limited scope as to what can be done, thus the usual methods of bringing back data won't generally work. Data seems to be returned using cursors based on the errors output when using "for xml auto".

select(
(select min(name) from 
(select top 1 name from sys.Tables order by name desc)
as ax) + ', ' + 
(select min(name) from 
(select top 2 name from sys.Tables order by name desc)
as ax)  + ', ' + 

)

now i wish to repeat this procedure to return the second, third, fourth column_name within a table. THe below could works to retrive the first column_name

SELECT TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.Columns where TABLE_NAME = <table name>

but my attempts to repeat the first procedure fails; top 2, top 3 etc returned the same column_name.

Help would be great.

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

写下不归期 2024-11-12 00:20:21

尝试将 for xml auto 查询嵌入到另一个 select 语句中,

select (select table_name, column_name, ordinal_position, data_type
        from information_schema.columns
        where table_name = 'T'
        order by 1,3
        for xml auto)

在游标中使用它效果很好:

declare C cursor for
select (select table_name, column_name, ordinal_position, data_type
        from information_schema.columns
        where table_name = ''
        order by 1,3
        for xml auto)
open C
declare @L varchar(max)
fetch next from C into @L
close C
deallocate C
select @L

Try to embed the for xml auto query in another select statement

select (select table_name, column_name, ordinal_position, data_type
        from information_schema.columns
        where table_name = 'T'
        order by 1,3
        for xml auto)

Using this in a cursors works fine:

declare C cursor for
select (select table_name, column_name, ordinal_position, data_type
        from information_schema.columns
        where table_name = ''
        order by 1,3
        for xml auto)
open C
declare @L varchar(max)
fetch next from C into @L
close C
deallocate C
select @L
终难遇 2024-11-12 00:20:21

这应该会为您带来所需的所有信息(在 SQL Server 2005 上测试)

select table_name, column_name, ordinal_position, data_type
from information_schema.columns
where table_name = ''
order by 1,3
for xml auto

This should bring you all the information you require (tested on SQL Server 2005)

select table_name, column_name, ordinal_position, data_type
from information_schema.columns
where table_name = ''
order by 1,3
for xml auto
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文