472,353 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 2747
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...
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...
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...
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...
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...
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...
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...
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...
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...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.