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;
}
}