How to do nothing in SQL Server [duplicate]

2020-06-30 09:13发布

问题:

This question already has answers here:
Closed 8 years ago.

Possible Duplicate:
Empty statement in T-SQL

How can I get this to compile in SQL Server?

IF @value IS NULL
BEGIN
  -- I don't want to do anything here
END

回答1:

You mean it fails because of the empty BEGIN-END? do something meaningless but syntactically valid if for some reason you cant remove the block;

IF @value IS NULL
BEGIN
  set @value=@value -- or print 'TODO' etc
END


回答2:

Based on the idea from this thread:

IF @value IS NULL
BEGIN
  WAITFOR DELAY '00:00:00'
END

Should be noted (didn't know at first) that, like PRINT, this method is not universal. In particular, it cannot be used in functions. Use other suggestions when you need to add a NO-OP somewhere in a function.



回答3:

My first answer.

IF @value IS NULL BEGIN 
    goto a a:
END 

Second answer after thinking a bit

IF @value IS NULL BEGIN 
    SET
END 


回答4:

IF @value IS NULL
BEGIN
  -- I don't want to do anything here
  Print 'What a waste of time'
END


回答5:

The only things I can think of are operations that are very fast and don't affect disk. The easiest and fastest are probably variable declarations and assignments...

DECLARE @t int

OR

SET @t=@t


回答6:

The question in my mind is: why would you put that in your code? If the reason is that you have an else block, then flip the condition to "if @value is not null". By itself though, this block doesn't do anything.