SQL 如何将表中的列作为游标语句列出
下面是我能够从数据库中提取表名的唯一方法,将顶部的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将
for xml auto
查询嵌入到另一个 select 语句中,在游标中使用它效果很好:
Try to embed the
for xml auto
query in another select statementUsing this in a cursors works fine:
这应该会为您带来所需的所有信息(在 SQL Server 2005 上测试)
This should bring you all the information you require (tested on SQL Server 2005)