SELECT OBJECT_NAME(OBJECT_ID)如何工作?

时间:2021-03-25 20:21:41

[A beginner question, after googling for an answer to this simple question, without success:]

[一个初学者的问题,在谷歌搜索这个简单问题的答案后,没有成功:]

What's being done in the following query (taken from here, removed the DISTINCT and added a column title):

在以下查询中执行了什么(从此处获取,删除了DISTINCT并添加了列标题):

USE AdventureWorks;
GO
SELECT OBJECT_NAME(object_id) AS ObjName
FROM master.sys.objects;
GO

I understand that it's a function, returning an object name for a given object id.

我知道它是一个函数,返回给定对象id的对象名称。

However, The argument for the SELECT command (i.e., OBJECT_NAME(object_id)) makes me confused, for 2 reasons:

但是,SELECT命令的参数(即OBJECT_NAME(object_id))让我感到困惑,原因有两个:

  1. I thought that the argument for SELECT is (well, usually) a column name, while here it is a value (within the name column of master.sys.objects).
  2. 我认为SELECT的参数是(通常)列名,而在这里它是一个值(在master.sys.objects的名称列中)。
  3. I don't understand the way object_id is used - it is not assigned with a value on the surface of the query; So my only guess is that it's there to be used under the surface in a similiar way to a foreach function (i.e., to use C# terms, foreach object_id, fill the returned datatable with the datarow) - but I can't be sure my guess is correct.
  4. 我不明白object_id的使用方式 - 它没有在查询表面上赋值;所以我唯一的猜测就是它在表面下以类似于foreach函数的方式使用(即使用C#术语,foreach object_id,用数据行填充返回的数据表) - 但我不能确定我的猜对了。

So, what is being done with the SELECT here?

那么,SELECT在这里做了什么?

EDIT: Following comments, I'll ask it from another angle:

编辑:以下评论,我会从另一个角度问:

Does OBJECT_NAME() refer differently to its argument, so that when it is given a specific id, it returns one value, and when given the name 'object_id` it returns the whole column?

OBJECT_NAME()是否以不同的方式引用其参数,因此当给定一个特定的id时,它返回一个值,当给定名称'object_id`时,它返回整列?

1 个解决方案

#1


4  

1) Yes, you are selecting a column name. You are selecting object_id from the sys.objects table. You are not interacting with the object_name at all from the table. As Aaron mentioned in his comment, the OBJECT_NAME() is just a function that returns a value/object_id. For example

1)是的,您正在选择列名称。您正在从sys.objects表中选择object_id。您根本没有与object_name进行交互。正如Aaron在评论中提到的,OBJECT_NAME()只是一个返回值/ object_id的函数。例如

select OBJECT_NAME(object_id) 
FROM (
select [object_id]
FROM sys.objects) x

This query returns the object_name of the objects without you referencing the object name from the objects table in either the select or the subquery.

此查询返回对象的object_name,而无需在select或子查询中的objects表中引用对象名称。

2)Object_ID is an indentifier for objects in your database. These objects can be tables, triggers, stored procedures, etc.

2)Object_ID是数据库中对象的标识符。这些对象可以是表,触发器,存储过程等。

You can use either a static value or query a value from a table:

您可以使用静态值或查询表中的值:

select OBJECT_NAME(1673317271)

select OBJECT_NAME(object_id)
FROM sys.objects
WHERE object_ID = 1673317271

#1


4  

1) Yes, you are selecting a column name. You are selecting object_id from the sys.objects table. You are not interacting with the object_name at all from the table. As Aaron mentioned in his comment, the OBJECT_NAME() is just a function that returns a value/object_id. For example

1)是的,您正在选择列名称。您正在从sys.objects表中选择object_id。您根本没有与object_name进行交互。正如Aaron在评论中提到的,OBJECT_NAME()只是一个返回值/ object_id的函数。例如

select OBJECT_NAME(object_id) 
FROM (
select [object_id]
FROM sys.objects) x

This query returns the object_name of the objects without you referencing the object name from the objects table in either the select or the subquery.

此查询返回对象的object_name,而无需在select或子查询中的objects表中引用对象名称。

2)Object_ID is an indentifier for objects in your database. These objects can be tables, triggers, stored procedures, etc.

2)Object_ID是数据库中对象的标识符。这些对象可以是表,触发器,存储过程等。

You can use either a static value or query a value from a table:

您可以使用静态值或查询表中的值:

select OBJECT_NAME(1673317271)

select OBJECT_NAME(object_id)
FROM sys.objects
WHERE object_ID = 1673317271