是否可以使用列序号位置选择sql server数据
是否可以使用表列的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您知道列的数量,但不知道其名称和类型,您可以使用以下技巧:
结果,您将拥有一个包含 2 列 [C1] 和 [C2] 的表。
如果表中有 100 列,则此方法不是很有用,但对于预定义列数较少的表效果很好。
If you know quantity of columns, but don't know its names and types, you can use following trick:
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.
你必须做类似的事情
You'd have to do something like
您可以使用此查询
来获取列的顺序位置。 就像 Michael Haren 所写的那样,您必须使用它来构建动态查询,无论是在代码中还是在将列位置传递到的存储过程中。
FWIW,这纯粹是邪恶的。
另外,deathofrats 是对的,你不能真正做到这一点,因为你将根据位置构建一个带有列名称的常规查询。
You can use this query
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.
是的,您可以通过对系统表进行一些非常丑陋的点击来做到这一点。 您可能需要进入动态 sql 的世界。
我真的非常不推荐这种方法。
如果这没有阻止您,那么这可能会让您开始(ref ):
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):
不,据我所知,您不能根据列的顺序位置来选择列。
当查看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).
如果您知道列数,一种方法可能是将数据传输到具有该列数的临时表中,并从临时表中进行选择...
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...
您可以选择使用条件:
在你的例子中:
恐怕进入模式会减慢查询速度......
An option you have is using conditions:
In your example:
Going to schema will slow query I am afraid...
我认为你不能。 正如 @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?
除了使用动态 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?
如果您使用的是 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.