0

I am trying to rename some tables programmatically with both greek and english characters (example :Δοκιμή_Tab) but the characters are shown as ??μ?φ_Tab? .etc

using (Command = new SqlCommand($"EXEC SP_RENAME '{OriginalNameTab}','{RenamedTab}';", ConnectionString))

Searching for a solution I came accross

Collate SQL_Latin1_General_CP1253_CI_AI

but if I try to use it withc EXEC it generates error.

Thanks in advance!

MrValvis
  • 107
  • 1
  • 10
  • 1
    8 bit characters are from 0x00 to 0xFF (256). With encoding the characters from 0x00 to 0x7F a usually the same. The characters 0x80 to 0xFF depending on encoding will map these character to unicode character so instead of using two byte unicode character one byte encoding is used. So when the wrong characters are displayed it means the wrong encoding is being used (or no encoding). Encoding takes a byte array and converts to a string. One a conversion form bytes to string is done you cannot easily convert one encoded string to a different encoding. You have to go back to bytes. – jdweng Jun 22 '20 at 11:57
  • 2
    Identifiers in SQL Server are Unicode. Specify the `N` prefix for to denote Unicode strings and avoid character loss: `EXEC sp_rename N'{OriginalNameTab}',N'{RenamedTab}';"`. Even better, use a parameterized query so you don't need to bother. – Dan Guzman Jun 22 '20 at 11:57
  • I have tried using @"EXEC SP_RENAME N'@OriginalRenterName}',N'@RenamedRenter';" but its resulting to error (the quotes block the using of @) – MrValvis Jun 22 '20 at 12:09
  • @MrValvis, one does not enclose parameters in quotes. See [this article](https://www.dbdelta.com/why-parameters-are-a-best-practice/) for more info. With a stored procedure, specify only the proc name in the command text, CommandType.StoredProcedure, and add the parameters separately. `sp_rename` expects parameters @objname nvarchar(1035) and @newname nvarchar(128). – Dan Guzman Jun 22 '20 at 12:35

0 Answers0