SQL Server Stored Procedure Syntax

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

shelbyinfotechAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Philip PinnellConnect With a Mentor Commented:
IF (bdistinct = 1)bit datatypes are not booleans just numerics that can be 0 or 1
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you have:

IF bdistinct

must be:

IF @bdistinct = 1
0
 
Philip PinnellCommented:
yes I missed the missing @
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
vdr1620Commented:
Change this part of the query

IF @bdistinct = 1
    BEGIN
        SET @sSql = @sSql + ' DISTINCT'
0
 
shelbyinfotechAuthor Commented:
Thank you
0
 
Philip PinnellCommented:
Thanks
0
All Courses

From novice to tech pro — start learning today.