Link to home
Start Free TrialLog in
Avatar of CEGE
CEGEFlag for Spain

asked on

SQL syntax

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

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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

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
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
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.
SOLUTION
Avatar of Roman Gherman
Roman Gherman
Flag of Moldova, Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEGE

ASKER

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
Sorry,

I am not sure I can understand you

You can find me on: www.extremedev.blogspot.com
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEGE

ASKER

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.
Avatar of CEGE

ASKER

thanks for the help.