Metadata OBJECT_NAME SQL function


1. Usage of OBJECT_NAME

SQL Server Query 1

            
 SELECT DISTINCT OBJECT_NAME(object_id) AS Schema_Name   
FROM master.sys.objects
UNION ALL
SELECT DISTINCT OBJECT_NAME(object_id, 1) AS Schema_Name
FROM master.sys.objects;

Create SQL query with SqlQueryBuilder 1

            
 var (sql1, parameters1) = new SqlQueryBuilder()  
.Select().Distinct()
.Column(new OBJECT_NAME(new Column("object_id")), "Schema_Name")
.From("master.sys.objects")
.UnionAll(new SqlQueryBuilder()
.Select().Distinct()
.Column(new OBJECT_NAME(new Column("object_id"), 1), "Schema_Name")
.From("master.sys.objects")
)
.Build();

Query build by SqlQueryBuilder 1

            
SELECT DISTINCT OBJECT_NAME(object_id) AS Schema_Name
FROM master.sys.objects
UNION ALL
SELECT DISTINCT OBJECT_NAME(object_id, @pMAIN_2606200152358429230) AS Schema_Name
FROM master.sys.objects;


            
        

Parameters (If used)

Name Value
@pMAIN_2606200152358429230 1

Query Results 1:

  Schema_Name
1
2 spt_fallback_db
3 spt_fallback_dev
4 spt_fallback_usg
5 spt_monitor
6 spt_values


2. Usage of OBJECT_NAME

SQL Server Query 2

            
 SELECT QUOTENAME(DB_NAME(db_id()))     
+ N'.'
+ QUOTENAME(OBJECT_NAME(object_id, db_id())) AS Full_Name
FROM sys.objects;

Create SQL query with SqlQueryBuilder 2

            
 var (sql2, parameters2) = new SqlQueryBuilder()  
.Select()
.Column(new CONCAT(new QUOTENAME(new DB_NAME(new DB_ID())),
".",
new QUOTENAME(new OBJECT_NAME(new Column("object_id"), new DB_ID()))), "Full_Name")
.From("sys.objects")
.Build();

Query build by SqlQueryBuilder 2

            
SELECT CONCAT(QUOTENAME(DB_NAME(DB_ID()), @pMAIN_2606200152358925740), @pMAIN_2606200152358925741, QUOTENAME(OBJECT_NAME(object_id, DB_ID()), @pMAIN_2606200152358925742)) AS Full_Name
FROM sys.objects;


            
        

Parameters (If used)

