Solved

SQL syntax

Posted on 2011-03-15
10
303 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now