sql if

Hi i need one ANSWER?

HOW TO CHECK IF THERE IS SOMETHING IN VALUE USER:

      if (@USER=NULL OR  @USER='0') then
         ALTER TABLE [TABLE] NOCHECK CONSTRAINT [FK_TABLE_USER]

IF THEN DOESN'T WORK



CREATE PROCEDURE [dbo].[TABLE_INSERT]
(
	@NAME varchar(3),
	@USER varchar(3),

)
AS
BEGIN

	SET NOCOUNT ON;

	BEGIN TRY
	
	if (@USER=NULL OR  @USER='0') then
	   ALTER TABLE [TABLE] NOCHECK CONSTRAINT [FK_TABLE_USER]

		-- Insert statements for procedure here
		INSERT INTO [TABLE]
				   ([NAME]
				   ,[USER]
				    
				VALUES
				   (@NAME,
				    @USER)					)
	END TRY
					
END

Open in new window


thx
hrvica5Asked:
Who is Participating?
 
HainKurtSr. System AnalystCommented:
here is the syntax

if (...)
begin
....
end;

no then, no end if
0
 
costafCommented:
i think you forgot to end if

probably you forgot to end if . you should do something like this:

...

 BEGIN
      var := var + 1;
      IF var = 100 THEN
            var := 0;
            COMMIT;
      END IF;

...
0
 
Scott PletcherSenior DBACommented:
What do you mean "doesn't work"?

Did the INSERT fail when you expected it to succeed?

Did the ALTER TABLE statement fail?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
HainKurtSr. System AnalystCommented:
use

if (@USER=NULL OR  @USER='0')
         ALTER TABLE [TABLE] NOCHECK CONSTRAINT [FK_TABLE_USER]

or

if (@USER is NULL OR  @USER='0')
         ALTER TABLE [TABLE] NOCHECK CONSTRAINT [FK_TABLE_USER]
0
 
Scott PletcherSenior DBACommented:
You can't use "= NULL", you must use IS NULL:

if (@USER IS NULL OR  @USER='0')
0
 
HainKurtSr. System AnalystCommented:
"You can't use "= NULL", you must use IS NULL:"

which is posted before :) actually that does not create any issue... but will not give the result author try to get...

@USER = NULL >>>> False, even if @USER is null...
0
 
Scott PletcherSenior DBACommented:
Actually you said use one "or" the other.

ONLY the version with IS NULL is correct and works.
0
 
HainKurtSr. System AnalystCommented:
"does not work" and "does not do the right thing" is different :)

if (@user=null)
... do something here
end;

above code is correct but the code inside never works since that condition gives false...

the original code posted by author gives syntax error :) So, I fixed syntax first, then gave the correct logical code :) combo...
0
 
Scott PletcherSenior DBACommented:
>> "does not work" and "does not do the right thing" is different :) <<

Not necessarily.  Colloquially then can mean the same thing.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.