Name Value
@pMAIN_2606200152358925740 [
@pMAIN_2606200152358925741 .
@pMAIN_2606200152358925742 [

Query Results 2:

  Full_Name
1 [TheBlueSoftDB].[PK_Categories]
2 [TheBlueSoftDB].[Countries]
3 [TheBlueSoftDB].[Products]
4 [TheBlueSoftDB].[PK_Countries]
5 [TheBlueSoftDB].[PK_Products]
6 [TheBlueSoftDB].[DF_Countries_EnteredOn]
7 [TheBlueSoftDB].[Alphabetical list of products]
8 [TheBlueSoftDB].[States]
9 [TheBlueSoftDB].[Current Product List]
10 [TheBlueSoftDB].[PK_States]
11 [TheBlueSoftDB].[Course_Master]
12 [TheBlueSoftDB].[Orders]
13 [TheBlueSoftDB].[DF_States_EnteredOn]
14 [TheBlueSoftDB].[PK_Course_Master]
15 [TheBlueSoftDB].[PK_Orders]
16 [TheBlueSoftDB].[DF__Countries__IsAct__061CD093]
17 [TheBlueSoftDB].[CustomerProductSalesIndexed]
18 [TheBlueSoftDB].[Tab_Desc]
19 [TheBlueSoftDB].[Orders Qry]
20 [TheBlueSoftDB].[DF__States__IsActive__0710F4CC]
21 [TheBlueSoftDB].[PK_Tab_Desc]
22 [TheBlueSoftDB].[Products Above Average Price]
23 [TheBlueSoftDB].[Tab_Headers]
24 [TheBlueSoftDB].[Products by Category]
25 [TheBlueSoftDB].[PK_Tab_Headers]
26 [TheBlueSoftDB].[Quarterly Orders]
27 [TheBlueSoftDB].[DF_Course_Master_Entered_On]
28 [TheBlueSoftDB].[Employees]
29 [TheBlueSoftDB].[DF_Tab_Desc_Sequence]
30 [TheBlueSoftDB].[PK_Employees]
31 [TheBlueSoftDB].[DF_Tab_Desc_EnteredOn]
32 [TheBlueSoftDB].[Shippers]
33 [TheBlueSoftDB].[DF_Tab_Headers_Sequence]
34 [TheBlueSoftDB].[PK_Shippers]
35 [TheBlueSoftDB].[user_feedbacks]
36 [TheBlueSoftDB].[DF_Tab_Headers_Entered_On]
37 [TheBlueSoftDB].[Order Details]
38 [TheBlueSoftDB].[PK_user_feedbacks]
39 [TheBlueSoftDB].[FK_Tab_Desc_Tab_Headers]
40 [TheBlueSoftDB].[PK_Order_Details]
41 [TheBlueSoftDB].[DF_user_feedbacks_entered_on]
42 [TheBlueSoftDB].[FK_Tab_Headers_Course_Master]
43 [TheBlueSoftDB].[Invoices]
44 [TheBlueSoftDB].[DF__Course_Ma__Page___11246EEB]
45 [TheBlueSoftDB].[Order Details Extended]
46 [TheBlueSoftDB].[Order Subtotals]
47 [TheBlueSoftDB].[Batch_Master]
48 [TheBlueSoftDB].[Product Sales for 1997]
49 [TheBlueSoftDB].[PK_Batches]
50 [TheBlueSoftDB].[Category Sales for 1997]
51 [TheBlueSoftDB].[Class_Master]
52 [TheBlueSoftDB].[Sales by Category]
53 [TheBlueSoftDB].[PK_Class_Master]
54 [TheBlueSoftDB].[Sales Totals by Amount]
55 [TheBlueSoftDB].[Summary of Sales by Quarter]
56 [TheBlueSoftDB].[Summary of Sales by Year]
57 [TheBlueSoftDB].[DF_Batches_Is_Active]
58 [TheBlueSoftDB].[CustomerCustomerDemo]
59 [TheBlueSoftDB].[DF_Batches_Entered_On]
60 [TheBlueSoftDB].[OnLine_Links]
61 [TheBlueSoftDB].[CustomerDemographics]
62 [TheBlueSoftDB].[DF_Class_Master_Entered_On]
63 [TheBlueSoftDB].[DF_OnLine_Links_Id]
64 [TheBlueSoftDB].[EmployeeTerritories]
65 [TheBlueSoftDB].[DF_OnLine_Links_Sequence]
66 [TheBlueSoftDB].[Region]
67 [TheBlueSoftDB].[DF_OnLine_Links_Entered_On]
68 [TheBlueSoftDB].[Territories]
69 [TheBlueSoftDB].[PK_OnLine_Links]
70 [TheBlueSoftDB].[PK_CustomerCustomerDemo]
71 [TheBlueSoftDB].[PK_CustomerDemographics]
72 [TheBlueSoftDB].[FK_Batches_Course_Master]
73 [TheBlueSoftDB].[PK_EmployeeTerritories]
74 [TheBlueSoftDB].[FK_Class_Master_Batch_Master]
75 [TheBlueSoftDB].[PK_Region]
76 [TheBlueSoftDB].[PK_Territories]
77 [TheBlueSoftDB].[DF_Order_Details_UnitPrice]
78 [TheBlueSoftDB].[DF_Order_Details_Quantity]
79 [TheBlueSoftDB].[DF_Order_Details_Discount]
80 [TheBlueSoftDB].[DF_Orders_Freight]
81 [TheBlueSoftDB].[DF_Products_UnitPrice]
82 [TheBlueSoftDB].[DF_Products_UnitsInStock]
83 [TheBlueSoftDB].[DF_Products_UnitsOnOrder]
84 [TheBlueSoftDB].[DF_Products_ReorderLevel]
85 [TheBlueSoftDB].[DF_Products_Discontinued]
86 [TheBlueSoftDB].[FK_CustomerCustomerDemo]
87 [TheBlueSoftDB].[FK_CustomerCustomerDemo_Customers]
88 [TheBlueSoftDB].[FK_Employees_Employees]
89 [TheBlueSoftDB].[FK_EmployeeTerritories_Employees]
90 [TheBlueSoftDB].[FK_EmployeeTerritories_Territories]
91 [TheBlueSoftDB].[FK_Order_Details_Orders]
92 [TheBlueSoftDB].[FK_Order_Details_Products]
93 [TheBlueSoftDB].[FK_Orders_Customers]
94 [TheBlueSoftDB].[FK_Orders_Employees]
95 [TheBlueSoftDB].[FK_Orders_Shippers]
96 [TheBlueSoftDB].[User_Emails]
97 [TheBlueSoftDB].[FK_Products_Categories]
98 [TheBlueSoftDB].[PK_User_Emails]
99 [TheBlueSoftDB].[FK_Products_Suppliers]
100 [TheBlueSoftDB].[DF_User_Emails_Id]
101 [TheBlueSoftDB].[FK_Territories_Region]
102 [TheBlueSoftDB].[DF_User_Emails_Email_Type]
103 [TheBlueSoftDB].[CK_Birthdate]
104 [TheBlueSoftDB].[DF_User_Emails_Sent]
105 [TheBlueSoftDB].[CK_Discount]
106 [TheBlueSoftDB].[DF_User_Emails_Entered_On]
107 [TheBlueSoftDB].[CK_Quantity]
108 [TheBlueSoftDB].[CK_UnitPrice]
109 [TheBlueSoftDB].[CK_Products_UnitPrice]
110 [TheBlueSoftDB].[CK_ReorderLevel]
111 [TheBlueSoftDB].[CK_UnitsInStock]
112 [TheBlueSoftDB].[CK_UnitsOnOrder]
113 [TheBlueSoftDB].[CustOrderHist]
114 [TheBlueSoftDB].[CustOrdersDetail]
115 [TheBlueSoftDB].[CustOrdersOrders]
116 [TheBlueSoftDB].[Employee Sales by Country]
117 [TheBlueSoftDB].[Sales by Year]
118 [TheBlueSoftDB].[SalesByCategory]
119 [TheBlueSoftDB].[Ten Most Expensive Products]
120 [TheBlueSoftDB].[User_Registrations]
121 [TheBlueSoftDB].[DF_User_Registrations_Is_Registered]
122 [TheBlueSoftDB].[DF_User_Registrations_Is_Payment_Done]
123 [TheBlueSoftDB].[DF_User_Registrations_Is_Active]
124 [TheBlueSoftDB].[DF_User_Registrations_Entered_On]
125 [TheBlueSoftDB].[DF_User_Registrations_Failed_Tried]
126 [TheBlueSoftDB].[PK_User_Registrations]
127 [TheBlueSoftDB].[FK_User_Registrations_Class_Master]
128 [TheBlueSoftDB].[FK_User_Registrations_States]
129 [TheBlueSoftDB].[FK_Payment_Tracker_User_Registrations]
130 [TheBlueSoftDB].[Payment_Tracker]
131 [TheBlueSoftDB].[Customer and Suppliers by City]
132 [TheBlueSoftDB].[PK_Payment_Tracker]
133 [TheBlueSoftDB].[Ten Most Expensive Products]
134 [TheBlueSoftDB].[DF_Payment_Tracker_Payment_Tracking_Id]
135 [TheBlueSoftDB].[Employee Sales by Country]
136 [TheBlueSoftDB].[DF_Payment_Tracker_Payment_Gateway]
137 [TheBlueSoftDB].[Sales by Year]
138 [TheBlueSoftDB].[DF_Payment_Tracker_Payment_Status]
139 [TheBlueSoftDB].[CustOrdersDetail]
140 [TheBlueSoftDB].[DF_Payment_Tracker_Entered_On]
141 [TheBlueSoftDB].[CustOrdersOrders]
142 [TheBlueSoftDB].[CustOrderHist]
143 [TheBlueSoftDB].[SalesByCategory]
144 [TheBlueSoftDB].[sp_Employees_Insert]
145 [TheBlueSoftDB].[sp_Employees_Update]
146 [TheBlueSoftDB].[sp_Employees_SelectAll]
147 [TheBlueSoftDB].[sp_Employees_SelectRow]
148 [TheBlueSoftDB].[DateOnly]
149 [TheBlueSoftDB].[MyRound]
150 [TheBlueSoftDB].[sp_Employees_Cursor]
151 [TheBlueSoftDB].[sp_employees_rownum]
152 [TheBlueSoftDB].[sp_employees_rank]
153 [TheBlueSoftDB].[sp_employees_rollup]
154 [TheBlueSoftDB].[QueryNotificationErrorsQueue]
155 [TheBlueSoftDB].[queue_messages_1977058079]
156 [TheBlueSoftDB].[EventNotificationErrorsQueue]
157 [TheBlueSoftDB].[queue_messages_2009058193]
158 [TheBlueSoftDB].[ServiceBrokerQueue]
159 [TheBlueSoftDB].[Customers]
160 [TheBlueSoftDB].[queue_messages_2041058307]
161 [TheBlueSoftDB].[PK_Customers]
162 [TheBlueSoftDB].[Suppliers]
163 [TheBlueSoftDB].[Razor_Payment_Logs]
164 [TheBlueSoftDB].[PK_Suppliers]
165 [TheBlueSoftDB].[PK_Razor_Payment_Logs]
166 [TheBlueSoftDB].[Customer and Suppliers by City]
167 [TheBlueSoftDB].[DF_Razor_Payment_Logs_Id]
168 [TheBlueSoftDB].[Categories]
169 [TheBlueSoftDB].[DF_Razor_Payment_Logs_Entered_On]


3. Usage of OBJECT_NAME in WHERE clause

SQL Server Query 3

            
 SELECT name AS Name, object_id AS Object_ID, type_desc AS Type_Desc  
FROM sys.objects
WHERE name = OBJECT_NAME(7);

Create SQL query with SqlQueryBuilder 3

            
 var (sql3, parameters3) = new SqlQueryBuilder()  
.Select()
.Column("name", "Name")
.Column("object_id", "Object_ID")
.Column("type_desc", "Type_Desc")
.From("sys.objects")
.Where(new Where(new Column("name").Equale(new OBJECT_NAME(7))))
.Build();

Query build by SqlQueryBuilder 3

            
SELECT name AS Name,
       object_id AS Object_ID,
       type_desc AS Type_Desc
FROM sys.objects
WHERE name = OBJECT_NAME(@pMAIN_2606200152359524640);


            
        

Parameters (If used)

Name Value
@pMAIN_2606200152359524640 7

Query Results 3:

  Name Object_ID Type_Desc