3

Possible Duplicate:
How do I escape a single quote in sqlserver?

I got a script below that drop everything on the database from this link. It does error when I execute on this line.

SET @statement = '
IF(@type = 'F') or (@type = 'C') or (@type = 'D') or (@type='F') or (@type='K')

The reason is because the single quote. I want to know how can I fix this error?

/*** drop (pretty much) everything before rebuilding the database ***/
DECLARE
    OBJECTS CURSOR FOR SELECT
        so.name,
        so.type,
        so.type_desc,
        p.name AS parentName
    FROM
        sys.objects AS so
    LEFT JOIN sys.objects AS p ON so.parent_object_id = p.object_id
    WHERE
        so.schema_id = 1
    ORDER BY
        CASE
    WHEN so.type = 'F' THEN
        0
    WHEN so.type = 'TR' THEN
        1
    WHEN so.type = 'U' THEN
        2
    WHEN so.type = 'F' THEN
        3
    ELSE
        4
    END OPEN OBJECTS DECLARE
        @name AS nvarchar (MAX) DECLARE
            @type AS nvarchar (2) DECLARE
                @type_desc AS nvarchar DECLARE
                    @parentName AS nvarchar (MAX) DECLARE
                        @statement AS nvarchar (MAX) FETCH NEXT
                    FROM
                        OBJECTS INTO @name,
                        @type,
                        @type_desc,
                        @parentName
                    WHILE @@FETCH_STATUS = 0
                    BEGIN

                    SET @statement = '    IF(@type = ' F ')
BEGIN
PRINT ' DROPING FK : ' + @name + ' OF type ' + @type + ' (' + @type_desc + ') '
SET @statement = ' ALTER TABLE ' + @parentName + ' DROP CONSTRAINT ' + @name
EXECUTE(@statement)
END
ELSE IF (@type = ' TR ')
BEGIN
PRINT ' DROPING TRIGGER : ' + @name + ' OF type ' + @type + ' (' + @type_desc + ') '
SET @statement = ' DROP TRIGGER ' + @name
EXECUTE(@statement)
END
ELSE IF (@type = ' U ')
BEGIN
PRINT ' DROPING TABLE : ' + @name + ' OF type ' + @type + ' (' + @type_desc + ') '
SET @statement = ' DROP TABLE ' + @name
EXECUTE(@statement)
END
ELSE IF (@type = ' FN ')
BEGIN
PRINT ' DROPING FUNCTION : ' + @name + ' OF type ' + @type + ' (' + @type_desc + ') '
SET @statement = ' DROP FUNCTION ' + @name
EXECUTE(@statement)
END
ELSE
PRINT ' Didn 't drop object ' + @name + ' of type ' + @type + ' (' + @type_desc + ')'    FETCH NEXT
                    FROM
                        OBJECTS INTO @name,
                        @type,
                        @type_desc,
                        @parentName
                    END CLOSE OBJECTS DEALLOCATE OBJECTS
Community
  • 1
  • 1
Anonymous
  • 9,366
  • 22
  • 83
  • 133

3 Answers3

6

if you want to use single quote inside a prepared statement, escape it with another single quote, example,

SET @statement = 'world''s view';
SET @statement2 = 'world''s view';

from your example above

SET @statement = '
                IF(@type = ''F'') or (@type = ''C'')  or
                  (@type = ''D'') or (@type=''F'') or
                  (@type=''K'')'

-- the strings are all red.
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

Single quote is used to represent a string literal in SQL. If you need to explicitly insert a single quote , you should use double single quotes ('')

Rockstart
  • 2,337
  • 5
  • 30
  • 59
0

It should be like this:

SET @statement = 'IF(@type = ''F'') or (@type = ''C'') or (@type = ''D'') or (@type=''F'') or (@type=''K'')'

Raj

Raj
  • 10,653
  • 2
  • 45
  • 52