Hi,
I have a vb.net function that creates several Stored Procedures based on parameters being passed into the function.
I want to move this vb.Net into a single SQL file (for maintenance reasons) but I am not sure how I can re-create it in SQL without creating 7 separate stored procedures.
I create a total of 20 Stored Procedures and I don't really want to create this many in a SQL file as maintenance will be a nightmare. I am wondering if there is a solution similar to how I have done it VB.Net below:
Private Sub CreateStoredProcedures()
CreateSP("SummaryNone", "YEAR([sale].[DateTime])")
CreateSP("SummaryUser", "[sale].[User]")
CreateSP("Summarysale", "[sale].[sale]")
CreateSP("SummaryBatch", "[sale].[Batch]")
CreateSP("SummaryDay", "dbo.FormatDateTime([sale].[DateTime], 'yyyy-mm-dd')")
CreateSP("SummaryMonth", "dbo.FormatDateTime(dbo.DateSerial(YEAR([sale].[DateTime]), MONTH([sale].[DateTime]), 1), 'yyyy-mm-dd')")
CreateSP("SummaryYear", "Year([sale].[DateTime])")
Return
End Sub
Private Sub CreateSP(ByVal vName As String, ByVal vGroup As String)
Dim CommandText As String = _
"CREATE PROCEDURE " & vName _
& " @StartDate varchar(50)," _
& " @EndDate varchar(50)" _
& " AS " _
& " SELECT " & vGroup & " AS GroupField," _
& " Sum([Project].[NumProject]) AS TotalProject," _
& " Sum([Project].[Title]) AS SumTitle," _
& " Sum([Project].[Duration]) AS SumDuration," _
& " Sum([Project].[Info]) AS SumInfo," _
& " Sum([Customer].[NumCustomer]) AS TotalNumCustomer," _
& " Sum([Orders].[NumOrders]) AS TotalNumOrders," _
& " Sum([OrderInspection].[NumInspects]) AS TotalNumInspects," _
& " Sum([OrderInspection].[NumFails]) AS TotalNumFails," _
& " Sum([CustomerInspection].[NumInspects]) AS TotalNumCustomerInspectionInspects," _
& " Sum([CustomerInspection].[NumFails]) AS TotalNumCustomerInspectionFails," _
& " Sum([Measurements].[NumMeasurements]) AS TotalNumMeasurementss" _
& " FROM ((((((sale LEFT JOIN Project ON [sale].[saleId]=[Project].[saleId])" _
& " LEFT JOIN Customer ON [Project].[PrintId]=[Customer].[PrintId])" _
& " LEFT JOIN Orders ON [Project].[PrintId]=[Orders].[PrintId])" _
& " LEFT JOIN OrderInspection ON [Project].[PrintId]=[OrderInspection].[PrintId])" _
& " LEFT JOIN CustomerInspection ON [Project].[PrintId]=[CustomerInspection].[PrintId])" _
& " LEFT JOIN Measurements ON [Project].[PrintId]=[Measurements].[PrintId])" _
& " WHERE [sale].[DateTime] BETWEEN dbo.FormatDateTime((@StartDate), 'yyyy-mm-dd')" _
& " AND dbo.FormatDateTime((@Enddate),'yyyy-mm-dd')" _
& " GROUP BY " & vGroup & "" _
& " ORDER BY " & vGroup & ";"
SqlExecuteNonQuery(CommandText)
return
End Sub
I look forward to your comments and replies.
Thanks