Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Japanese Characters to a SQL Server stored procedure

I'm trying to pass Japanese characters to a stored procedure in SQL Server. The characters get converted to ???? during execution and hence I get incoreect results.

How do I pass them such characters? I tried using nvarchar as its accepts Unicode, but no luck. If I do not use stored procedure and construct the SQL Query string dynamically in my app, things work fine. Kindly help

Update

declare @string as nvarchar(1000)
SET @string = @keyword 
SELECT * FROM TABLE where Title like @string 

Called using

EXEC @return_value = StoredProcName 
@keyword = N'Japanese Word' 

Update 2

I would like to add the following if that helps. Constructing the query dynamically works. So if in your app you were to write sql = "select * from TABLE where Title like '"+[JapaneseWord]+"'"; it works. I am unable to determine where the Unicode is failing for a stored proc

Solution

NVARCHAR is the key to the problem. In case anyone is interested is knowing how to use it to form a freetext search string, here you go

declare @string as nvarchar(100) set @string = 'FORMSOF(INLFLECTIONAL,"'+@string+'"))'
Then use it in your query. Like
select * from TABLE as TEMP INNERJOIN CONTAINSTABLE(#,#,@string) ......
Refer MSDN or relevant docs

like image 724
Tom Avatar asked Jan 20 '23 18:01

Tom


1 Answers

You definitely need to use NVARCHAR as your data type, and if you run this in SQL server Mgmt Studio or from an "inline" SQL query, you need to make sure to prefix the string literals with a N'.....' prefix.

So, your stored proc should look something like:

CREATE PROCEDURE dbo.YourProc  @Param1 NVARCHAR(100)
AS
   SELECT N'Test - [' + @Param1 + N'] - end of test'

And then call this using:

EXEC dbo.YourProc   N'foo-bar-value'

Should yield:

Test - [foo-bar-value] - end of test

and of course this would be much nicer if I knew how to get some Japanese characters in here.... :-)

Update: OK, I've picked some random Cyrillic and Thai characters, which I'm positive require NVARCHAR, too - and it would appear to me that it works:

EXEC dbo.YourProc   N'Є Ї Љ Њ Ќ Ѝ Ў А Й Ж М Р Б'

Test - [Є Ї Љ Њ Ќ Ѝ Ў А Й Ж М Р Б] - end of test

EXEC dbo.YourProc   N'฿ ᴂ ᴆ ᴌ ᴔ ᴓ ᴙ ᴚ ᴝ ᴭ ᴦ ᴣ ᴟ'

Test - [฿ ᴂ ᴆ ᴌ ᴔ ᴓ ᴙ ᴚ ᴝ ᴭ ᴦ ᴣ ᴟ] - end of test

Not sure why this Unicode-based test would work, and why it shouldn't work with Japanese characters....

Update #2: thanks for Wikipedia - this is said to mean "Tokyo", in Hiragana - seems to work for me:

EXEC dbo.YourProc   N'とうきょう'

Test - [とうきょう] - end of test
like image 199
marc_s Avatar answered Jan 31 '23 09:01

marc_s