Solved

SQL syntax

Posted on 2011-03-15
10
333 Views
Last Modified: 2012-08-13
Hello:

Can someone please help me with this SQL syntax. why is it giving me this error:

Server: Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'THEN'.
Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near 'ELSEIF'.


IF [g_entr_espec_est]=NULL THEN 
SELECT [dbo].[PreferenciasCliente].[EspecEst] from [dbo].[PreferenciasCliente] where [dbo].[PreferenciasCliente].[CodigoCliente] = [g_concat_codigo]
ELSEIF NOT([g_entr_espec_est]=NULL) THEN
select [dbo].[PreferenciasCliente].[EspecEst] from [dbo].[PreferenciasCliente] where [dbo].[PreferenciasCliente].[CodigoCliente] = [g_concat_codigo_dir]

Open in new window

0
Comment
Question by:CEGE
[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
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35137076
t-sql does not have ELSEIF ... only ELSE IF

what you actually see to want is this:

select c.[EspecEst] 
 from [dbo].[PreferenciasCliente] c
 where c.[CodigoCliente] = isnull( g.[g_concat_codigo_dir], g.[g_concat_codigo] )

Open in new window

0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35137083
IF [g_entr_espec_est] is NULL
begin
    SELECT [dbo].[PreferenciasCliente].[EspecEst] from [dbo].[PreferenciasCliente] where [dbo].[PreferenciasCliente].[CodigoCliente] = [g_concat_codigo]
end
else if [g_entr_espec_est] is not NULL)
begin
    select [dbo].[PreferenciasCliente].[EspecEst] from [dbo].[PreferenciasCliente] where [dbo].[PreferenciasCliente].[CodigoCliente] = [g_concat_codigo_dir]
end
0
 
LVL 9

Expert Comment

by:Roman Gherman
ID: 35137163
Hi,

YOu have some mistakes in your code, here is the correct one:

IF [g_entr_espec_est]=NULL 
SELECT [dbo].[PreferenciasCliente].[EspecEst] from [dbo].[PreferenciasCliente] where [dbo].[PreferenciasCliente].[CodigoCliente] = [g_concat_codigo]
ELSE IF NOT([g_entr_espec_est]=NULL)
select [dbo].[PreferenciasCliente].[EspecEst] from [dbo].[PreferenciasCliente] where [dbo].[PreferenciasCliente].[CodigoCliente] = [g_concat_codigo_dir]

Open in new window


You can find me on: www.extremedev.blogspot.com
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 25

Expert Comment

by:Lee Savidge
ID: 35137171
roma1123. Your code is still incorrect. You don't check for NULL using the = operator. You check for using "is". angelIII's code is the best answer.
0
 
LVL 9

Assisted Solution

by:Roman Gherman
Roman Gherman earned 200 total points
ID: 35137280
AHa,
yes, I forgot to change that

IF [g_entr_espec_est] IS NULL 
SELECT [dbo].[PreferenciasCliente].[EspecEst] from [dbo].[PreferenciasCliente] where [dbo].[PreferenciasCliente].[CodigoCliente] = [g_concat_codigo]
ELSE IF [g_entr_espec_est] IS NOT NULL
select [dbo].[PreferenciasCliente].[EspecEst] from [dbo].[PreferenciasCliente] where [dbo].[PreferenciasCliente].[CodigoCliente] = [g_concat_codigo_dir]

Open in new window



You can find me on: www.extremedev.blogspot.com
0
 
LVL 1

Author Comment

by:CEGE
ID: 35138792
thanks, this last code works well, but what would I need to do for it to execute based on another variable? like if espec_chk=0 then (execute code), but if espec_chk=0, then do nothng.

thanks.

joseph
0
 
LVL 9

Expert Comment

by:Roman Gherman
ID: 35138860
Sorry,

I am not sure I can understand you

You can find me on: www.extremedev.blogspot.com
0
 
LVL 25

Accepted Solution

by:
Lee Savidge earned 300 total points
ID: 35139250
That last code is identical to my code above except mine has the begin and end delimiters in the if/else block. angelIII is the best one for you. It does exactly what you want.

To answer you (second) question

if espec_chk<>0
begin
   -- execute code here
end

The above will not execute the code in the if block when then value of espec_chk is 0

Lee
0
 
LVL 1

Author Comment

by:CEGE
ID: 35149399
the deal is that I am using a BPM which has an SQL action tool, that executes SQL code and returns a value. What i've done is create two actions. One that goes to the table and pulls info in for a specific register, there may or may not be. Then In the BPM I evaluate it, and then in a second step, depending on chk value, it should pull in the generic information.

if [l_entr_espec_est_chk] =1 then
select [dbo].[PreferenciasCliente].[EspecEst] from [dbo].[PreferenciasCliente] where [CodigoCliente] = [g_codcli]

(but this is not accepted by the BPM SQL area)

thanks.
0
 
LVL 1

Author Closing Comment

by:CEGE
ID: 35181428
thanks for the help.
0

Featured Post

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!

Question has a verified solution.

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

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…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

730 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