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 8938841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:43:25+00:00 2026-06-15T10:43:25+00:00

I have a dynamic SQL which sits inside a stored procedure, but when I

  • 0

I have a dynamic SQL which sits inside a stored procedure, but when I run the stored procedure I am not seeing any results. It is very odd, because when I strip out the SQL from the string, and just run it as an SQL Query I do get back results. I have tried getting the Dynamic SQL to print out so I could see what is going on, but this isn’t working either. Therefore, I am at a loss to see what I am doing wrong, and would kindly ask if anyone can see what is wrong. Below is the query:

SELECT @SQL = @SQL + 'Select Production_Site, CSN, Target, Action, Fail '
SELECT @SQL = @SQL + 'From syn_products prod, '
SELECT @SQL = @SQL + '(select Production_Site, CSN, SUM([Target]) AS Target,SUM([Action]) AS Action,SUM([Fail]) AS Fail '
SELECT @SQL = @SQL + ' from '
SELECT @SQL = @SQL + ' ( '
SELECT @SQL = @SQL + ' select Production_Site, value, Period, YEAR, week, CSN '
SELECT @SQL = @SQL + ' from t_Pqe_Grocery '
SELECT @SQL = @SQL + ' unpivot ( '
SELECT @SQL = @SQL + ' value '
SELECT @SQL = @SQL + ' for col in (Grocery_Packaging_And_Coding, Grocery_Measurable, '
SELECT @SQL = @SQL + ' Grocery_Appearance, Grocery_Aroma, '
SELECT @SQL = @SQL + ' Grocery_Flavour, Grocery_Texture)) unp '
SELECT @SQL = @SQL + ' ) src '
SELECT @SQL = @SQL + ' pivot '
SELECT @SQL = @SQL + ' ( '
SELECT @SQL = @SQL + ' count(value) '
SELECT @SQL = @SQL + ' for value in ([Target], [Action], [Fail]) '
SELECT @SQL = @SQL + ' ) piv '
SELECT @SQL = @SQL + ' where Production_Site IN ( ''' + @Site + ''') AND YEAR BETWEEN ' + CONVERT(varchar(50),CONVERT(BIGINT,@ToYear))+ 'AND '+ CONVERT(varchar(50),CONVERT(BIGINT,@FromYear))+ 'AND Period BETWEEN ' + CONVERT(varchar(50),CONVERT(BIGINT,@ToPeriod))+ ' AND '+ CONVERT(varchar(50),CONVERT(BIGINT,@FromPeriod))+ 'AND Week BETWEEN ' + CONVERT(varchar(50),CONVERT(BIGINT,@ToWeek))+ ' AND '+CONVERT(varchar(50),CONVERT(BIGINT,@FromWeek))+ ' GROUP BY Production_Site CSN'
SELECT @SQL = @SQL + ' ) pit'
SELECT @SQL = @SQL + ' WHERE prod.pProductCode = pit.CSN AND prod.pPowerBrand = ''POW'''
EXECUTE(@SQL)
  • 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-15T10:43:26+00:00Added an answer on June 15, 2026 at 10:43 am

    Sometimes formatting your query in a different way can help find any errors with your query. You were missing some spaces in your query string:

    declare @sql varchar(max)
    declare @Site varchar(10) = 'testSite'
    declare @ToYear int = 2010
    declare @FromYear int = 2012
    declare @ToPeriod int = 45
    declare @FromPeriod int = 56
    declare @ToWeek int = 10
    declare @FromWeek  int = 1
    
    SET @SQL =
     'Select Production_Site, CSN, Target, Action, Fail 
      From syn_products prod
      inner join 
      (
        select Production_Site, CSN, SUM([Target]) AS Target,SUM([Action]) AS Action,SUM([Fail]) AS Fail 
        from 
        ( 
          select Production_Site, value, Period, YEAR, week, CSN 
          from t_Pqe_Grocery 
          unpivot 
          ( 
            value 
            for col in (Grocery_Packaging_And_Coding, 
                        Grocery_Measurable, Grocery_Appearance, 
                        Grocery_Aroma, Grocery_Flavour, Grocery_Texture)
          ) unp 
        ) src 
        pivot
        ( 
          count(value)
          for value in ([Target], [Action], [Fail])
        ) piv 
        where Production_Site IN ( ''' + @Site + ''') 
          AND YEAR BETWEEN ' + CONVERT(varchar(50),CONVERT(BIGINT,@ToYear))+ ' AND '+ CONVERT(varchar(50),CONVERT(BIGINT,@FromYear))
          + ' AND Period BETWEEN ' + CONVERT(varchar(50),CONVERT(BIGINT,@ToPeriod))+ ' AND '+ CONVERT(varchar(50),CONVERT(BIGINT,@FromPeriod)) 
          + ' AND Week BETWEEN ' + CONVERT(varchar(50),CONVERT(BIGINT,@ToWeek))+ ' AND '+CONVERT(varchar(50),CONVERT(BIGINT,@FromWeek))
        + ' GROUP BY Production_Site CSN
      ) pit
         on prod.pProductCode = pit.CSN 
      where prod.pPowerBrand = ''POW'''
    
    select @sql
    

    This is now printing — See SQL Fiddle with Demo — I also changed the query to use ANSI join syntax instead of comma separated joins.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a messy stored procedure which uses dynamic sql. I can debug it
I have a stored procedure which executes some dynamic sql, shown below. I've tried
I have a Stored Procedure which executes some dynamic SQL. I want to use
I have a stored procedure which is building a dynamic sql query and then
I have a stored procedure which generates and executes a piece of dynamic T-SQL
I have a dynamic sql statement that may or may not return any results.
I have a dynamic SQL statement I've created in a stored procedure. I need
I have created a procedure in dynamic SQL which has a select statement and
I have a big procedure which consists mostly dynamic SQL. I am having issues
We have a ton of SQL Server stored procedures which rely on dynamic SQL.

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.