Solved

SQL Server Stored Procedure Syntax

Posted on 2010-08-26
6
206 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

758 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

20 Experts available now in Live!

Get 1:1 Help Now