Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 1st, 2004, 11:45 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , NY, USA.
Posts: 141
Thanks: 1
Thanked 0 Times in 0 Posts
Default Passing a parameter value to Stored Procedure

Hi All,

I am using VB 6.0 with SQL Server 2000
and I am trying to pass a string value to a stored prcodeure to use
in "WHERE" clause.

Sample:

In VB:
strWhere = "StateID = 1"

By using the "Command Object", I pass "strWhere" parameter

---------------------------------------------
In Stored Procedure:
create up_parmSel_state
   (
    @strWhere as varchar(255)
    )
as
 select *
   from State_T
   where @strWhere

* That's what I am trying to accomplish, but it does not work.
* I know that there is an other way by using some type of SQL Function to make it work in the Stored Procedure,
but I don't remember it now.

Thank you in advance.
__________________
MCinar

Love all the creatures because of the creator.
Reply With Quote
  #2 (permalink)  
Old October 1st, 2004, 10:44 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

try

CREATE PROCEDURE up_parmSel_state
   (@strWhere as varchar(255))AS
SELECT *
FROM state_t
WHERE <column> = @strWhere

go

EXEC up_parmSel_state @strWhere= 1
GO



Reply With Quote
  #3 (permalink)  
Old October 1st, 2004, 11:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , NY, USA.
Posts: 141
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you for reply.
What I need to try is to also pass "actual column id" with the value. In this case, "State_ID" is my column name and the value is "1".
The reason that I need this type of logic is to be able to pass multiple columns with their values based on the filtering conditions.

For Example;

In Visual Basic:

I have "State" and "Industry" options for user to choose. Let's say a user picks "State" but not "Industry". In this case my string variable
"strWhere" will be "State_ID = 10" Let's say #10
corresponds to "NY" from the pick list.
The advantage of having this type of logic prevents the "if" logic in the "Stored Procedure".

Thank you.

Reply With Quote
  #4 (permalink)  
Old October 2nd, 2004, 12:48 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

you probably have to use something like sp_executesql

post your table columns

I can get you this far:

create proc up_parmSel_state
as
DECLARE @IntVariable INT
DECLARE @SQLString VARCHAR(50)
DECLARE @ParmDefinition VARCHAR(50)
SET @SQLString = N'SELECT * FROM state_t WHERE stateID = @stateID'
SET @ParmDefinition = N'@stateID int'
/* Execute the string. */
SET @IntVariable = 1

EXECUTE sp_executesql @SQLString, @ParmDefinition, @stateID= @IntVariable
Reply With Quote
  #5 (permalink)  
Old October 2nd, 2004, 06:26 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

One small thing, you're better declaring the strings as NVARCHAR originally as that's what sp_executesql expects rather than convert on route.

--

Joe
Reply With Quote
  #6 (permalink)  
Old October 2nd, 2004, 07:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , NY, USA.
Posts: 141
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you jemacc and joefawcett.

I will ask a similiar question;

Let's say,

create procedure up_parmSel_State
   (
   @TableName as varchar(255)
   )
as
  select *
     from @TableName
-------------------------------
execute up_parmSel_State "State_T"

I know that this won't work.
I need to use different type of sql statement to make it work. But, I don't remember it.

Thanks in advance.



Reply With Quote
  #7 (permalink)  
Old October 3rd, 2004, 12:19 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Caution using a variable for your table name causes security risk

use the below adding your where clause

create proc up_parmSel_state
as
declare @SQL varchar(255), @tblname sysname

SELECT @tblname = 'state_t'

SELECT @SQL = 'SELECT * FROM ' + @tblname
SELECT @SQL = @SQL + ' WHERE (stateID) = 12345'
EXEC(@SQL)
Reply With Quote
  #8 (permalink)  
Old October 3rd, 2004, 08:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , NY, USA.
Posts: 141
Thanks: 1
Thanked 0 Times in 0 Posts
Default

This is a very good example and it will work for me.

Thank you, Jemacc.


Reply With Quote
  #9 (permalink)  
Old October 3rd, 2004, 11:01 AM
Friend of Wrox
 
Join Date: Nov 2003
Location: Lehigh Acres, FL, USA.
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

Here is the alternative to your original question

create proc up_parmSel_state
as
declare @SQL varchar(255),@state_ID varchar(30) @tblname sysname

SELECT @tblname = 'state_t'

SELECT @SQL = 'SELECT * FROM ' + @tblname
SELECT @SQL = @SQL + ' WHERE (state_ID) = ' + @state_ID'
EXEC(@SQL)



Reply With Quote
  #10 (permalink)  
Old October 3rd, 2004, 09:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , NY, USA.
Posts: 141
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you jemacc.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
usin g stored procedure with parameter Sheraz Khan ASP.NET 2.0 Basics 1 September 5th, 2007 12:27 AM
Query Parameter of a Stored Procedure tarang SQL Server 2000 4 July 25th, 2007 11:43 AM
pass null to the parameter in the stored procedure kokila BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 July 12th, 2007 03:02 AM
How to pass a parameter to a stored procedure? MaxGay2 VB.NET 2002/2003 Basics 1 November 8th, 2006 02:48 PM
passing table as a parameter to stored procedure pankaj_daga SQL Server 2000 7 September 30th, 2003 05:11 AM



All times are GMT -4. The time now is 08:04 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.