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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:12:37+00:00 2026-06-08T12:12:37+00:00

I am trying to use some dynamic sql. I have generated over 100 parameters,

  • 0

I am trying to use some dynamic sql. I have generated over 100 parameters, and to speed things up I am trying to use dynamic sql to only insert values into a table that I have to based off of information retrieved from other tables. I have tried many things like adding a cast etc.

Example of what I mean:

DECLARE @var1 NVARCHAR(MAX)

-- Loop through and add various values
SET @var1 = @var1 + @parameterName
-- The parameter name is retrieved from a table that holds this information

the problem is that when I add the parameter name which would be like “@myFirstParameter” into my final expression so something like this:

DECLARE @finalString NVARCHAR(MAX)
SET @finalString = 'INSERT INTO myTableName ([myFirstParameter]) VALUES (@myFirstParameter)'
EXEC(@finalString)

The “@myFirstParameter” does not get replaced by it’s value and I get the following error:
Must declare the scalar variable “@myFirstParameter”.

Does anyone know of a way to go from the string name of a parameter to the actual value? I am trying to avoid hardcoding all the values and any work around I have attempted has failed and given me errors which appear to be much worse than what I have stated above.

  • 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-08T12:12:39+00:00Added an answer on June 8, 2026 at 12:12 pm

    The first way is to add the parameter’s value, instead of its name, to the SQL string:

    SET @finalString = 'INSERT INTO myTableName ([myFirstParameter]) VALUES (' + 
        @myFirstParameter + ')'
    

    This assumes the parameter has a string value. If not, you have to cast it, like cast(@myFirstParameter as varchar(128)).

    The second way is to use sp_executesql instead of exec:

    exec sp_executesql 
        N'INSERT INTO myTableName ([myFirstParameter]) VALUES (@myFirstParameter)',
        N'@myFirstParameter varchar(128)',
        @myFirstParameter = @myFirstParameter;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use some parameters in my sql query like this; Answer_table
I have a problem trying to parameterize some dynamic SQL build in an existing
I'm trying to do an input that only accept digit. I can have some
I'm trying to use FindControl() to SET the values of some DropDownLists on a
I am trying to use a textarea form entry which I have some javascript
EDIT: Database names have been modified for simplicity I'm trying to get some dynamic
I am trying to use mod_rewrite to clean up some dynamic URLS that I
I am trying to use some Word 2007 automation where we give the user
I am trying to use some rails code withing a javascript and need to
I am trying to use some action when I click button in navigation controller.

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.