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

  • Thread starter WWGD
  • Start date
  • Tags
    Set
In summary, the conversation discusses the use of the SET and SELECT statements in T-SQL, specifically in terms of assigning values to variables and querying database metadata. There is confusion around the OBJECT_ID function and its purpose, and it is clarified that it is used to determine the existence of tables, not databases. It is also mentioned that in standard SQL, an UPDATE or INSERT statement would be used to insert or update data in a separate table.
  • #1
WWGD
Science Advisor
Gold Member
6,997
10,416
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
  • #2
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 WWGD

What is the difference between SET and SELECT in T-SQL?

In T-SQL, SET is used to assign a value to a variable, while SELECT is used to retrieve data from a database table. SET is typically used for single-value assignments, while SELECT is used for retrieving multiple rows or columns of data.

When should I use SET instead of SELECT in T-SQL?

SET should be used when assigning a single value to a variable, such as a string or number. This is useful when performing calculations or data manipulation within a query. SELECT should be used when retrieving data from a database table.

What is the purpose of OBJECT_ID() in T-SQL?

OBJECT_ID() is a built-in function in T-SQL that is used to retrieve the object ID of a specified database object, such as a table, view, or stored procedure. This function is useful for performing operations on specific database objects within a query.

How do I use OBJECT_ID() in T-SQL?

To use OBJECT_ID() in T-SQL, you need to specify the name of the object you want to retrieve the ID for within the parentheses. For example, OBJECT_ID('myTable') will return the object ID for a table named 'myTable'.

Can OBJECT_ID() be used to retrieve the ID for temporary tables in T-SQL?

Yes, OBJECT_ID() can be used to retrieve the ID for temporary tables in T-SQL. However, the temporary table needs to be created with the '#' prefix, and the object name needs to be specified with the '#' prefix as well, such as OBJECT_ID('#tempTable').

Similar threads

  • Programming and Computer Science
Replies
7
Views
2K
  • STEM Academic Advising
Replies
1
Views
664
  • Programming and Computer Science
Replies
5
Views
5K
  • Programming and Computer Science
2
Replies
45
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Computing and Technology
Replies
1
Views
1K
  • Set Theory, Logic, Probability, Statistics
2
Replies
35
Views
2K
Replies
11
Views
1K
  • Computing and Technology
Replies
1
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top