Solved

SQL If..End If - need help with syntax

Posted on 2013-01-18
6
456 Views
Last Modified: 2013-01-18
Hi Please help me :-)

I can't get this to sproc to work, it keeps saying:

Incorrect syntax near the keyword then
Incorrect syntax near the keyword set
Incorrect syntax near GO

[spListProductsByCatOrAll]
(
@catId int
)
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
    declare @sql nvarchar(max)
   
    -- Insert statements for procedure here
      set @sql = 'SELECT Product_ID, Description, Cat_ID, Product_Title,Created_By FROM Products'
    If @catId <> 0 then
         set @sql = @sql + ' WHERE Cat_ID = '+@catId
      End If
    set @sql = @sql +' ORDER BY Sequence'
      Execute(@sql)

END
GO
0
Comment
Question by:Lorna70
[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
  • 2
  • 2
6 Comments
 
LVL 25

Assisted Solution

by:Lee Savidge
Lee Savidge earned 50 total points
ID: 38792616
If @catId <> 0
    set @sql = @sql + ' WHERE Cat_ID = '+@catId
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 38792665
simplest version, no else, 1 statement:
IF <condition> 
   <statement 

Open in new window


with a simple else
IF <condition> 
   <statement
  ELSE [statement] 

Open in new window


and if you need to have more than 1 statement:
IF <condition> 
  BEGIN
   <statement >
   <statement >
   <statement >
  END 
 ELSE
 BEGIN
   <statement >
   <statement >
   <statement >
 END 

Open in new window

0
 

Author Comment

by:Lorna70
ID: 38792667
OK thanks - I removed 'then' but still get:

Incorrect syntax near the keyword set
Incorrect syntax near GO
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38792674
please see my comment. in t-sql, there is no "end if" as such.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 38792685
Remove the END IF.

[spListProductsByCatOrAll]
(
@catId int
)
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
    declare @sql nvarchar(max)
   
    -- Insert statements for procedure here
      set @sql = 'SELECT Product_ID, Description, Cat_ID, Product_Title,Created_By FROM Products'
    If @catId <> 0
       set @sql = @sql + ' WHERE Cat_ID = '+@catId

    set @sql = @sql +' ORDER BY Sequence'
      Execute(@sql)

END
GO
0
 

Author Comment

by:Lorna70
ID: 38793340
Thanks - I had to remove the GO aswell and now it works fine :-)
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

733 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