467,895 Members | 1,452 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,895 developers. It's quick & easy.

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
  • viewed: 2552
Share:
4 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jarrod Morrison | last post: by
7 posts views Thread by eholz1 | last post: by
5 posts views Thread by Dennis | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.