• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

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
0
hrvica5
Asked:
hrvica5
  • 4
  • 4
3 Solutions
 
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
 
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
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
HainKurtSr. System AnalystCommented:
here is the syntax

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

no then, no end if
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now