Remembering T-SQL: SET vs SELECT and OBJECT_ID()

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Set
Click For Summary
SUMMARY

This discussion focuses on the differences between the T-SQL commands SET and SELECT, particularly in the context of variable assignment. It clarifies that SELECT can assign values to multiple variables simultaneously, while SET is typically used for single variable assignments. Additionally, the OBJECT_ID function is explained as a tool for querying database metadata, specifically for checking the existence of database objects like tables, rather than databases themselves. The user encountered an error when using OBJECT_ID with a database name, highlighting a common misunderstanding of its application.

PREREQUISITES
  • Understanding of T-SQL syntax and commands
  • Familiarity with SQL Server and its functions
  • Knowledge of variable declaration and assignment in T-SQL
  • Basic concepts of database metadata and querying
NEXT STEPS
  • Explore the differences between SET and SELECT in T-SQL for variable assignments
  • Learn about the usage and limitations of the OBJECT_ID function in SQL Server
  • Investigate how to use SELECT with INSERT and UPDATE statements in T-SQL
  • Review SQL Server documentation on metadata functions and their applications
USEFUL FOR

Database developers, SQL Server administrators, and anyone looking to deepen their understanding of T-SQL variable handling and database metadata querying.

WWGD
Science Advisor
Homework Helper
Messages
7,804
Reaction score
13,107
Hi, I wonder if someone remembers their T-SQL. We have this statement:

upload_2016-4-21_1-52-0.png


Just curious: I remember using SET where SELECT appears. Is this SELECT used in order to
assign values to many variables simultaneously? Also, Is there a reason to use two separate
DECLARE statements , or could we just use a single one?

EDIT : I am also confused about the OBJECT_ID function. I tried it on my SQL Server and got an error message when entering the name of the database I was using, i.e., OBJECT_ID( 'Database Name') generated an error message.
Thanks.
 
Last edited:
Technology news on Phys.org
From the examples I saw online it seems object-id is used to determine if tables exist not databases so it probably is used to query database metadata like table names, column names and schema names:

https://msdn.microsoft.com/en-us/library/ms187731.aspx

and more on object-id:

http://stackoverflow.com/questions/9372867/what-does-object-id-do-in-sql-server

I think the select is just returning a set of values matching the SELECT criteria. I don't think its making some sort of array of values assigned to the variables mentioned. In standard SQL, you'd use an UPDATE or INSERT with an embedded SELECT to pick rows and columns to be inserted/updated in a separate table.

http://www.w3schools.com/sql/sql_insert_into_select.asp
 
  • Like
Likes   Reactions: WWGD

Similar threads

Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
5
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 45 ·
2
Replies
45
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
11
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 35 ·
2
Replies
35
Views
3K
Replies
1
Views
4K