473,780 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.@Fro ntpageProduct" 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.@va riable_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
@FrontpageProdu ct varchar,
@FrontpageDay varchar
AS
SELECT * FROM Products LEFT JOIN FrontPage ON Products.Code =
FrontPage.@Fron tpageProduct WHERE FrontPage.theDa y = @FrontPageDay
GO
=============== ==============

Again, thank you in advance for any help.
Dave
Jul 20 '05 #1
4 2859
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
@FrontpageProdu ct varchar(30),
@FrontpageDay varchar(30)
AS
SELECT *
FROM Products
LEFT JOIN FrontPage ON
Products.Code = @FrontpageProdu ct
WHERE FrontPage.theDa y = @FrontPageDay
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Toonman" <to*****@NOSPAM toonman.com> wrote in message
news:pK******** ************@ne ws1.news.adelph ia.net...
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.@Fro ntpageProduct" 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.@va riable_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
@FrontpageProdu ct varchar,
@FrontpageDay varchar
AS
SELECT * FROM Products LEFT JOIN FrontPage ON Products.Code =
FrontPage.@Fron tpageProduct WHERE FrontPage.theDa y = @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
@FrontPageProdu ct 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
@FrontPageProdu ct 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 @FrontPageProdu ct.

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

Products.Code = FrontPage.featu redItem

....it works fine. When I try to pass "featuredIt em" through the
@FrontpageProdu ct variable...

Products.Code = FrontPage.@Fron tpageProduct

.... 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*******@nosp am-earthlink.net> wrote in message
news:PZ******** **********@news read3.news.atl. earthlink.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
@FrontpageProdu ct varchar(30),
@FrontpageDay varchar(30)
AS
SELECT *
FROM Products
LEFT JOIN FrontPage ON
Products.Code = @FrontpageProdu ct
WHERE FrontPage.theDa y = @FrontPageDay
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Toonman" <to*****@NOSPAM toonman.com> wrote in message
news:pK******** ************@ne ws1.news.adelph ia.net...
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.@Fro ntpageProduct" 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.@va riable_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
@FrontpageProdu ct varchar,
@FrontpageDay varchar
AS
SELECT * FROM Products LEFT JOIN FrontPage ON Products.Code =
FrontPage.@Fron tpageProduct WHERE FrontPage.theDa y = @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.htm l> 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*****@NOSPAM toonman.com> wrote in message
news:mT******** ************@ne ws1.news.adelph ia.net...
Dan,

I appreciate your help. The problem I'm still having is that the
@FrontPageProdu ct 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
@FrontPageProdu ct 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 @FrontPageProdu ct.

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

Products.Code = FrontPage.featu redItem

...it works fine. When I try to pass "featuredIt em" through the
@FrontpageProdu ct variable...

Products.Code = FrontPage.@Fron tpageProduct

... 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*******@nosp am-earthlink.net> wrote in message
news:PZ******** **********@news read3.news.atl. earthlink.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
@FrontpageProdu ct varchar(30),
@FrontpageDay varchar(30)
AS
SELECT *
FROM Products
LEFT JOIN FrontPage ON
Products.Code = @FrontpageProdu ct
WHERE FrontPage.theDa y = @FrontPageDay
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Toonman" <to*****@NOSPAM toonman.com> wrote in message
news:pK******** ************@ne ws1.news.adelph ia.net...
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.@Fro ntpageProduct" 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.@va riable_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
@FrontpageProdu ct varchar,
@FrontpageDay varchar
AS
SELECT * FROM Products LEFT JOIN FrontPage ON Products.Code =
FrontPage.@Fron tpageProduct WHERE FrontPage.theDa y = @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*******@nosp am-earthlink.net> wrote in message
news:vL******** *****@newsread3 .news.atl.earth link.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.htm l> 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*****@NOSPAM toonman.com> wrote in message
news:mT******** ************@ne ws1.news.adelph ia.net...
Dan,

I appreciate your help. The problem I'm still having is that the
@FrontPageProdu ct 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
@FrontPageProdu ct 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 @FrontPageProdu ct.

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

Products.Code = FrontPage.featu redItem

...it works fine. When I try to pass "featuredIt em" through the
@FrontpageProdu ct variable...

Products.Code = FrontPage.@Fron tpageProduct

... 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*******@nosp am-earthlink.net> wrote in message
news:PZ******** **********@news read3.news.atl. earthlink.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
@FrontpageProdu ct varchar(30),
@FrontpageDay varchar(30)
AS
SELECT *
FROM Products
LEFT JOIN FrontPage ON
Products.Code = @FrontpageProdu ct
WHERE FrontPage.theDa y = @FrontPageDay
GO

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Toonman" <to*****@NOSPAM toonman.com> wrote in message
news:pK******** ************@ne ws1.news.adelph ia.net...
> 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.@Fro ntpageProduct" 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.@va riable_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
> @FrontpageProdu ct varchar,
> @FrontpageDay varchar
> AS
> SELECT * FROM Products LEFT JOIN FrontPage ON Products.Code =
> FrontPage.@Fron tpageProduct WHERE FrontPage.theDa y = @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
3303
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 find much info on it. Basically the procedure takes the machinename and username supplied and searches a table or two for some matches and this part works great. The only problem i have is that with the app that ties in with the procedure returns...
4
13468
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 not work. I've been trying to get this to work with converting the UDF to a procedure, but I'm having no luck. Here is the background on what I'm trying to accomplish. I need to perform a sub-identity on a table, I have the normal identity set,...
12
10411
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 @salesdate as varchar(20),
9
2468
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 through the application in Visual Studio .NET 2003 the application an exception when it executes the query.
7
5842
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 procedure from a PHP program, and run a stored procedure. I have yet to figure out the proper way to do this. My stored procedures work fine from the mysql command line using syntax: "call sp_min_record (101);"
5
3629
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 '--------------------------------------------------------------------------­-------------------------------- Before - This Works without a paramater '--------------------------------------------------------------------------­--------------------------------
20
6305
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: The correct table could either be dbo.MyTable or zzz.MyTable. I want the user to enter the name of the schema as a parameter of the procedure at run-time.
6
7283
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 (confirmation) declared in page. Dim RsSp , SQLSp Set RsSp = Server.CreateObject("ADODB.Recordset") SQLSp = "Declare @confirm varchar(1)" SQLSp = SQLSp & "Exec SendMsg_proc "& "'" & UniCode &"' , '" & DintUserId &"' , '" & DintOrg_id &"' ,...
2
2820
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 the role 'db_generaluser' to the user created in step 2. The login name and password for the login to be created will be supplied from externally through input parameters. If this procedure executes successfully it returns 0 else 1 to the caller...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.