简体   繁体   中英

How to get table/view name (i.e. remove schema prefix)

Given one of the following strings (which represent a table/view name in SQL Server):

var x = "[my-Table.request]"; 
var x = "[dbo].[my-Table.request]";
var x = "dbo.[my-Table.request]";

I would like to get the table name (by C# code): my-Table.request

Any ideas? have i missed any possible representations here?

Quite simply via PARSENAME in TSQL:

PARSENAME(@x, 1)

With your edit where you want it in C#, you'd basically have to write it from scratch in C#, tokenizing by the . , [ , ] . AFAIK there is no pre-canned implementation that does this for you.

If you're doing this on the SQL side, there's a PARSENAME function for doing exactly that.

Eg PARSENAME(x,1) returns the object (in this case table) name. PARSENAME(x,2) returns the schema, etc.

you can use the below query:

USE MyDB GO SELECT * FROM sys.Tables GO

Please Note: MyDB is your database name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM