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

  • Home
  • SEARCH
  • 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 5996093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:03:13+00:00 2026-05-23T00:03:13+00:00

So I got a problem looks like with string conversion. I have tried everything

  • 0

So I got a problem looks like with string conversion. I have tried everything under the sun. My goal is to inert the values from the loop into the table. However I can get the field value but I can not get the table name value due to sql thinks I am calling a table rather than a string. I have tried casting converting everything. Here is the code sample:

DECLARE @C varchar(65)
DECLARE @FieldName varchar(50)
SET @FieldName = 'ProjectID'
SELECT @C = MIN(TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'Base Table'

CREATE TABLE #temp_t (
    [field] varchar(100),
    [tblname] varchar(100)
    )

WHILE @C is not null
    BEGIN
        DECLARE @qbExists int
        DECLARE @tblname varchar(100)
        SET @tblname = CONVERT(varchar(100), @C)
        SELECT @qbExists = COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @C AND COLUMN_NAME = @FieldName
        IF @qbExists = 1 AND 
           @C <> 'TimeExpenseUnionQuery' AND 
           @C <> 'TimeExpense' AND
           @C <> 'BC_BA' AND
           @C <> 'BC_BE' AND
           @C <> 'BC_EL' AND
           @C <> 'BC_TE'
          BEGIN
            EXEC('
                  SELECT ' + @FieldName + ', ' + @C + ' FROM ' + @tblName +'
                 ')
          END

        SELECT @C = MIN( TABLE_NAME ) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME > @C
END

SELECT * FROM #temp_t 

DROP TABLE #temp_t

The problem child lies within the SELECT ‘ + @FieldName + ‘, ‘ + @C + ‘ FROM ‘ + @tblName.
Now if I do not use the @C it works fine. If I try to CONVERT or CAST the @C I get an invalid column name? What gives?

  • 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-23T00:03:14+00:00Added an answer on May 23, 2026 at 12:03 am

    You need to quote it to make it a constant, which requires doubling up because of dynamic SQL

    EXEC('
                  SELECT ' + @FieldName + ', ''' + @C + ''' FROM ' + @tblName +'
                 ')
    

    Whether the use of dynamic SQL is a good idea or not is a different matter…

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

Sidebar

Related Questions

i've got an xml-string in $response that looks like this: <?xml version=1.0?> <RESPONSE> <ERROR>
I have problem with Activities navigation, searching works good. Activities hierarchy looks like that:
I have got a window that looks like window1 and I would like it
I got a problem like this (this is html/css menu): Eshop | Another eshop
I've got a problem where I have a .co.uk domain of which I am
I've got a problem similar to,but subtly different from, that described here (Loading assemblies
I have got the following problem since the server has safe mode turned on,
I've got a problem with VB6. I have a form with several ComboBox objects
I've got a Controller ActionResult that looks like this Function Edit(ByVal user As Domain.User,
I use Boost.Serialization to serialize a std::map. The code looks like this void Dictionary::serialize(std::string

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.