Passing text parameters including quotes in SQL Server

Posted on 2012-08-20
Last Modified: 2012-08-20
I'm sure this will be a straightforward one for someone to answer, but my brain is about to melt trying to get this to work.

I have a stored procedure that accepts 3 parameters:

DECLARE @cTableName varchar(50)
DECLARE @cCriteria nvarchar(1000)
DECLARE @iRowsAffected int

-- TODO: Set parameter values here.

EXECUTE [dbo].[spDeleteRows_IdentifyTables]
  ,@iRowsAffected OUTPUT

I want to pass it a string including a piece of text:
DECLARE @iRowsAffected int
EXEC spDeleteRows_IdentifyTables 'Claim', 'DateClosed < '01/01/2008'', @iRowsAffected OUTPUT

This generates a syntax error because I'm not escaping the quotes correctly.

Question by:reticentKoala
    LVL 142

    Accepted Solution

    duplicate the single quotes:
    DECLARE @iRowsAffected int
    EXEC spDeleteRows_IdentifyTables 'Claim', 'DateClosed < ''01/01/2008''', @iRowsAffected OUTPUT 

    Open in new window

    LVL 23

    Expert Comment

    use two single quote ( instead of one )

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

    734 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

    16 Experts available now in Live!

    Get 1:1 Help Now