Solved

Incorrect syntax near the keyword then

Posted on 2014-11-26
2
550 Views
Last Modified: 2014-11-26
I have a sql stored procedure, and I am trying to add an if statement to the sql script. however I keep getting an error message of : "Incorrect syntax near the keyword 'THEN'.  

I then tried making the most simple if statement shown below, and I still get the same error message.

IF 'A' = 'A' THEN SELECT 'Succes'

What am I doing wrong?
0
Comment
Question by:brgdotnet
[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
2 Comments
 
LVL 70

Accepted Solution

by:
Qlemo earned 250 total points
ID: 40467936
There is no THEN in T-SQL:
IF 'A' = 'A'
  SELECT 'Succes'
ELSE
  SELECT 'Failure'

Open in new window

0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 40467937
Copy-paste the entire T-SQL statement into this question please.

you can't conditionally SELECT a column or not in a SQL statement, but you can conditionally pass a value, like..

SELECT 
   CASE WHEN orange = banana THEN 'Success' ELSE 'Fail' END as status

Open in new window

0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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