Avatar of hrvica5
hrvica5
Flag for Croatia asked on

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
Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon
costaf

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;

...
Scott Pletcher

What do you mean "doesn't work"?

Did the INSERT fail when you expected it to succeed?

Did the ALTER TABLE statement fail?
SOLUTION
HainKurt

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
HainKurt

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Scott Pletcher

You can't use "= NULL", you must use IS NULL:

if (@USER IS NULL OR  @USER='0')
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SOLUTION
HainKurt

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Scott Pletcher

Actually you said use one "or" the other.

ONLY the version with IS NULL is correct and works.
HainKurt

"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...
Scott Pletcher

>> "does not work" and "does not do the right thing" is different :) <<

Not necessarily.  Colloquially then can mean the same thing.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.