简体   繁体   中英

How to write correct syntax to rename SQL Server database table from C# code?

I'm coding in C# in ASP.NET environment and I need to write a function that takes SQL Server database table and gives it another name.

So from SQL standpoint I need to do this:

EXEC sp_rename 'OldTableName', 'NewTableName';

But the problem is that at times the (old) table name supplied to my function can be something like this: [dbo].[OldTableName] and as far as I can understnad the brackets ('[' and ']') are not the part of the name itself, as well as the "dbo" part.

So how to handle such situation?

EDIT: I was able to come up with C# code to remove brackets (needs to be checked though):

for (int i = 0; i < strTableName.Length; i++)
{
    if (strTableName[i] == '[' ||
    strTableName[i] == ']')
    {
        int j = i;
        for (; j < strTableName.Length && strTableName[j] == strTableName[i]; j++) ;

        int nRepeatCnt = j - i;
        int nNumKeep = nRepeatCnt / 2;
        int nNumRemove = nRepeatCnt - nNumKeep;

        strTableName = strTableName.Remove(i, nNumRemove);
        i += nNumKeep - 1;
    }
}

When using SQL Server internal (system) stored procedures and functions ( sp_rename , sp_help , OBJECT_ID , ...), there is no need to remove or add delimiters and qualifiers ('[' and ']' or default schema name such as 'dbo'), because these functions parse the identifier names and infer the actual name. Also there are some situations that you require to use the delimiters (When they are not Regular Identifiers . See Identifiers ).

For example when renaming dbo.MyTable to dbo.NewTable , all of these command are valid:

sp_rename 'dbo.MyTable', 'NewTable'
sp_rename '[dbo].MyTable', 'NewTable'
sp_rename 'MyTable', 'NewTable'
sp_rename '[dbo].[MyTable]', 'NewTable'

But be noticed that the new name you specify as the second parameter of the sp_rename will not be parsed, and the stored procedure will set the object name exactly as what you specified:

sp_rename 'dbo.MyTable', '[dbo].NewTable'

This changes MyTable to [dbo].NewTable , and your qualified table name is exactly dbo.[dbo].NewTable ! Accessing this new table with this name, is a little tricky:

sp_rename 'dbo."[dbo].NewTable"', 'OldTableName'

But when accessing object names in SQL Server system tables (like sys.table , sys.columns , ...), you should not use delimiters and qualifiers, because the identifiers in those table are stored as character strings:

select * from sys.columns where object_id = OBJECT_ID('dbo.Orders') and [name]='OrderID'

OBJECT_ID() is a system function and parses the object name, but OrderID should be specified as the exact column name (case insesitive ).

The fact that you include [dbo] in renaming the table should not matter. For example:

Why do table names in SQL Server start with "dbo"?

Whether you include [dbo] or not, the rename command should still work.

Is this the case for you?

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