是否可以使用列序号位置选择sql server数据

发布于 2024-07-10 12:52:46 字数 345 浏览 9 评论 0原文

是否可以使用表列的ordinal_position 选择列数据? 我知道使用序数位置是一种不好的做法,但对于一次性数据导入过程,我需要能够使用序数位置来获取列数据。

例如,

create table Test(
    Col1 int,
    Col2 nvarchar(10)

)

而不是使用

select Col2 from Test

我可以写

select "2" from Test -- for illustration purposes only

Is it possible to select column data using the ordinal_position for a table column? I know using ordinal positions is a bad practice but for a one-off data import process I need to be able to use the ordinal position to get the column data.

So for example

create table Test(
    Col1 int,
    Col2 nvarchar(10)

)

instead of using

select Col2 from Test

can I write

select "2" from Test -- for illustration purposes only

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

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

发布评论

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

评论(10

我的鱼塘能养鲲 2024-07-17 12:52:46

如果您知道列的数量,但不知道其名称和类型,您可以使用以下技巧:

select NULL as C1, NULL as C2 where 1 = 0 
-- Returns empty table with predefined column names
union all
select * from Test 
-- There should be exactly 2 columns, but names and data type doesn't matter

结果,您将拥有一个包含 2 列 [C1] 和 [C2] 的表。
如果表中有 100 列,则此方法不是很有用,但对于预定义列数较少的表效果很好。

If you know quantity of columns, but don't know its names and types, you can use following trick:

select NULL as C1, NULL as C2 where 1 = 0 
-- Returns empty table with predefined column names
union all
select * from Test 
-- There should be exactly 2 columns, but names and data type doesn't matter

As a result, you will have a table with 2 columns [C1] and [C2].
This method is not very usefull if you have 100 columns in your table, but it works well for tables with small predefined number of columns.

べ繥欢鉨o。 2024-07-17 12:52:46

你必须做类似的事情

declare @col1 as varchar(128)
declare @col2 as varchar(128)
declare @sq1 as varchar(8000) 

select @col1 = column_name from information_schema.columns where table_name = 'tablename'
and ordinal_position = @position

select @col2 = column_name from information_schema.columns where table_name = 'tablename'
and ordinal_position = @position2

set @sql = 'select ' + col1 ',' + col2 'from tablename' 

exec(@sql)

You'd have to do something like

declare @col1 as varchar(128)
declare @col2 as varchar(128)
declare @sq1 as varchar(8000) 

select @col1 = column_name from information_schema.columns where table_name = 'tablename'
and ordinal_position = @position

select @col2 = column_name from information_schema.columns where table_name = 'tablename'
and ordinal_position = @position2

set @sql = 'select ' + col1 ',' + col2 'from tablename' 

exec(@sql)
夜巴黎 2024-07-17 12:52:46

您可以使用此查询

select * from information_schema.columns

来获取列的顺序位置。 就像 Michael Haren 所写的那样,您必须使用它来构建动态查询,无论是在代码中还是在将列位置传递到的存储过程中。

FWIW,这纯粹是邪恶的。

另外,deathofrats 是对的,你不能真正做到这一点,因为你将根据位置构建一个带有列名称的常规查询。

You can use this query

select * from information_schema.columns

to get the ordinal positions of the columns. Like Michael Haren wrote, you'll have to build a dynamic query using this, either in code or in a sproc that you pass the column positions to.

FWIW, this is pure evil.

Also, deathofrats is right, you can't really do this, since you'll be building a regular query w/ column names based on position.

浊酒尽余欢 2024-07-17 12:52:46

是的,您可以通过对系统表进行一些非常丑陋的点击来做到这一点。 您可能需要进入动态 sql 的世界。

我真的非常不推荐这种方法。

如果这没有阻止您,那么这可能会让您开始(ref ):

select table_name, column_name, ordinal_position, data_type
from information_schema.columns
order by 1,3

Yes, you could do this with some really ugly hits into the system tables. You'd probably need to fall into the world of dynamic sql.

I really, really do not recommend this approach.

If that didn't deter you, then this might get you started (ref):

select table_name, column_name, ordinal_position, data_type
from information_schema.columns
order by 1,3
横笛休吹塞上声 2024-07-17 12:52:46

不,据我所知,您不能根据列的顺序位置来选择列。

当查看transact SQL参考时,没有什么建议你可以
(http://msdn.microsoft.com/en- us/library/ms176104(SQL.90).aspx)。

No, you can't select columns based on their ordinal position, as far as I know.

When looking at the transact SQL reference, there is nothing to suggest you can
(http://msdn.microsoft.com/en-us/library/ms176104(SQL.90).aspx).

空宴 2024-07-17 12:52:46

如果您知道列数,一种方法可能是将数据传输到具有该列数的临时表中,并从临时表中进行选择...

declare @tmp table(field1 sql_variant, field2 int, field3 sql_variant)

insert into @tmp
select * from Test

select field2 from @tmp

If you know the number of columns, one way might be to transfer the data into a tmp table with that number of columns and select from the temp table...

declare @tmp table(field1 sql_variant, field2 int, field3 sql_variant)

insert into @tmp
select * from Test

select field2 from @tmp
枕花眠 2024-07-17 12:52:46

您可以选择使用条件:
在你的例子中:

    SELECT 
     CASE YourColumnNumber 
      WHEN "1" THEN Col1
      WHEN "2" THEN Col2
      ELSE "?"
     END AS Result
    FROM Test

恐怕进入模式会减慢查询速度......

An option you have is using conditions:
In your example:

    SELECT 
     CASE YourColumnNumber 
      WHEN "1" THEN Col1
      WHEN "2" THEN Col2
      ELSE "?"
     END AS Result
    FROM Test

Going to schema will slow query I am afraid...

从﹋此江山别 2024-07-17 12:52:46

我认为你不能。 正如 @Michael Haren 所示,您可以在 ORDER BY 子句中使用序数位置,但我从未见过它们在 SQL Server 中的其他地方使用过。

我不确定您的一次性数据导入会遇到什么问题,这会有所帮助 - 可能是一些不幸的列名称? 你能再解释一下吗?

I don't think you can. As @Michael Haren showed, you can use ordinal positions in ORDER BY clauses but I've never seen them used elsewhere in SQL Server.

I'm not sure what problem you are havng with your one-off data import that this would help with - presumably some unfortunate column name? Can you explain a little more?

甜嗑 2024-07-17 12:52:46

除了使用动态 SQL 之外,我不知道还有什么方法可以做到这一点。 也许如果您提供更多关于为什么您认为必须使用序数值的信息,这里的某人可以为您提供有关如何解决该问题的建议。

编辑:我看到你在另一条评论中在某种程度上回答了这个问题。 您能提供更多具体信息吗? 导入过程和/或表定义?

I don't know of any way to do this short of using dynamic SQL. Maybe if you include a bit more information about why you feel you have to use the ordinal values someone on here can give you advice on how to get around that problem.

EDIT: I see that you answered this to some degree in another comment. Can you provide more specifics? The import proc and/or table definitions?

轻许诺言 2024-07-17 12:52:46

如果您使用的是 MS SQL 2005,则可以使用 ROW_NUMBER 函数。

选择 Col1、Col2、ROW_NUMBER() OVER(ORDER BY Col1)
来自测试
WHERE ROW_NUMBER() Over(Order BY Col1) Between @Position AND @Position

如果我正确阅读问题,这应该会得到您想要的结果。

If you are using MS SQL 2005 you can use the ROW_NUMBER function.

SELECT Col1, Col2, ROW_NUMBER() OVER(ORDER BY Col1)
FROM Test
WHERE ROW_NUMBER() Over(Order BY Col1) Between @Position AND @Position

That should get you the desired results if I am reading the question correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文