last day (14 days later) » 

7:49 AM
0
Q: How SELECT OBJECT_NAME(OBJECT_ID) works?

HeyJude[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): USE AdventureWorks; GO SELECT OBJECT_NAME(object_id) AS ObjName FROM master.sys.objects; GO ...

 
SELECT list can contain columns, expressions, constants... this is an expression made up of a function that takes one of the table columns as input. You can also do things like SELECT OBJECT_NAME(4); - without referencing any table at all. I'm not sure why you think of any individual item in the select list as a "record"... Try changing your query to SELECT object_id, object_id + 10, OBJECT_NAME(object_id) FROM master.sys.objects - should make it clearer that object_id is just a name of one of the columns in sys.objects.
 
I refer to a 'record' since the description of OBJECT_NAME() says "Returns the database object name", and an "object name" is one record in the name column. Anyway, I see what your'e saying, but I'm still left with some confusion - I'll edit my question.
 
I don't know why "an object name is one record in the name column"... a column doesn't contain rows, it contains values in each row.
 
Value it is, then.
 
Well, it's a big difference. Semantics are important, I wanted to make sure I understand what you meant. :-)
 
7:49 AM
I agree (editted), wasn't aware to the fact that a 'record' is a word reserved only for a row.
 
There some people out there (like Joe Celko) who argue that a record doesn't exist in sql because we have rows in sql. Records are a legacy concept but a name that has stood the test of time.
 
The OBJECT_NAME() function here takes a different object_id from each row, and returns the name associated with that object_id (there can be only one). Of course this doesn't accomplish anything here, since you could have just used name instead of OBJECT_NAME([object_id]), but it can be useful in other cases (e.g. catalog views or DMVs that contain only the object_id and not the name). I'm still not sure what you mean by "the whole column"...
@Sean I happen to agree with Joe, but I've become less of an idiot about it. I mean, I don't pretend to not know what people mean when they say record or field instead of row or column, and I don't harp on it. But it is a point I call out whenever I'm teaching new people about databases.
 
@AaronBertrand I am in the same camp as you. I don't call them records and try to help new people to strike that from their vocabulary. But as we all know Joe is impossible to deal with online.
 
@AaronBertrand, "takes a different object_id from each row, and returns the name associated with that object_id" - bottom line, it returns the whole values of the name column, and that's what I mean by saying the "whole column". So, it seems to me I was correct with my foreach analogy, right?
 
The function doesn't technically return the value from the name column, it has its own mechanism to go and get the name. It is convenient here because that happens to match the name column, but not go run the same query against sys.columns. OBJECT_NAME(object_id) is not going to give you the same value as the name column, because that column refers to the name of the column, not the object. You should think of a table as a set, not something like a foreach.
 
7:49 AM
I lost you...Can you paraphrase your last comment? Especially this sentence: "OBJECT_NAME(object_id) is not going to give you the same value as the name column" - But it does... Also, I refer the table filling as done by some equivalence to foreach - Is that wrong?
 
@HeyJude: It does for sys.objects, but it doesn't for sys.columns.
 
@AndriyM, Can you elaborate?
 
@HeyJude: In sys.columns, name refers to the name of the column, while object_id refers to the object containing the column. Consequently, getting OBJECT_NAME() from object_id in sys.columns would give you the name of that object. That's how it wouldn't match sys.columns.name. That was all in Aaron's comment, actually.
 
@AndriyM, of course...thanks. Anyway, I'm still left unanswered about how OBJECT_NAME(object_id) works. Maybe you can shed some light (As an answer to this post or to my last coment to dfundko's answer).
 
I'm not sure how many other ways we can use the English language to describe this. OBJECT_NAME() takes an integer as input (object_id) and, if there is an object with that id, it returns the name, otherwise it returns NULL. The function works the same whether you use it on its own or as part of a query, whether you pass in a variable or constant or column name, etc. What exact part of "works" do you still struggle with here?
 
7:49 AM
Are you asking specifically about OBJECT_NAME(object_id) or are you asking about FUNCTION_NAME(column_name) in general?
 
@AaronBertrand, I see why OBJECT_NAME(4) is a function taking an integer. But OBJECT_NAME(object_id) is a function taking a string (a column name) - and in that case, it seems to work differently (namely, returning the entire column values). So I guess the function treats these different inputs differently, and I'm trying to verify it, that's all.
@AndriyM, I now understand it is about the general case.
 
OBJECT_NAME(object_id) is NOT taking a string. Look at the definition of sys.objects - object_id is an integer. If it were being used as a string, it would be OBJECT_NAME('object_id') and that wouldn't work for multiple reasons. I'm still not sure you know what you mean by "entire column values"...
 
@AaronBertrand, then what is the integer value of object_id in my snippet query? It is not something declared before or as part of the query, and we thus have to suplly the query with a FROM clause. Thus, my conclusion is that it is assigned with an integer (i.e., each columnXrow value) as part of the function implementation. Compare that to a case when we do pass an integer - we don't have to write a FROM clause (and doing that will result in a one-column table filled with the same value)
 
Try SELECT object_id FROM sys.objects. See those values in the object_id column? Those are the values that get passed to the function in order to return the name, and they definitely are coming from the query.
 
@AaronBertrand, of course. But doesn't that mean the function can get not only an integer, but also a 'set' of integers (a column, in this case)? When calling the function with object_id, The caller itself doesn't pass an integer; The integers passing is being taking care as part of the function implementation.
 
7:49 AM
Ok, I'm not following, but if that clears it up for you..
 

last day (14 days later) »