We help IT Professionals succeed at work.

stored procedure - create t-sql to replace the dreaded apostrophe

Bob Butcher
Bob Butcher asked
on
Medium Priority
2,165 Views
Last Modified: 2008-03-03
I have to create a stored procedure where I pass in 4 variables, each with different lengths, and I need to check to see if there is an apostrophe that I replace it with a double quote. I then need to insert the record into the table. If everything goes OK, I'd like to pass back a variable to my VB program indicating that the INSERT worked fine.

Could someone throw together the snippet of code on what would be required to put this all together and please a short comment on each step of what is going on.

Fairly new to creating stored procedures. Have always using the INSERT, UPDATE and DELETE in my VB code using ADO and am trying to convert everything to stored procedures.

Thanks in advance.
Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
unless you use dynamic sql, you don't need to check for quotes in the parameter variables.
So, just create a stored procedure like the code snippet, and pass your parameters as they are, and you will not have to worry about double quotes when you are working with stored procedure. Single quote wont give any issue when using SP. Look at the insert SP code snippet
Create Procedure InsertValues
@EmpID int,
@Name varchar(50),
@Firstname varchar(100)
Begin
       Insert Into TableName (EmpID, Name, FirstName) Values (@Empid, @Name, @FirstName)
End

Open in new window

Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
a sample code for the procedure attached.
note: if the procedure execution succeeded, the insert completed successfully. if any error was raised, the insert failed (and rolled back).

CREATE PROCEDURE proc_do_insert
 (@pk int
 ,@data_1 varchar(100)
 ,@data_2 varchar(200)
 ,@data_3 datetime
 )
AS
BEGIN
  INSERT INTO yourtable (pk, col1, col2, col3)
  VALUES (@pk, @data_1, @data_2, @data_3) 
END

Open in new window

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
note: the corresponding vb will probably be using ADO, in which case you check out the ADODB.Command (vb6) resp SqlCommand (vb.net with ado.net) object, and using the ADODB.Parameter (vb6) / SqlParameter (vb.net + ado.net) object to be able to work without the quotes problems...

Explore More ContentExplore courses, solutions, and other research materials related to this topic.