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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:20:08+00:00 2026-05-27T12:20:08+00:00

In my sqluery, i am trying to add columns dynamically, but it is showing

  • 0

In my sqluery, i am trying to add columns dynamically, but it is showing error Like:
Msg 214, Level 16, State 2, Procedure sp_executesql, Line 1
Procedure expects parameter ‘@statement’ of type ‘ntext/nchar/nvarchar’.
this is my proc

        declare @curntyear int
           declare @pasyear int
           declare @sql nvarchar(max)
           declare @temp varchar(50)                
            select  @pasyear=YEAR( DATEADD (YEAR,-3,GETDATE ()))
            select @curntyear =YEAR (getdate())
            print @pasyear
            print @curntyear               
             create table #TempTable (years varchar(30))
             insert into #TempTable  select (cargo+port+railway+estate)as 'sum' from operatingincome where YEAR(createddate)=@pasyear                   
           while (@curntyear >=@pasyear )
            begin
               set @pasyear =@pasyear +1
               --select @temp=(cargo+port+railway+estate)as 'sum' from operatingincome where YEAR(createddate)=@pasyear 
               select  @temp= convert(varchar,(cargo+port+railway+estate),106)   from operatingincome where YEAR(createddate)= @pasyear
               set @sql ='alter table #TempTable add '+ CONVERT(varchar,@pasyear,106)+' varchar(50)'        

                  exec sp_executesql @sql 
                   print @sql
                                           set @sql = 'update #TempTable set '+CONVERT(varchar,@pasyear,106) +'='+@temp +' where years='''+CONVERT(varchar,@pasyear,106)+''
                      exec sp_executesql @sql 
               set @temp =''
              end
              select * from #TempTable     

i am getting error like this :

Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ‘2009’.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ‘2010’.
alter table #TempTable add 2010 varchar(50)
update #TempTable set 2010=9300 where years=2010

  • 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-05-27T12:20:09+00:00Added an answer on May 27, 2026 at 12:20 pm

    Sounds like you’re calling sp_executesql with a VARCHAR statement, when it needs to be NVARCHAR.

    e.g. This will give the error because @SQL needs to be NVARCHAR

    Declare your variable @sql varchar to nvarchar, that will work fine:

    declare @sql nvarchar(max)
    

    UPdate:

    Change your sql setting as below: you need to use [] in column name as [ColumName]

    set @sql ='alter table #TempTable add ['+ CONVERT(varchar,@pasyear,106)+'] varchar(50)'
    
    set @sql = 'update #TempTable set ['+CONVERT(varchar,@pasyear,106) +']='+@temp +' where years='''+CONVERT(varchar,@pasyear,106)+''
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a stored procedure that will allow me to write
I'm trying to add the following string from a C# application to a varchar
I'm trying to add a datetime into a database table by grabbing values from
I am trying to implement CoolStorage in my project, but am running into an
I am trying to add a parameter to a sqlDataAdapter. I have tried to
I'm getting an error when trying to upload a .csv to my sql server
I'm trying to insert data into a table in a database, but the insertion
I'm trying to load data into a data frame in R from a state
I'm trying to retrive a dataset to a Gridview, but I just don't get
I'm kind of like stuck trying to implement YUI autocomplete textbox. here's the code:

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.