Solved

Problem in passing a parameter to Strored Procedure?

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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 265
Math Equation 23 149
Cygwin - GNU GPL License 1 57
Looking for reading material 2 44
This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn different types of Android Layout and some basics of an Android App.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

751 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