我们正在使用SQL Server将经典的ASP应用程序迁移到多语言,为此,第一步,我们必须从ansi / windows-1252字符集迁移到Unicode。
我们已经成功地移动了ASP程序(然后通过BOM将其保存在UTF-8中),但是现在我们遇到了SQL Server问题。
我们已将所有列从CHAR
转换为NCHAR
,并从VARCHAR
转换为NVARCHAR
。
我们的问题出现在动态SQL语句中(我知道,我知道,以后会删除它)。
当我们使用Unicode字符串文字时,SQL Server手册要求使用N'MyUnicode'格式(前缀为大写N的文字)。但这对我们来说是一场噩梦(我们只能在SQL语句中寻找代码,搜索文字。
是否有任何方法可以请求SQL将任何文字视为Unicode? p>
我们的测试代码:1和3有效,2失败
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Language" content="en" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<%
' Respose charset UTF-8 is equivalent to "Content-Type" content="text/html; charset=UTF-8"
Response.Charset="UTF-8"
Response.LCID=1033
Session.LCID=1033
Response.Write "Testing charset UTF-8 with BOM: " & "áéΔ" & "<br/>"
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Provider=SQLOLEDB; Database=testUnicode;User Id=test;Password=xxxxxxxx"
SQLStmt = "select * from test"
Set rs = Conn.execute(SQLStmt)
If (rs.EOF) then
Response.Write "Select (all records) failed. No record return.<br/>"
Else
Response.Write "Select (all records) succeed. The table contains:<br/>"
Do while not rs.EOF
Response.Write rs.fields("HTML") & " = " & Server.HTMLEncode(rs.fields("UNICODE")) & " Value=" & AscW(rs.fields("UNICODE")) & "<br/>"
valueToSearch=rs.fields("UNICODE")
rs.MoveNext
Loop
rs.Close
Response.Write "<br/><br/>"
Response.Write "Checking for specific record<br/>"
' This works!!
valueToSearch="Δ"
Response.Write "Checking for value = " & AscW(valueToSearch) & "<br/>"
SQLStmt = "select * from test where UNICODE='" & valueToSearch & "'"
Set rs = Conn.execute(SQLStmt)
If (rs.EOF) then
Response.Write "Select (one record " & Server.HTMLEncode(valueToSearch) & ") failed. No record return. Select=" & SQLStmt & "<br/>"
Else
Response.Write "Select (one record " & Server.HTMLEncode(valueToSearch) & ") succeed.<br/>"
rs.MoveFirst
Response.Write rs.fields("HTML") +" = "+Server.HTMLEncode(rs.fields("UNICODE"))+"<br/>"
Response.Write "TESTBIT = " + Server.HTMLEncode(rs.fields("TESTBIT"))+"<br/>"
Response.Write "TEXTBIT TEXTO = " + trim(rs.fields("TESTBIT")) +"<br/>"
End if
Response.Write "<br/>"
' This fail!
valueToSearch="é"
Response.Write "Checking for value = " & AscW(valueToSearch) & "<br/>"
SQLStmt = "select * from test where UNICODE='" & valueToSearch & "'"
Set rs = Conn.execute(SQLStmt)
If (rs.EOF) then
Response.Write "Select (one record " & Server.HTMLEncode(valueToSearch) & ") failed. No record return. Select=" & SQLStmt & "<br/>"
Else
Response.Write "Select (one record " & Server.HTMLEncode(valueToSearch) & ") succeed.<br/>"
rs.MoveFirst
Response.Write rs.fields("HTML") +" = "+Server.HTMLEncode(rs.fields("UNICODE"))+"<br/>"
Response.Write "TESTBIT = " + Server.HTMLEncode(rs.fields("TESTBIT"))+"<br/>"
Response.Write "TEXTBIT TEXTO = " + trim(rs.fields("TESTBIT")) +"<br/>"
End if
Response.Write "<br/>"
' This works!
valueToSearch="é"
Response.Write "Checking for value = " & AscW(valueToSearch) & "<br/>"
SQLStmt = "select * from test where UNICODE=N'" & valueToSearch & "'"
Set rs = Conn.execute(SQLStmt)
If (rs.EOF) then
Response.Write "Select (one record " & Server.HTMLEncode(valueToSearch) & ") failed. No record return. Select=" & SQLStmt & "<br/>"
Else
Response.Write "Select (one record " & Server.HTMLEncode(valueToSearch) & ") succeed.<br/>"
rs.MoveFirst
Response.Write rs.fields("HTML") +" = "+Server.HTMLEncode(rs.fields("UNICODE"))+"<br/>"
Response.Write "TESTBIT = " + Server.HTMLEncode(rs.fields("TESTBIT"))+"<br/>"
Response.Write "TEXTBIT TEXTO = " + trim(rs.fields("TESTBIT")) +"<br/>"
End if
End if
Conn.Close
%>
</body>
我们的测试表是这样的
CREATE TABLE [dbo].[test]
(
[HTML] [NVARCHAR](50) NULL,
[UNICODE] [NVARCHAR](50) NULL,
[TESTBIT] BIT
) ON [PRIMARY]
GO
INSERT [dbo].[test] ([HTML], [UNICODE], TESTBIT)
VALUES (N'á', N'á', 1), (N'é', N'é', 1),
/* Greek Delta Δ */
(N'Δ', N'Δ',0);
GO