G
Guest
I have a problem I can't seem to solve although it should be easy. I've done
these many times before. But I keep getting an exception, "Line 1: Incorrect
syntax near 'GetFinancialData'."
This query works fine through Query Analyzer or if I do not set SQLParameter
for the query. I hope someone spots what I overlooked. Please let me know
if I have missed something. Thanks in advance.
Here is the query:
ALTER PROCEDURE GetFinancialData @SearchDate VARCHAR(50) AS
IF @SearchDate = 'All'
BEGIN
SELECT d.[Date], r.Retailer, i.GrossProfit AS Gross, i.NetProfit AS Net,
e.Value AS Incentives
FROM dbo.dimDate d
INNER JOIN dbo.factIncomes i ON i.DateNDX = d.DateNDX
INNER JOIN (SELECT DateNDX, RetailerNDX, SUM(Value) AS Value FROM
dbo.factExpenses GROUP BY DateNDX, RetailerNDX) e ON e.DateNDX = d.DateNDX
INNER JOIN dbo.dimRetailer r ON r.RetailerNDX = i.RetailerNDX
ORDER BY d.[Date], r.Retailer ASC
END
ELSE
BEGIN
SELECT d.[Date], r.Retailer, i.GrossProfit AS Gross, i.NetProfit AS Net,
e.Value AS Incentives
FROM dbo.dimDate d
INNER JOIN dbo.factIncomes i ON i.DateNDX = d.DateNDX
INNER JOIN (SELECT DateNDX, RetailerNDX, SUM(Value) AS Value FROM
dbo.factExpenses GROUP BY DateNDX, RetailerNDX) e ON e.DateNDX = d.DateNDX
INNER JOIN dbo.dimRetailer r ON r.RetailerNDX = i.RetailerNDX
WHERE d.[Date] = @SearchDate
ORDER BY d.[Date], r.Retailer ASC
END
Here is the code that calls the Stored Procedure:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cn As New SqlConnection("workstation
id=(local);uid=<Removed>;password=<Removed>;initial catalog=TestDB")
Dim cmd As New SqlCommand("GetFinancialData", cn)
Dim adpt As New SqlDataAdapter(cmd)
Dim returnTable As New DataTable
Dim stbFinancialData As New StringBuilder
Dim dr As DataRow
Dim drObject As Object
Dim blnFirstPass As Boolean = True
Dim prmDate As New SqlParameter
With prmDate
.ParameterName = "@SearchDate"
.SqlDbType = SqlDbType.VarChar
.Size = 50
.Direction = ParameterDirection.Input
.DbType = DbType.String
.Value = "All"
End With
adpt.SelectCommand.Parameters.Add(prmDate)
adpt.Fill(returnTable)
With stbFinancialData
For Each dr In returnTable.Rows
If .Length > 0 Then
.Append(",")
End If
blnFirstPass = True
For Each drObject In dr.ItemArray
If blnFirstPass = False Then
.Append(":")
End If
.Append(drObject)
blnFirstPass = False
Next
Next
End With
cn.Dispose()
cmd.Dispose()
adpt.Dispose()
TextBox1.Text = stbFinancialData.ToString
End Sub
these many times before. But I keep getting an exception, "Line 1: Incorrect
syntax near 'GetFinancialData'."
This query works fine through Query Analyzer or if I do not set SQLParameter
for the query. I hope someone spots what I overlooked. Please let me know
if I have missed something. Thanks in advance.
Here is the query:
ALTER PROCEDURE GetFinancialData @SearchDate VARCHAR(50) AS
IF @SearchDate = 'All'
BEGIN
SELECT d.[Date], r.Retailer, i.GrossProfit AS Gross, i.NetProfit AS Net,
e.Value AS Incentives
FROM dbo.dimDate d
INNER JOIN dbo.factIncomes i ON i.DateNDX = d.DateNDX
INNER JOIN (SELECT DateNDX, RetailerNDX, SUM(Value) AS Value FROM
dbo.factExpenses GROUP BY DateNDX, RetailerNDX) e ON e.DateNDX = d.DateNDX
INNER JOIN dbo.dimRetailer r ON r.RetailerNDX = i.RetailerNDX
ORDER BY d.[Date], r.Retailer ASC
END
ELSE
BEGIN
SELECT d.[Date], r.Retailer, i.GrossProfit AS Gross, i.NetProfit AS Net,
e.Value AS Incentives
FROM dbo.dimDate d
INNER JOIN dbo.factIncomes i ON i.DateNDX = d.DateNDX
INNER JOIN (SELECT DateNDX, RetailerNDX, SUM(Value) AS Value FROM
dbo.factExpenses GROUP BY DateNDX, RetailerNDX) e ON e.DateNDX = d.DateNDX
INNER JOIN dbo.dimRetailer r ON r.RetailerNDX = i.RetailerNDX
WHERE d.[Date] = @SearchDate
ORDER BY d.[Date], r.Retailer ASC
END
Here is the code that calls the Stored Procedure:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim cn As New SqlConnection("workstation
id=(local);uid=<Removed>;password=<Removed>;initial catalog=TestDB")
Dim cmd As New SqlCommand("GetFinancialData", cn)
Dim adpt As New SqlDataAdapter(cmd)
Dim returnTable As New DataTable
Dim stbFinancialData As New StringBuilder
Dim dr As DataRow
Dim drObject As Object
Dim blnFirstPass As Boolean = True
Dim prmDate As New SqlParameter
With prmDate
.ParameterName = "@SearchDate"
.SqlDbType = SqlDbType.VarChar
.Size = 50
.Direction = ParameterDirection.Input
.DbType = DbType.String
.Value = "All"
End With
adpt.SelectCommand.Parameters.Add(prmDate)
adpt.Fill(returnTable)
With stbFinancialData
For Each dr In returnTable.Rows
If .Length > 0 Then
.Append(",")
End If
blnFirstPass = True
For Each drObject In dr.ItemArray
If blnFirstPass = False Then
.Append(":")
End If
.Append(drObject)
blnFirstPass = False
Next
Next
End With
cn.Dispose()
cmd.Dispose()
adpt.Dispose()
TextBox1.Text = stbFinancialData.ToString
End Sub