Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL syntax

Posted on 2011-03-15
10
Medium Priority
?
350 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 800 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 1200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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

604 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