473,372 Members | 1,103 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,372 software developers and data experts.

Stored Procedure Syntax Problem

I'm trying to use a couple of variables in a stored procedure. Things work
fine when I hard code the data into the variables and also work fine when I
use the variable in the WHERE clause and hard code data for the other
variable. So, I think I have a syntax problem when trying to use
"FrontPage.@FrontpageProduct" as seen in my example code below. I've tried
many variations... and either get syntax errors or end up with a result of
"no records." If somebody could assist me with the proper syntax for a
"table_name.@variable_name" reference it would be greatly appreciated.

The following procedure is called from a VB/.asp page. It's for a
storefront front page where product codes listed in the table "FrontPage"
are used to pull product data from table "Products."

=============================
CREATE PROCEDURE dbo.frontpage
@FrontpageProduct varchar,
@FrontpageDay varchar
AS
SELECT * FROM Products LEFT JOIN FrontPage ON Products.Code =
FrontPage.@FrontpageProduct WHERE FrontPage.theDay = @FrontPageDay
GO
=============================

Again, thank you in advance for any help.
Dave
Jul 20 '05 #1
4 2827
Variable names are not qualified. The scope is the variable declared in the
proc. Also, you need to specify the varchar length. For example::

CREATE PROCEDURE dbo.frontpage
@FrontpageProduct varchar(30),
@FrontpageDay varchar(30)
AS
SELECT *
FROM Products
LEFT JOIN FrontPage ON
Products.Code = @FrontpageProduct
WHERE FrontPage.theDay = @FrontPageDay
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Toonman" <to*****@NOSPAMtoonman.com> wrote in message
news:pK********************@news1.news.adelphia.ne t...
I'm trying to use a couple of variables in a stored procedure. Things work fine when I hard code the data into the variables and also work fine when I use the variable in the WHERE clause and hard code data for the other
variable. So, I think I have a syntax problem when trying to use
"FrontPage.@FrontpageProduct" as seen in my example code below. I've tried many variations... and either get syntax errors or end up with a result of
"no records." If somebody could assist me with the proper syntax for a
"table_name.@variable_name" reference it would be greatly appreciated.

The following procedure is called from a VB/.asp page. It's for a
storefront front page where product codes listed in the table "FrontPage"
are used to pull product data from table "Products."

=============================
CREATE PROCEDURE dbo.frontpage
@FrontpageProduct varchar,
@FrontpageDay varchar
AS
SELECT * FROM Products LEFT JOIN FrontPage ON Products.Code =
FrontPage.@FrontpageProduct WHERE FrontPage.theDay = @FrontPageDay
GO
=============================

Again, thank you in advance for any help.
Dave

Jul 20 '05 #2
Dan,

I appreciate your help. The problem I'm still having is that the
@FrontPageProduct variable itself isn't the qualifying value that I want to
compare to the value contained in Products.Code. I'm wanting to use the
@FrontPageProduct variable as the column reference to get the value from the
joined table FrontPage and ultimately use that value as the qualifier. So,
basically I'm feeding a column reference through @FrontPageProduct.

If I run the SP with the value hard coded into it...

Products.Code = FrontPage.featuredItem

....it works fine. When I try to pass "featuredItem" through the
@FrontpageProduct variable...

Products.Code = FrontPage.@FrontpageProduct

.... I get a syntax error.

Maybe I'm just steering down the wrong path here. Thanks again for any help
you might provide.

Dave
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:PZ******************@newsread3.news.atl.earth link.net...
Variable names are not qualified. The scope is the variable declared in the proc. Also, you need to specify the varchar length. For example::

CREATE PROCEDURE dbo.frontpage
@FrontpageProduct varchar(30),
@FrontpageDay varchar(30)
AS
SELECT *
FROM Products
LEFT JOIN FrontPage ON
Products.Code = @FrontpageProduct
WHERE FrontPage.theDay = @FrontPageDay
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Toonman" <to*****@NOSPAMtoonman.com> wrote in message
news:pK********************@news1.news.adelphia.ne t...
I'm trying to use a couple of variables in a stored procedure. Things work
fine when I hard code the data into the variables and also work fine when I
use the variable in the WHERE clause and hard code data for the other
variable. So, I think I have a syntax problem when trying to use
"FrontPage.@FrontpageProduct" as seen in my example code below. I've

