473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Parameterized Query

Rob
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>;initia l 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

Nov 21 '05 #1
3 1383
Hi Rob, t's not clear which line causes the exception, but the following may
help narrow down the problem.

1) If you use SQL Profiler, you should be able to see the exact syntax sent
to the server. This will allow you to identify if it's SQL or the code
causing the exception

2) You can simplify the procedure to be this:

ALTER PROCEDURE GetFinancialData @SearchDate VARCHAR(50) AS
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) OR (@SearchDate = 'ALL'))
ORDER BY d.[Date], r.Retailer ASC

Sorry it's not a difinitive answer for you.
Regards

"Rob" <Ro*@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
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>;initia l 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

Nov 21 '05 #2
Try

Dim cmd As New SqlCommand("GetFinancialData @SearchDate", cn)

You set the parameter later, but you never specified the parameter in your
SqlCommand.

"Rob" <Ro*@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
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>;initia l 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

Nov 21 '05 #3
I'm new at using stored procedures and was wondering if it was possible to
create a stored procedure and add it to a database in runtime code. For
example, I create a database in Access at my applicaiton's start-up then if
it can't find the database used by the applicaiton, it creates a new one. I
would like to use stored procedures but can't figure out how to create one
and add it to the database. An example would be helpful if there is a way to
do it. Thanks.

"JohnFol" wrote:
Hi Rob, t's not clear which line causes the exception, but the following may
help narrow down the problem.

1) If you use SQL Profiler, you should be able to see the exact syntax sent
to the server. This will allow you to identify if it's SQL or the code
causing the exception

2) You can simplify the procedure to be this:

ALTER PROCEDURE GetFinancialData @SearchDate VARCHAR(50) AS
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) OR (@SearchDate = 'ALL'))
ORDER BY d.[Date], r.Retailer ASC

Sorry it's not a difinitive answer for you.
Regards

"Rob" <Ro*@discussions.microsoft.com> wrote in message
news:99**********************************@microsof t.com...
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>;initia l 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


Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gary b | last post by:
Hello When I use a PreparedStatement (in jdbc) with the following query: SELECT store_groups_id FROM store_groups WHERE store_groups_id IS NOT NULL AND type = ? ORDER BY group_name
8
by: deko | last post by:
I'm trying to open a Recordset based on a parameterized query. I'm kind of new to parameterized queries, so I'm sure I'm missing something simple. Set qdfs = db.QueryDefs Set qdf =...
2
by: deko | last post by:
Is it possible to build a parameterized query from another parameterized query? I've tried two variations of this and can't seem to get it to work (using DAO). Any suggestions welcome! I...
11
by: anony | last post by:
Hello, I can't figure out why my parameterized query from an ASP.NET page is dropping "special" characters such as accented quotes & apostrophes, the registered trademark symbol, etc. These...
8
by: Roland Hall | last post by:
In Access you use "*" + + "*", + can be replaced with & Calling a parameterized query in Access requires % be used in place of *, however, all that I have read show dynamic SQL passed to Access: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.