Solved

Problem in passing a parameter to Strored Procedure?

Posted on 2004-09-13
2
183 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

763 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