tried
many variations... and either get syntax errors or end up with a result

of "no records." If somebody could assist me with the proper syntax for a
"table_name.@variable_name" reference it would be greatly appreciated.

The following procedure is called from a VB/.asp page. It's for a
storefront front page where product codes listed in the table "FrontPage" are used to pull product data from table "Products."

=============================
CREATE PROCEDURE dbo.frontpage
@FrontpageProduct varchar,
@FrontpageDay varchar
AS
SELECT * FROM Products LEFT JOIN FrontPage ON Products.Code =
FrontPage.@FrontpageProduct WHERE FrontPage.theDay = @FrontPageDay
GO
=============================

Again, thank you in advance for any help.
Dave


Jul 20 '05 #3
I see what you are trying to do now. Using variables as column names
necessitates that the SQL statement to be constructed dynamically. Check
out Erlands's article on dynamic SQL at
<http://www.sommarskog.se/dynamic_sql.html> for a discussion on the issues
involved.

IMHO, the need for dynamic SQL is often an indicator of application
architectural issues. If needed, this is better done in application code
rather than in T-SQL.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Toonman" <to*****@NOSPAMtoonman.com> wrote in message
news:mT********************@news1.news.adelphia.ne t...
Dan,

I appreciate your help. The problem I'm still having is that the
@FrontPageProduct variable itself isn't the qualifying value that I want to compare to the value contained in Products.Code. I'm wanting to use the
@FrontPageProduct variable as the column reference to get the value from the joined table FrontPage and ultimately use that value as the qualifier. So, basically I'm feeding a column reference through @FrontPageProduct.

If I run the SP with the value hard coded into it...

Products.Code = FrontPage.featuredItem

...it works fine. When I try to pass "featuredItem" through the
@FrontpageProduct variable...

Products.Code = FrontPage.@FrontpageProduct

... I get a syntax error.

Maybe I'm just steering down the wrong path here. Thanks again for any help you might provide.

Dave
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:PZ******************@newsread3.news.atl.earth link.net...
Variable names are not qualified. The scope is the variable declared in the
proc. Also, you need to specify the varchar length. For example::

CREATE PROCEDURE dbo.frontpage
@FrontpageProduct varchar(30),
@FrontpageDay varchar(30)
AS
SELECT *
FROM Products
LEFT JOIN FrontPage ON
Products.Code = @FrontpageProduct
WHERE FrontPage.theDay = @FrontPageDay
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Toonman" <to*****@NOSPAMtoonman.com> wrote in message
news:pK********************@news1.news.adelphia.ne t...
I'm trying to use a couple of variables in a stored procedure. Things

work
fine when I hard code the data into the variables and also work fine when
I
use the variable in the WHERE clause and hard code data for the other
variable. So, I think I have a syntax problem when trying to use
"FrontPage.@FrontpageProduct" as seen in my example code below. I've

tried
many variations... and either get syntax errors or end up with a result of "no records." If somebody could assist me with the proper syntax for
a "table_name.@variable_name" reference it would be greatly appreciated.

The following procedure is called from a VB/.asp page. It's for a
storefront front page where product codes listed in the table

"FrontPage" are used to pull product data from table "Products."

=============================
CREATE PROCEDURE dbo.frontpage
@FrontpageProduct varchar,
@FrontpageDay varchar
AS
SELECT * FROM Products LEFT JOIN FrontPage ON Products.Code =
FrontPage.@FrontpageProduct WHERE FrontPage.theDay = @FrontPageDay
GO
=============================

Again, thank you in advance for any help.
Dave



Jul 20 '05 #4
I'm sure you're right about my having architectural issues... I'm just
starting the turn down SQL street and anticipate a bumpy road for awhile.

Thanks again for your help,
Dave
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:vL*************@newsread3.news.atl.earthlink. net...
I see what you are trying to do now. Using variables as column names
necessitates that the SQL statement to be constructed dynamically. Check
out Erlands's article on dynamic SQL at
<http://www.sommarskog.se/dynamic_sql.html> for a discussion on the issues
involved.

IMHO, the need for dynamic SQL is often an indicator of application
architectural issues. If needed, this is better done in application code
rather than in T-SQL.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Toonman" <to*****@NOSPAMtoonman.com> wrote in message
news:mT********************@news1.news.adelphia.ne t...
Dan,

