Groups | Blog | Home
all groups > sql server programming > december 2003 >

sql server programming : VBscript - Delimiter in the data problem


Dan Guzman
12/16/2003 9:30:28 PM
You can replace the quote with 2 consecutive quotes. For example:

strSQL1 = "INSERT INTO TableTest (TestName, TestDesc) VALUES ( '"
&DoubleUpQuotes(CurName)& "','" &DoubleUpQuotes(CurDesc)& "')"

Function DoubleUpQuotes(Value)
DoubleUpQuotes = Replace(Value, "'", "''")
End Function

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

robc
12/16/2003 10:48:42 PM
Running a DTS package that has worked until now... delimiters are now
present in the data and the package crashes... anyone know how to prevent ?
I've attached snippets of the code so you could reproduce the error if
necessary. I believe I must reconstruct strSQL1, but how ?



-- The table it tries to insert into
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[TableTest]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[TableTest]
GO

CREATE TABLE [dbo].[TableTest] (
[TestName] [char] (30) NULL ,
[TestDesc] [char] (30) NULL
) ON [PRIMARY]
GO



'**********************************************************************
' Visual Basic ActiveX Script
'************************************************************************

Function Main()


Dim ADOcon
Dim strConnection
Dim ADOrst
Dim strSQL1
Dim CurName
Dim CurDesc

Set ADOcon = CreateObject("ADODB.Connection")
Set ADOrst = CreateObject("ADODB.Recordset")
ADOcon.Provider = "sqloledb"
ADOcon.Properties("Data Source").Value = "(local)"
ADOcon.Properties("Initial Catalog").Value ="AATest"
ADOcon.Properties("User ID").Value ="sa"
ADOcon.Properties("Password").Value ="sa"

ADOcon.Open strConnection

CurName= "7' Test "
CurDesc = "Whatever "

strSQL1 = "INSERT INTO TableTest (TestName, TestDesc) VALUES ( '"
&CurName& "','" &CurDesc& "')"
'You can plainly see why it crashes here
msgbox strSQL1
ADOcon.Execute strSQL1


Main = DTSTaskExecResult_Success
End Function

robc
12/17/2003 5:32:32 AM
Thanks, works great now. I do not understand why though... if time
permits, could you please point me in the direction of some helpful
documentation.

Thanks,
Rob


[quoted text, click to view]

Dan Guzman
12/17/2003 7:30:02 AM
You can find details in the SQL Server Books Online:

<Excerpt href="tsqlref.chm::/ts_ca-co_1n1v.htm">

If a character string enclosed in single quotation marks contains an
embedded quotation mark, represent the embedded single quotation mark with
two single quotation marks.

</Excerpt>

Another method is to use parameters rather than building a query string so
that you don't need to escape the quotes. Example below. Also, consider
using a login other than 'sa' for application database access.

Function Main()

Const adVarChar = 200
Const adParamInput = 1

Dim ADOcon
Dim ADOcmd
Dim ADOparm
Dim strConnection
Dim ADOrst
Dim strSQL1
Dim CurName
Dim CurDesc

Set ADOcon = CreateObject("ADODB.Connection")
Set ADOcmd = CreateObject("ADODB.Command")
ADOcon.Provider = "sqloledb"
ADOcon.Properties("Data Source").Value = "(local)"
ADOcon.Properties("Initial Catalog").Value ="AATest"
ADOcon.Properties("User ID").Value ="MyLogin"
ADOcon.Properties("Password").Value ="MyPassword"

ADOcon.Open strConnection

CurName= "7' Test "
CurDesc = "Whatever "

ADOcmd.ActiveConnection = ADOcon
ADOcmd.CommandText = "INSERT INTO TableTest (TestName, TestDesc) VALUES ( ?,
? )"

Set ADOparm = ADOcmd.CreateParameter("TestName", adVarChar, adParamInput,
20, CurName)
ADOcmd.Parameters.Append ADOparm

Set ADOparm = ADOcmd.CreateParameter("TestDesc", adVarChar, adParamInput,
20, CurDesc)
ADOcmd.Parameters.Append ADOparm

ADOcmd.Execute
Main = DTSTaskExecResult_Success
End Function


--
Hope this helps.

Dan Guzman
SQL Server MVP


[quoted text, click to view]

robc
12/17/2003 9:40:22 AM
Thanks again !


[quoted text, click to view]

AddThis Social Bookmark Button