K
Karl Rhodes
Hi all, I have a Windows Forms application (VB.Net 2005) that connects
to a SQL Server 2005 Database.
When the application runs it checks the database for the existance of
a certain stored procedure and if it doesnt exist, it needs to create
it. The code in the VB app that is used to create the SP is shown
below...
Dim strSQL As String = ""
Dim oDT As New DataTable
strSQL = strSQL & "select * from INFORMATION_SCHEMA.ROUTINES where
ROUTINE_NAME like 'MyNewSP'"
Using oSQLConn As New
SqlConnection(ConfigurationManager.ConnectionStrings("MyDataStore").ConnectionString)
Using oDA As New SqlDataAdapter(strSQL, oSQLConn)
oDA.Fill(oDT)
If oDT.Rows.Count < 1 Then
Using oCmd As New SqlCommand
Dim strCreateSP As String = ""
strCreateSP = strCreateSP & "CREATE PROCEDURE MyNewSP ( " &
vbCrLf
strCreateSP = strCreateSP & "@Param1 Varchar(100) = Null, " &
vbCrLf
strCreateSP = strCreateSP & "@Param2 Varchar(100) = Null ) " &
vbCrLf
strCreateSP = strCreateSP & "AS " & vbCrLf
strCreateSP = strCreateSP & "BEGIN " & vbCrLf
strCreateSP = strCreateSP & "DECLARE @strSQL varchar(8000) " &
vbCrLf
strCreateSP = strCreateSP & "SET @strSQL = 'Select * from
tbl_MyTable " & vbCrLf
strCreateSP = strCreateSP & "WHERE FieldOne like <Param1> " &
vbCrLf
strCreateSP = strCreateSP & "AND FieldTwo Like <Param2> " &
vbCrLf
strCreateSP = strCreateSP & "ORDER BY FieldOne' " & vbCrLf
strCreateSP = strCreateSP & "SET @strSQL = replace(@strSQL,
'<Param1>', @Param1 ) " & vbCrLf
strCreateSP = strCreateSP & "SET @strSQL = replace(@strSQL,
'<Param2>', @Param2 ) " & vbCrLf
strCreateSP = strCreateSP & "EXEC (@strSQL) " & vbCrLf
strCreateSP = strCreateSP & "END"
oCmd.Connection = oSQLConn
oCmd.CommandType = CommandType.Text
oCmd.CommandText = strCreateSP
oCmd.Parameters.Clear()
oCmd.Parameters.AddWithValue("@Param1", "@Param1")
oCmd.Parameters.AddWithValue("@Param2", "@Param2")
Try
If oCmd.Connection.State <> ConnectionState.Open Then
oCmd.Connection.Open()
oCmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Cannot continue!" & vbCrLf & vbCrLf & "Failed to create
the stored procedure.")
Me.Close()
End Try
End Using
End If
End Using
End Using
When I run this and check SQL Profiler I see the text "exec
sp_executesql N'" before the word "CREATE" and get the error
"incorrect syntax near the keyword 'PROCEDURE'"
Can anyone see where I'm going wrong, or does anyone know how to
dynamically create a stored procedure in a sql server database in this
way?
Thanks
Karl
to a SQL Server 2005 Database.
When the application runs it checks the database for the existance of
a certain stored procedure and if it doesnt exist, it needs to create
it. The code in the VB app that is used to create the SP is shown
below...
Dim strSQL As String = ""
Dim oDT As New DataTable
strSQL = strSQL & "select * from INFORMATION_SCHEMA.ROUTINES where
ROUTINE_NAME like 'MyNewSP'"
Using oSQLConn As New
SqlConnection(ConfigurationManager.ConnectionStrings("MyDataStore").ConnectionString)
Using oDA As New SqlDataAdapter(strSQL, oSQLConn)
oDA.Fill(oDT)
If oDT.Rows.Count < 1 Then
Using oCmd As New SqlCommand
Dim strCreateSP As String = ""
strCreateSP = strCreateSP & "CREATE PROCEDURE MyNewSP ( " &
vbCrLf
strCreateSP = strCreateSP & "@Param1 Varchar(100) = Null, " &
vbCrLf
strCreateSP = strCreateSP & "@Param2 Varchar(100) = Null ) " &
vbCrLf
strCreateSP = strCreateSP & "AS " & vbCrLf
strCreateSP = strCreateSP & "BEGIN " & vbCrLf
strCreateSP = strCreateSP & "DECLARE @strSQL varchar(8000) " &
vbCrLf
strCreateSP = strCreateSP & "SET @strSQL = 'Select * from
tbl_MyTable " & vbCrLf
strCreateSP = strCreateSP & "WHERE FieldOne like <Param1> " &
vbCrLf
strCreateSP = strCreateSP & "AND FieldTwo Like <Param2> " &
vbCrLf
strCreateSP = strCreateSP & "ORDER BY FieldOne' " & vbCrLf
strCreateSP = strCreateSP & "SET @strSQL = replace(@strSQL,
'<Param1>', @Param1 ) " & vbCrLf
strCreateSP = strCreateSP & "SET @strSQL = replace(@strSQL,
'<Param2>', @Param2 ) " & vbCrLf
strCreateSP = strCreateSP & "EXEC (@strSQL) " & vbCrLf
strCreateSP = strCreateSP & "END"
oCmd.Connection = oSQLConn
oCmd.CommandType = CommandType.Text
oCmd.CommandText = strCreateSP
oCmd.Parameters.Clear()
oCmd.Parameters.AddWithValue("@Param1", "@Param1")
oCmd.Parameters.AddWithValue("@Param2", "@Param2")
Try
If oCmd.Connection.State <> ConnectionState.Open Then
oCmd.Connection.Open()
oCmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Cannot continue!" & vbCrLf & vbCrLf & "Failed to create
the stored procedure.")
Me.Close()
End Try
End Using
End If
End Using
End Using
When I run this and check SQL Profiler I see the text "exec
sp_executesql N'" before the word "CREATE" and get the error
"incorrect syntax near the keyword 'PROCEDURE'"
Can anyone see where I'm going wrong, or does anyone know how to
dynamically create a stored procedure in a sql server database in this
way?
Thanks
Karl