I appreciate your help. The problem I'm still having is that the
@FrontPageProduct variable itself isn't the qualifying value that I want to
compare to the value contained in Products.Code. I'm wanting to use the
@FrontPageProduct variable as the column reference to get the value from

the
joined table FrontPage and ultimately use that value as the qualifier.

So,
basically I'm feeding a column reference through @FrontPageProduct.

If I run the SP with the value hard coded into it...

Products.Code = FrontPage.featuredItem

...it works fine. When I try to pass "featuredItem" through the
@FrontpageProduct variable...

Products.Code = FrontPage.@FrontpageProduct

... I get a syntax error.

Maybe I'm just steering down the wrong path here. Thanks again for any

help
you might provide.

Dave
"Dan Guzman" <da*******@nospam-earthlink.net> wrote in message
news:PZ******************@newsread3.news.atl.earth link.net...
Variable names are not qualified. The scope is the variable declared in
the
proc. Also, you need to specify the varchar length. For example::

CREATE PROCEDURE dbo.frontpage
@FrontpageProduct varchar(30),
@FrontpageDay varchar(30)
AS
SELECT *
FROM Products
LEFT JOIN FrontPage ON
Products.Code = @FrontpageProduct
WHERE FrontPage.theDay = @FrontPageDay
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Toonman" <to*****@NOSPAMtoonman.com> wrote in message
news:pK********************@news1.news.adelphia.ne t...
> I'm trying to use a couple of variables in a stored procedure.
Things work
> fine when I hard code the data into the variables and also work fine

when
I
> use the variable in the WHERE clause and hard code data for the other > variable. So, I think I have a syntax problem when trying to use
> "FrontPage.@FrontpageProduct" as seen in my example code below. I've tried
> many variations... and either get syntax errors or end up with a

result
of
> "no records." If somebody could assist me with the proper syntax for a > "table_name.@variable_name" reference it would be greatly

appreciated. >
> The following procedure is called from a VB/.asp page. It's for a
> storefront front page where product codes listed in the table

"FrontPage"
> are used to pull product data from table "Products."
>
> =============================
> CREATE PROCEDURE dbo.frontpage
> @FrontpageProduct varchar,
> @FrontpageDay varchar
> AS
> SELECT * FROM Products LEFT JOIN FrontPage ON Products.Code =
> FrontPage.@FrontpageProduct WHERE FrontPage.theDay = @FrontPageDay
> GO
> =============================
>
> Again, thank you in advance for any help.
> Dave
>
>



Jul 20 '05 #5

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

Similar topics

4
by: Jarrod Morrison | last post by:
Hi All Im using a stored procedure on my sql server and am unsure of the syntax that i should use in it. Im pretty sure that there is a way to do what i want, but as yet i havent been able to...
4
by: marc | last post by:
I've been developing a stored procedure that uses a user defined function in the query portion of the procedure. However, since the end product needs to allow for dynamic table names, the UDF will...
12
by: Bill Nguyen | last post by:
What's the VB syntax to run the CR report using the following SP? I use CrystalreportViewer and ReportDocument. Thanks Bill Here's the SP in SQLserver 2K: CREATE proc mysp_ReportSubmission...
9
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug...
7
by: eholz1 | last post by:
Hello PHP group, Could someone help me out? I have PHP 5.2, Apache 2.0, and MySQL 5.0 running on Linux (Redhat Fedora Core 6). All that works fine. I would like to be able to "call" a stored...
5
by: Dennis | last post by:
Hi I'm trying to alter my stored procedure to take a parameter for the Database Name, but as usual the syntax is killing me. Thanks for any help Dennis ...
20
by: billmaclean1 | last post by:
I need to write a stored procedure that selects from a table and returns the result set. I don't always know the TableSchema that I need to use when qualifying the table at run-time Example:...
6
Soniad
by: Soniad | last post by:
Hello, I am excecuting a stored procedure in my ASP page , it has one out parameter (@confirm) . after executing the procedure i want to retreive this out parameter and assign it to variable...
2
by: priyamtheone | last post by:
I'm trying to create a stored procedure in MSSQL Server 2005 that'll perform the following jobs: 1) Create a login. 2) Create an user in TestDB database for the login created in step 1. 3) Assign...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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:
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...

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.