Solved

SQL Server Stored Procedure Syntax

Posted on 2010-08-26
6
207 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 142

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

914 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

12 Experts available now in Live!

Get 1:1 Help Now