Solved

Problem in passing a parameter to Strored Procedure?

Posted on 2004-09-13
2
186 Views
Last Modified: 2010-04-17
Hi,

I want to send a string to a stored procedure. the string is as follows:

A, B, C, D

I'm facing a problem in giving single quotes. How can I do it?

thanks in advance.
0
Comment
Question by:indiagenious
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 12042758
Hi indiagenious,

Double the single quotes is the simple answer, SQL (of any flavour, access included) will treat two consecutive single (or double) quote characters as a single one when stored in the database (or handled in a stored procedure). It is always useful to have a "fixup" procedure which in VB/ASP would be something like this:

Function FixUp(ByVal SQLString As String) As String
  FixUp = Replace(Replace(SQLString,"'","''"),"""","""""")
End Function

Call it with for example:

cnn.Execute "Insert Into MyTable ('" & Fixup("O'Reilly is a common name in Ireland") & "'")

To sort out the problem with the quotes.

Tim Cottee
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
LVL 3

Expert Comment

by:Validor
ID: 12049273
Is this a problem in CALLING the procedure with these parameters, or is it a problem with PASSING the list as a single string?  For example, I have a stored procedure that expects a variable number of parameters, each one a string.  I want to run a query to fetch all rows whose "datatype" field has one of those values in it.  There are 2 ways that I know to do this.  The former method has limits as to how many values can be passed, and the values must be quoted properly as described by TimCottee.  The latter method is recommended.

1) In MSSQL, you can execute plain SQL text by passing a string (containing SQL text) to the system stored procedure named sp_executesql.  Example:

create procedure findrecswiththesedatatypes
  @datatypes nvarchar(1000)
as
@declare sqltext nvarchar(1000)
set @sqltext ='select * from mytable where datatype=' + nvarchar
exec sp_executesql @sqltext


2) Create 2 tables.  One will have a record for each query.  The other will hold parameters for the query.  Instead of dealing with quotes and formatted strings converted to SQL text, just use a join with the parameters against the data to filter the data that you want.

create table requests (
  requestid autoint not null primary key,
  inserttime datetime not null
)

create table requestparms (
  parmid autoint not null primary key,
  requestid int not null,
  parmvalue varchar(25)
)

create proceduresp_ createrequest
  @requestid int out
as
insert into requests (inserttime) values (getdate())
set @requestid = @@scope_identity,

create procedure sp_addparm
  @requestid int,
  @parmvalue varchar(25)
as
insert into requestparms (requestid, parmvalue) values (@requestid, @parmvalue)

create procedure sp_queryrequest
  @requestid
as
select *
from mydata
inner join requestparms
on mydata.datatype = requestparms.parmvalue
where requestparms.requestid = @requestid


declare @requestid int
exec sp_createrequest(@requestid)
exec sp_addparm(@requestid, 'A')
exec sp_addparm(@requestid, 'B')
exec sp_addparm(@requestid, 'C')
exec sp_addparm(@requestid, 'D')
exec sp_queryrequest(@requestid)


Remember lastly to clean up these tables.  You can decide which parms and requests to delete by the insert time.  You may also delete them right away if necessary.
0

Featured Post

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question