Do you only have a comment in the block of your SQL statement?

I would just like to add a comment to my if-statement block, but I get an error when I try. I want to be more like Steve McConnell.

declare @ConstraintName varchar(255)
set @ConstraintName = 'PK_Whatever'

IF LEFT(@ConstraintName, 2) = 'PK'
BEGIN
    --can't drop primary keys
END

The error I get is:

Incorrect syntax near 'END'.

If I add something after the comment, i.e. PRINT @ConstraintNameIt works great.

+3
source share
9 answers

No, you cannot have an empty if block (or one that contains only comments).

You do not say why you need it. If you are just trying to comment on the contents of the if for debugging, you should comment on all if.

+5
source

SELECT NULL . , -, , :

IF LEFT(@ConstraintName, 2) = 'PK'
BEGIN
  DECLARE @Dummy bit -- Do nothing here, but this is required to compile
END
+4

SQL Server, Oracle PL/SQL NULL , :

BEGIN
  -- This is a comment
  NULL;
END
+3

: SQL Server

BEGIN
  DONOTHING:
END

, .

+2

, , . , , , /*... */ .

+1

. , . . .

+1

"" ( Charles Graham), if-statement ( BlackWasp), ..end, ( GiLM).

, ?

declare @ConstraintName varchar(255)
set @ConstraintName = 'PK_Whatever'

--can't drop primary keys
IF LEFT(@ConstraintName, 2) = 'PK'
BEGIN
    --do nothing here
    DECLARE @Dummy bit --required to compile
END
+1

SQL- , ? , ,

If left(@constraintname,2 <> 'PK'
BEGIN
     -- Drop your constraint here
     ALTER TABLE dbo.mytable DROP constraint ... -- etc
END
0

, , , , , < > 'PK'?

-- drop only if not primary
IF LEFT (@ConstraintName, 2) <> 'PK'
BEGIN
    --do something here
END
0

Source: https://habr.com/ru/post/1697538/


All Articles