OnlyHope
OnlyHope

Reputation: 123

Table Name Display

Is there a function to show the table name at sql server? I want to check if the user is found at a specific table, the table's name should be returned, can I do it?

 IF EXISTS(
 SELECT        Std_ID
 FROM            Student
 WHERE Std_ID = @UserId) 

I want it here to return Student.

Upvotes: 0

Views: 83

Answers (2)

Mayur Desai
Mayur Desai

Reputation: 713

You can use the following query which check the table is already in sys.object and if it is there then it returns the name of table.

select name from sys.objects where type = 'U' and name = 'Student'

Upvotes: 1

Chris Gessler
Chris Gessler

Reputation: 23113

Since you already know you're going to check the Student table, just return the value 'Student'

if(exists(select * from Student where Std_ID = @UserId))
begin
  select 'Student' as TableName
  return;
end

Upvotes: 3

Related Questions