Solved

SQL Server Stored Procedure Syntax

Posted on 2010-08-26
6
209 Views
Last Modified: 2012-05-10
I am attempting to write code to build a dynamic query based on a number of parameter and get and error in my IF block

Incorrect syntax near the keyword 'BEGIN'.


CREATE PROCEDURE Ewdataviewlink 
(@sTable     VARCHAR(100),
 @sLnkFld    VARCHAR(100),
 @sDispFld   VARCHAR(100),
 @sDispFld2  VARCHAR(100),
 @sFilterFld VARCHAR(100),
 @sOrderBy   VARCHAR(100),
 @sOrderType VARCHAR(100),
 @bDistinct  AS BIT,
 @sFilter    VARCHAR(100))
AS
  DECLARE @sSql VARCHAR(500)

  -- Construct@sQL@statement
  SET @sSql = 'SELECT'

  IF bdistinct
    BEGIN
        SET @sSql = @sSql + ' DISTINCT'
    END

  SET @sSql = @sSql + ' [' + @sLnkFld + '], [' + @sDispFld + ']'

  IF @sDispFld2 <> ''
    BEGIN
        SET @sSql = @sSql + ', [' + @sDispFld2 + ']'
    END

  IF @sFilterFld <> ''
    BEGIN
        SET @sSql = @sSql + ', [' + @sFilterFld + ']'
    END

  SET @sSql = @sSql + ' FROM [' + @sTable + ']'

  IF @sFilter <> ''
    BEGIN
        SET @sSql = @sSql + ' WHERE ' + @sFilter
    END

  IF @sOrderBy <> ''
    BEGIN
        SET @sSql = @sSql + ' ORDER BY [' + @sOrderBy + '] ' + @sOrderType
    END

  EXEC(@sSql)

Open in new window

0
Comment
Question by:shelbyinfotech
6 Comments
 
LVL 13

Accepted Solution

by:
Philip Pinnell earned 500 total points
ID: 33533027
IF (bdistinct = 1)bit datatypes are not booleans just numerics that can be 0 or 1
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33533029
you have:

IF bdistinct

must be:

IF @bdistinct = 1
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 33533047
yes I missed the missing @
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 16

Expert Comment

by:vdr1620
ID: 33533059
Change this part of the query

IF @bdistinct = 1
    BEGIN
        SET @sSql = @sSql + ' DISTINCT'
0
 

Author Closing Comment

by:shelbyinfotech
ID: 33533062
Thank you
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 33533075
Thanks
0

Featured Post

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.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

860 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