Solved

sql if

Posted on 2012-03-15
9
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 2

Expert Comment

by:costaf
ID: 37725990
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:Scott Pletcher
ID: 37726062
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:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 300 total points
ID: 37726113
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Accepted Solution

by:
Huseyin KAHRAMAN earned 300 total points
ID: 37726117
here is the syntax

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

no then, no end if
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 37726463
You can't use "= NULL", you must use IS NULL:

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

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 300 total points
ID: 37726508
"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:Scott Pletcher
ID: 37726541
Actually you said use one "or" the other.

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

Expert Comment

by:Huseyin KAHRAMAN
ID: 37726558
"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:Scott Pletcher
ID: 37726708
>> "does not work" and "does not do the right thing" is different :) <<

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

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MS SQL Sever Import/export problem 7 51
SQL works but want to get the XML node data separately 11 30
SQL Get Store Procedure Column Name As Row 5 41
SQL query 7 18
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

726 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