Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
pcperera
Starting Member
12 Posts |
Posted - 2007-06-18 : 00:07:10
|
| I have a problem with Insert and Update Unicode data to databaseI can send Unicode data using string with N prefix (Ref 001 below)Below I write some line of my SP But It does not write Unicode data to table, it writes like ????????How to insert data using n prefix with Declared Variable?How can avoid this problem?Any other method to pass unicode data using SP?Please help me...column UnitName nvarcharSample Stored Procedure (SP)SP header:CREATE PROCEDURE ltrsp_AddEditUnit @UnitID char(4), @UnitName nvarchar(20),@UtrID intInsert:INSERT INTO ltrtb_Unit (UnitID,UnitName,UtrID) VALUES (@UnitID,@UnitName,@UtrID)Update:---UPDATEltrtb_UnitSETUnitName=@UnitName, UtrID=@UtrIDWHERE UnitID=@UnitIDRef 001:found a method to insert unicode data to table (Using N' prefix), but I want to add using declared variableINSERT INTO ltrtb_UserLevel (UserLevel,Description) VALUES (1,N'Unicode String')From the URL,http://databases.aspfaq.com/general/why-do-some-sql-strings-have-an-n-prefix.htmlBut I need to know how to pass the data using stored procedure |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-06-18 : 03:24:04
|
I'm not following the issueYou are passing the Unicode data in ltrsp_AddEditUnit exec ltrsp_AddEditUnit 'U12',N'MyUnicode',1 Can you please explain a little more of what you are trying to accomplish?[/code] |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-06-18 : 04:17:58
|
| "it writes like ????????"Maybe it is writing it correctly, but the application cannot display it properly??kristen |
 |
|
|
pcperera
Starting Member
12 Posts |
Posted - 2007-06-21 : 04:32:55
|
| Thanks Vinnie and Kristen, I can pass a string using N preficx,My problem is how to write data in a varialbeExample:@myUniStr varchar(20)I want write the data containing in @myUniStr to Tables Column, with this variable, How can I use N prefixRef 001:found a method to insert unicode data to table (Using N' prefix), but I want to add using declared variableINSERT INTO ltrtb_UserLevel (UserLevel,Description) VALUES (1,N'Unicode String')From the URL,http://databases.aspfaq.com/general/why-do-some-sql-strings-have-an-n-prefix.html |
 |
|
|
pcperera
Starting Member
12 Posts |
Posted - 2007-06-21 : 04:35:39
|
| Application displays correctly, I use Visual Basic .NET |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-06-21 : 05:13:55
|
I'm still not following youDECLARE @myTABLE TABLE(NVARCHARCol nvarchar(40))DECLARE @NValue nvarchar(20)SET @Nvalue = N'Test 12321'INSERT INTO @MyTABLESELECT @NvalueDECLARE @Value varchar(20)SET @Value = 'Test 3424324'INSERT INTO @MyTABLESELECT @ValueSELECT * FROM @MyTABLEDECLARE @NewNVal nvarchar(20)SET @NewNVal = @ValueSELECT @NValue AS OriginalNVarchSELECT @Value AS OriginalValueSELECT @NewNVal AS OriginalValueConvertedTONVARCHAR--You can even use CONVERT but it's really not necessary in most cases if your Storing strings in englishSELECT CONVERT(nvarchar(20),@Value) |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2007-06-21 : 08:14:33
|
| "My problem is how to write data in a varialbeExample:@myUniStr varchar(20)"@myUniStr Nvarchar(20)Kristen |
 |
|
|
pcperera
Starting Member
12 Posts |
Posted - 2007-06-22 : 04:52:25
|
| Vinnie881,UR following meyour wroteDECLARE @NValue nvarchar(20)SET @Nvalue = N'Test 12321'you SETting value to @Nvalue in side the stored procedureit works, it write to columnas example my stored procedure like belowCREATE PROCEDURE ltrsp_AddEditUnit @UnitID char(4), @UnitName nvarchar(20),@UtrID intValue for @unitName pass from the program, I use .NET command variableNomal INSERT statement not generate any error but it writes data unitname column like ?????, means it cant write unicode data, I think we have to N prefix,But my problem is how to use N prefix with Insert statementINSERT INTO ltrtb_Unit (UnitID,UnitName,UtrID) VALUES (@UnitID,@UnitName,@UtrID) |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-06-22 : 05:20:54
|
| Let's start from the beginning.Can you execute the Procedure w/o issue outside the .net application?If yes post your .net code, and i'll take a look at it. |
 |
|
|
pcperera
Starting Member
12 Posts |
Posted - 2007-06-22 : 05:51:39
|
| my Stored Procedure is CREATE PROCEDURE ltrsp_AddEditUnit @UnitID char(4), @UnitName nvarchar(20), @UtrID int ASBEGIN TRAN IF EXISTS (SELECT UnitID FROM ltrtb_Unit WHERE UnitID=@UnitID) --EDIT UNIT UPDATE ltrtb_Unit SET UnitName=@UnitName, UtrID=@UtrID WHERE UnitID=@UnitID ELSE -- NEW UNIT BEGIN DECLARE @prefix char(2),@LastNo int EXEC ltrsp_GetSetLastNo 'UNITID',@prefix OUTPUT, @LastNo OUTPUT SET @UnitID=@prefix + LTRIM(STR(@LastNo)) INSERT INTO ltrtb_Unit (UnitID,UnitName,UtrID) VALUES (@UnitID,@UnitName,@UtrID) ENDCOMMIT TRANmy Class to handle Database is / I put only code to manage Command ObjectImports System.Data.OleDbPublic Class clsDbs Public Sub SetCmd(ByVal spName As String) '1 - SetCmd() 2 - AddParameter() 3 - ExecuteSP() / ExecuteSP_R() OpenConnection() myCmd = New OleDbCommand With myCmd .Connection = myCon .CommandText = spName .CommandType = CommandType.StoredProcedure End With End Sub Public Sub AddParameter(ByVal pName As String, ByVal pType As OleDbType, ByVal pSize As Integer, ByVal pValue As Object, ByVal pDirection As ParameterDirection) myCmd.Parameters.Add(pName, pType, pSize).Value = pValue myCmd.Parameters(myCmd.Parameters.Count - 1).Direction = pDirection End Sub Public Function ExecuteSP() As Integer ExecuteSP = myCmd.ExecuteNonQuery() myCmd.Dispose() myCmd = Nothing CloseConnection() End Function Public Function ExecuteSP_R() As String myCmd.ExecuteNonQuery() ExecuteSP_R = myCmd.Parameters(0).Value myCmd.Dispose() myCmd = Nothing CloseConnection() End FunctionEnd Classbefore use command object I tried to run the stored procedure using query analyzerI can write unicode data, but they don't goto databaseit doesn't work, Public Sub Save() Dim myDb As New clsDbs With myDb .SetCmd("ltrsp_AddEditUnit ") .AddParameter("UnitID ", OleDb.OleDbType.Char, 4, UnitID, ParameterDirection.Input) .AddParameter("UnitName", OleDb.OleDbType.varChar, 20, UnitName, ParameterDirection.Input) .AddParameter("UtrID", OleDb.OleDbType.integer, 1, UtrID, ParameterDirection.Input) .ExecuteSP() End With myDb = Nothing End Sub |
 |
|
|
Vinnie881
Master Smack Fu Yak Hacker
1231 Posts |
Posted - 2007-06-22 : 08:08:06
|
I successfully passed the variable Unitname into a Nvarchar column in my table here is the exact code I usedImports System.Data.OleDbPublic Class clsDbs Dim myCon As OleDbConnection Dim myCmd As OleDbCommand Public Sub SetCmd(ByVal spName As String) '1 - SetCmd() 2 - AddParameter() 3 - ExecuteSP() / ExecuteSP_R() myCon = New OleDbConnection myCmd = New OleDbCommand myCon.ConnectionString = "Provider=sqloledb;" & _ "Data Source=(Local);" & _ "Initial Catalog=MyDatabase;" & _ "Integrated Security=SSPI" With myCmd .Connection = myCon .CommandText = spName .CommandType = CommandType.StoredProcedure End With End Sub Public Sub AddParameter(ByVal pName As String, ByVal pType As OleDbType, ByVal pSize As Integer, ByVal pValue As Object, ByVal pDirection As ParameterDirection) myCmd.Parameters.Add(pName, pType, pSize).Value = pValue myCmd.Parameters(myCmd.Parameters.Count - 1).Direction = pDirection End Sub Public Function ExecuteSP() As Integer myCmd.Connection.Open() ExecuteSP = myCmd.ExecuteNonQuery() myCmd.Dispose() myCmd = Nothing End Function Public Function ExecuteSP_R() As String myCmd.ExecuteNonQuery() ExecuteSP_R = myCmd.Parameters(0).Value myCmd.Dispose() myCmd = Nothing End FunctionEnd ClassPublic Class savevar Public Sub Save() Dim myDb As New clsDbs With myDb .SetCmd("ltrsp_AddEditUnit ") .AddParameter("@UnitID", OleDb.OleDbType.Char, 4, "3234", ParameterDirection.Input) .AddParameter("@UnitName", OleDb.OleDbType.VarChar, 20, "Testing123", ParameterDirection.Input) .AddParameter("@UtrID", OleDb.OleDbType.Integer, 1, 23, ParameterDirection.Input) .ExecuteSP() End With myDb = Nothing End Sub Shared Sub main() Dim x As New savevar x.Save() End SubEnd ClassHere is the stored procedure I used/*create Table TestTable(Col1 nvarchar(20))*/create PROCEDURE ltrsp_AddEditUnit@UnitID char(4),@UnitName nvarchar(20),@UtrID intasinsert into TestTable(Col1)Select @UnitName |
 |
|
|
|
|
|
|
|