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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:48:39+00:00 2026-06-18T06:48:39+00:00

declare @SQL nvarchar(max); with tbl1 as ( SELECT … ), tbl2 as ( SELECT

  • 0
declare @SQL nvarchar(max);
with tbl1 as
    (
    SELECT ...
    ),
tbl2 as
    (
    SELECT ...
    ),
tbl15 as
    (
    select [tbl1].[DT],
    [tbl1].[Kr_IL.BTS],
    [tbl2].[Kr_IL.CS],
    from [tbl1], [tbl2]
    where 
    [tbl1].[DT] = [tbl2].[DT] 
    and [tbl1].[DT] = [tbl3].[DT] 
    )

set @SQL = 'select [tbl15].[DT], '
if @tag1 = 1 set @SQL = @SQL + '[tbl15].[Kr_IL.BTS], '
    else set @SQL = @SQL + 'null as [Kr_IL.BTS], '
if @tag2 = 1 set @SQL = @SQL + '[tbl15].[Kr_IL.CS], '
    else set @SQL = @SQL + 'null as [Kr_IL.CS], ';
set @SQL = STUFF(@SQL, len(@SQL), 1, ' from [tbl15]')
exec (@SQL)

This is part of stored procedure script I have a problem with. The message is:

“Msg 156, Level 15, State 1, Procedure SP_select, Line 202 Incorrect
syntax near the keyword ‘set’.”.

If I write a standard select statement (with full set of columns) it works fine. But I need to “control” the columns (real data or null data according enable tags).
According error message the error point is:

set @SQL = 'select [tbl15].[DT], '

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-18T06:48:40+00:00Added an answer on June 18, 2026 at 6:48 am

    You’re getting this error because you’re not referencing the CTE in the query immediately after it (dynamic SQL doesn’t count!)

    Your variables for controlling the flow could be introduced into the script directly, rather than the need for dynamic SQL, by using a case statement:

    SELECT [DT],
           CASE
             WHEN @tag1 = 1 THEN [Kr_IL.BTS]
             ELSE NULL
           END AS BTS,
           CASE
             WHEN @tag2 = 1 THEN [Kr_IL.CS]
             ELSE NULL
           END AS CS
    FROM   tbl15
    

    So your whole script becomes:

    ;WITH tbl1 AS
        (
        SELECT ...
        ),
    tbl2 AS
        (
        SELECT ...
        ),
    tbl15 AS
        (
        SELECT [tbl1].[DT],
        [tbl1].[Kr_IL.BTS],
        [tbl2].[Kr_IL.CS],
        FROM [tbl1], [tbl2]
        WHERE
        [tbl1].[DT] = [tbl2].[DT]
        AND [tbl1].[DT] = [tbl3].[DT]
        )
    SELECT [DT],
           CASE
             WHEN @tag1 = 1 THEN [Kr_IL.BTS]
             ELSE NULL
           END AS BTS,
           CASE
             WHEN @tag2 = 1 THEN [Kr_IL.CS]
             ELSE NULL
           END AS CS
    FROM   tbl15
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i am using the following dynamic sql: declare @cmd nvarchar(4000) set @cmd= 'select
I've got this dynamic t-sql: declare @sql nvarchar(max) set @sql = N' insert into
I am using this SQL query DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) select
declare @TableName nvarchar(max) set @TableName='addresses' DECLARE @sql NVARCHAR(MAX) set @sql= 'create table #tempadd (
I'm declaring a varchar variable in T-SQL declare @var varchar(max); I have a select
I have some legacy SQL (SP) declare @FactorCollectionId int; select @FactorCollectionId = collectionID from
I got this Sql Query declare @ProductNo int set @ProductNo = 123 select o.OrderNo,
I have a number of stored procedures structured similarly to this: DECLARE @sql NVARCHAR(MAX)
All, I have the following dynamic SQL Query DECLARE @TableName NVARCHAR(255); SET @TableName =
I have dynamic table so I need to execute this query DECLARE @SQL nvarchar(max)

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.