Solved

sql if

Posted on 2012-03-15
9
267 Views
Last Modified: 2012-06-27
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
Comment
Question by:hrvica5
  • 4
  • 4
9 Comments
 
LVL 2

Expert Comment

by:costaf
Comment Utility
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
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
What do you mean "doesn't work"?

Did the INSERT fail when you expected it to succeed?

Did the ALTER TABLE statement fail?
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 300 total points
Comment Utility
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
 
LVL 51

Accepted Solution

by:
HainKurt earned 300 total points
Comment Utility
here is the syntax

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

no then, no end if
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
You can't use "= NULL", you must use IS NULL:

if (@USER IS NULL OR  @USER='0')
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 300 total points
Comment Utility
"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
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Actually you said use one "or" the other.

ONLY the version with IS NULL is correct and works.
0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
"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
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
>> "does not work" and "does not do the right thing" is different :) <<

Not necessarily.  Colloquially then can mean the same thing.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now