Solved

Problem in passing a parameter to Strored Procedure?

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now