Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7868355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:58:28+00:00 2026-06-03T00:58:28+00:00

I have to create a stored procedure using a Dynamic SQL solution because I

  • 0

I have to create a stored procedure using a Dynamic SQL solution because I need to include several nesting if conditions. I receive the following error when I run the code in Query Analyzeer.

Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'Procedure'.

The procedure I am trying to create follows:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF EXISTS (SELECT * FROM sys.objects 
WHERE object_id = OBJECT_ID(N'[dbo].[sp_Payments]') 
AND type in (N'P', N'PC'))
Drop procedure sp_Payments
BEGIN
DECLARE @SQL varchar(max),
    @categoryID smallint,
    @startDate datetime,
    @stopDate datetime,
    @debtCode tinyint,
    @izEscrow tinyint


Create Procedure sp_Payments
    (@categoryID smallint,
    @startDate datetime,
    @stopDate datetime,
    @debtCode tinyint,
    @izEscrow tinyint
    )
AS
BEGIN
Declare @Payments table( 
        paydate datetime, 
        principaldue float, 
        interestdue float, 
        debtid int, 
        debtname varchar(50), 
        debtnumber varchar(10), 
        fsrc varchar(40), 
        category varchar(40), 
        PayMonth tinyint,
        PayYear int
)
SET @SQL = '
insert into @Payments 
        select dtl.paydate, 
            dtl.principaldue, 
            dtl.interestdue, 
            dtl.debtid, 
            dmf.debtname, 
            dmf.debtnumber, 
            fsrc.fsrc, 
            app.category, 
            month(dtl.paydate) as PayMonth,
            case
                when month(PayDate) <= 6 then year(PayDate)
                else year(PayDate)+1  
            end "PayYear"
             from debtdetail dtl 
            inner join masterfile dmf 
            on dtl.debtid = dmf.debtid
            inner join categories app
            on dmf.categoryid = app.categoryid
            left outer join fsrc 
            on dmf.fsrcid = fsrc.fsrcid
            left outer join debtissues di
            on dmf.issueid = di.issueid
              where dtl.debtid in 
            (select debtid from masterfile
                where categoryid = @categoryID '

        IF @debtCode > 0    
            SET @SQL = @SQL + '
                AND codeid = @debtCode 
                '
            SET @SQL = @SQL + '
                ) 
                AND di.iscontingent = 0 
                '
            IF @stopDate = '' 
                SET @SQL = @SQL + '
                    and dtl.paydate >= @startDate 
                '
            ELSE 
                SET @SQL = @SQL + '
                    and dtl.paydate between  
                    @startDate AND @stopDate
                '
            IF @izEscrow = 0
                SET @SQL = @SQL + '
                    and dtl.isescrow = 0
                '
            SET @SQL = @SQL + '
            and (principaldue + interestdue) > 0 and dtl.active = 1
                    order by dtl.Paydate, dmf.DebtNumber '
EXEC @SQL
END

    SELECT * from @Payments

RETURN




END 

Any help would be appreciated. Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:58:29+00:00Added an answer on June 3, 2026 at 12:58 am

    Get rid of that dynamic sql, and do something like:

    AND (@debtCode = 0 OR codeId = @debtCode)
    ... and so on
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Oracle 10g and I have the following stored procedure: CREATE OR
I have a T-SQL stored procedure with the signature CREATE PROCEDURE MyProc @recordCount INT
I have a textbox using multiline mode and when I create the stored procedure
I have just created a report in Report Manager using a Stored Procedure which
I have this stored procedure: CREATE OR REPLACE PROCEDURE LIQUIDACION_OBTENER ( p_Cuenta IN NUMBER,
I have the fallowing stored procedure: CREATE PROCEDURE `get`(IN tb VARCHAR(50), IN id INTEGER)
I have this stored procedure for Oracle: create or replace procedure bns_saa_message_get() <--- PROBLEM
I have a stored procedure which returns a ref cursor as follows: CREATE OR
I have a stored procedure in which i want to create a user defined
I have a stored procedure with an IN OUT parameter declared like follows: create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.