Solved

SQL If..End If - need help with syntax

Posted on 2013-01-18
6
435 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
  • 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 142

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
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 142

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

910 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

23 Experts available now in Live!

Get 1:1 Help Now