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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:33:35+00:00 2026-06-17T22:33:35+00:00

Task: Replace certain variables within an sql statement with the requisite data. Issue: The

  • 0

Task:

Replace certain variables within an sql statement with the requisite data.

Issue:

The variables are not being replaced

C# code:

SqlCommand cmd = new SqlCommand(@"
     select 
                 1 [GL], 
                 (
                     select 
                             isnull(COUNT(*), 0)
                         from 
                             @DataDB..APSetup
                 ) [AP],
                 (
                     select 
                             isnull(COUNT(*), 0)
                         from
                             @DataDB..ARSetup
                 ) [AR],
                 (
                     select 
                             isnull(COUNT(*), 0)
                         from 
                             @DataDB..CASetup
                 ) [CA],
                 case 
                     when (
                          select 
                                  isnull(COUNT(*), 0) 
                              from
                                  @DataDB..SalesTax
                          ) > 0 
                         then 1 else 0
                 end [TX], 
                 (
                     select 
                             isnull(COUNT(*), 0)
                         from
                             @DataDB..INSetup
                 ) [IN], 
                 (
                     select
                             isnull(COUNT(*), 0)
                         from 
                             @DataDB..POSetup
                 ) [PO],
                 (
                     select
                             isnull(COUNT(*), 0)
                         from 
                             @DataDB..SOSetup
                 ) [SO], 
                 (
                     select 
                             isnull(COUNT(*), 0)
                         from
                             @DataDB..RQSetup
                 ) [RQ],
                 (
                    select 
                             COUNT(*)
                        from 
                             @DataDB..pcsetup 
                        Where 
                             Setupid = 'PC' AND PERNBR <> ''
                 ) [PJ] 
    from 
          @DataDB..glsetup");

    cmd.Parameters.AddWithValue("@DataDB", CurrentBranch.AppDB);
    AppConnection.InitialCatalog = CurrentBranch.AppDB;
    Modules.AddRange(SqlQueries.DataQuery2(cmd, AppConnection.ConnectionString)); 

Sql Query:

select 1 [GL], (select isnull(COUNT(*),0) from @DataDB..APSetup) [AP], (select isnull(COUNT(*),0) from @DataDB..ARSetup) [AR], (select isnull(COUNT(*),0) from @DataDB..CASetup) [CA], case when (select isnull(COUNT(*),0) from @DataDB..SalesTax) > 0 then 1 else 0 end [TX], (select isnull(COUNT(*),0) from @DataDB..INSetup) [IN], (select isnull(COUNT(*),0) from @DataDB..POSetup) [PO], (select isnull(COUNT(*),0) from @DataDB..SOSetup) [SO], (select isnull(COUNT(*),0) from @DataDB..RQSetup) [RQ],  (select COUNT(*) from @DataDB..pcsetup Where Setupid = 'PC' AND PERNBR <> '') [PJ] from @DataDB..glsetup

Question:

Why does the variable replacement not work?

Additional Information:

The issue is not the query, when I manually replace the @DataDB with its intended value it works fine.

The issue is the C# SqlCommand.Parameters.AddWithValue();

  • 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-17T22:33:36+00:00Added an answer on June 17, 2026 at 10:33 pm

    The reason is because you can’t parameterize what database/table you are trying to select from. You can’t do: select * from @SomeTable You already are doing this in C#, just do this:

    (snipped for clarity sake):

    string sql = string.Format("select isnull(COUNT(*),0) from {0}..APSetup)...", CurrentBranch.AppDB);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to the SQL and need some help with a certain task.
The ant replace task is not like copy task which have a overwrite property,
Is it possible to create/replace PL/SQL via the Oracle JDBC driver, i.e not via
Task at hand — I have three versions of some code, developed by different
the task I want to achieve is import XML file into SQL Server. Once
The task is pretty simple, but I've not been able to come up with
Looking for the equivalent of Ant Replace task in Nant
I have the following ant task: <target name=test> <replace file=test.txt token=smth value=anything/> </target> test.txt
I've got a rake file that performs a find and replace operation on certain
I would like to cancel a running task and replace it with a new

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.