Solved

SQL syntax

Posted on 2011-03-15
10
321 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
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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

785 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