>I am working on a script to create a database in SQL Server 2008. The script includes the creation of UDFs, and at least one of those contains a "?" in the UDF. Since SQLDMO is apparently gone, I am working on using SQLExec() to execute the script. The problem is that SQLExec() is interpreting the "?" as a parameter (and prompting for a value) when it is not. Is there anyway to tell SQLExec() to ignore anything it interprets as a parameter?
>
Apparently, there is no way around this using SQLExec():
http://stackoverflow.com/questions/3348932/how-to-avoid-odbc-parameterization-for-the-question-mark-character-within-literHowever, the same problem does not occur if you use ADO to send the commands. Another well known issue is that you can not send commands containing GO to SQL Server. Although GO works in SSMS, it is not officially part of the T-SQL language and does not work when sent as a command. Workarounds are to send the script using SQLDMO (deprecated), SQLSMO (.NET), a command-line utility, or to parse the GOs out and send each command separately. The sample code below does the latter and uses ADO to send the commands.
Local lcConnString, lcSQLCmd, lcSQLScript, llSuccess
Local loConnection as ADODB.Connection
Local Array laScript[1]
TEXT TO lcConnString NOSHOW TEXTMERGE
Provider=SQLNCLI10;
Server=MySQLServer;
Uid=MyUserID;
Pwd=MyPassword;
Trusted_Connection=no;
ENDTEXT
loConnection = CreateObject("ADODB.Connection")
Try
loConnection.Open(lcConnString)
Catch
MessageBox('Connection Failed: ' + Chr(13) + Message() + Chr(13) + lcConnString, 16, 'SQL Connect Error')
EndTry
If loConnection.State <> 1
Return
EndIf
lcSQLScript = FileToStr("MyScript.sql")
ALines(laScript, lcSQLScript, 8, "GO" + Chr(13))
For each lcSQLCmd in laScript
Try
loConnection.Execute(lcSQLCmd)
llSuccess = .t.
Catch
MessageBox('Error Running Script:' + Chr(13) + Message() + Chr(13) + lcSQLCmd, 16, 'SQL Error')
llSuccess = .f.
EndTry
If !llSuccess
Return
EndIf
EndFor