views:

394

answers:

9

I'd like to just put in a comment in the block of my if-statement, 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 @ConstraintName, it works fine.

+3  A: 

I can't say for sure in SQL Server, but in Oracle PL/SQL you would put a NULL statement in a block that you want to do nothing:

BEGIN
  -- This is a comment
  NULL;
END
Dave Costa
Updated the title. I'm looking to just have a comment in the clause.
Even Mien
Yeah, what I'm saying is you can put the comment, and include the NULL statement (if it exists in SQL Server) to get around the compilation error. I'll edit my example to make this clearer
Dave Costa
+5  A: 

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

You don't say why you would want this. If you are just trying to comment out the contents of the if for debugging, you should comment the entire if.

BlackWasp
+1  A: 

No, I don't think you can. If you want to temporarily comment that out, you'll probably need to just put a /* ... */ around the entire statement.

Dana
No, I need the statement.
Even Mien
+1  A: 

It's not the comment. It's that you have an empty if block. You have to have at least one statement in there. Putting in a print statement might be your best bet.

Charles Graham
A: 

You could always

SELECT NULL
Ricardo C
Usually a bad idea, as it'll give you a resultset
Mark Brackett
A: 

Since you can't have an "empty" blocks (thanks Charles Graham), I'll place a comment above the if-statement for the intention of the conditional (thanks BlackWasp), and then have a comment within the begin..end block that describes a dummy declare (thanks GiLM).

Do you think this is how I should comment the code?

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
Even Mien
A: 

Would it not be better to design your SQL statement around items you do wish to drop constraints for? So If you wish to remove the ability for this then

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

SELECT NULL will generate a result-set and could affect client apps. Seems better to do something that will have no effect, like:

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

i know it doesn't answer your original question as to whether you can place just a comment inside a block, but why not inverse your conditional so the block only executes if <> 'PK'?

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