我正在尝试在VB.NET中完成一个过程,它创建一个SQL数据库的副本并创建所有数据库对象(表,函数和存储过程)。我可以成功创建数据库,对于表,我使用以下代码从文本文件加载脚本:
Dim strConnection As String = myConnectionString
Dim objCon As New SqlConnection(strConnection)
Dim objCmd As SqlCommand = Nothing
Dim sr As StreamReader = Nothing
Dim sb As System.Text.StringBuilder = Nothing
Dim line As String = ""
Dim ScriptFileNames(5) As String
Dim i As Integer
Dim filename As String
ScriptFileNames(1) = "CreateTables.sql"
ScriptFileNames(2) = "CreateStoredProcedures.sql"
ScriptFileNames(3) = "CreateFunctions.sql"
For i = 1 To 1
filename = SysConfig.DBScriptLocation & ScriptFileNames(i)
sr = New StreamReader(filename)
sb = New System.Text.StringBuilder
Do While sr.Peek() <> -1
line = sr.ReadLine()
If (line = "GO" Or line Is Nothing Or line Is System.DBNull.Value) Then
' do nothing
Else
If line Is System.DBNull.Value Then
sb.Append(ControlChars.CrLf & " ")
Else
sb.Append(ControlChars.CrLf & line)
End If
End If
Loop
' Create the database
objCon.Open()
objCmd = objCon.CreateCommand()
' Execute
objCmd.CommandText = sb.ToString
objCmd.ExecuteNonQuery()
objCon.Close()
Next
这适用于所有表,但总是用于存储过程的炸弹。我的文本文件如下:
CREATE PROCEDURE [dbo]。[InsertDeal]( @DealID as int)
AS
BEGIN
INSERT INTO Table1(DealID,LoanID) 选择@DealID作为DealID,LOAN_ID FROM ImportedLoans
INSERT INTO Table2(DealID,LoanID) 选择@DealID作为DealID,LOAN_ID FROM ImportedLoans
END
创建程序SProc2 如 开始 SProc2代码..... 结束
创建程序SProc23 如 开始 SProc3的代码..... 结束 等等
我的代码在“GO”上炸弹,所以我删除了“GO”语句,但它仍然没有运行,我得到的消息没有定义@DealID。
非常感谢任何帮助。感谢
答案 0 :(得分:0)
答案 1 :(得分:0)
我从来没有真正看到用parens包含的T-SQL创建过程语句中的参数...另外,我认为你在为sproc声明参数时不需要使用“As”这个词(见下文) )。
如果删除参数周围的parens会发生什么?例如:
CREATE PROCEDURE [dbo].[InsertDeal]
@DealID int
AS
BEGIN
INSERT INTO Table1 (DealID, LoanID) SELECT @DealID as DealID, LOAN_ID FROM ImportedLoans
INSERT INTO Table2 (DealID, LoanID) SELECT @DealID as DealID, LOAN_ID FROM ImportedLoans
END
我会在T-SQL编辑器中获取您的语法,以便它可以工作,然后使用该知识生成您的脚本。