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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:25:17+00:00 2026-05-23T15:25:17+00:00

I’m creating a temporary table, #ua_temp, which is a subset of regular table. I

  • 0

I’m creating a temporary table, #ua_temp, which is a subset of regular table. I don’t get an error, but when I try to SELECT from #ua_temp in the second step, it’s not found. If I remove the #, a table named ua_temp is created.

I’ve used the exact same technique from created the table with SELECT INTO elsewhere. It runs fine, so I don’t think it has anything to do with database settings. Can anyone see the problem?

        // Create temporary table 
        q = new StringBuilder(200);
        q.Append("select policy_no, name, amt_due, due_date, hic, grp, eff_dt, lis_prem, lis_grp, lis_co_pay_lvl, ");
        q.Append("lep_prem, lapsed, dn_code, [filename], created_dt, created_by ");
        q.Append("into #ua_temp from elig_ua_response ");
        q.Append("where [filename] = @fn1 or [filename] = @fn2 ");
        sc = new SqlCommand(q.ToString(), db);
        sc.Parameters.Add(new SqlParameter("@fn1", sFn));
        sc.Parameters.Add(new SqlParameter("@fn2", sFn2));
        int r = sc.ExecuteNonQuery();
        MessageBox.Show(r.ToString() + " rows");

        // Rosters
        q = new StringBuilder(200);
        q.Append("select policy_no,name,amt_due,due_date,hic,grp,eff_dt,");
        q.Append("lis_prem,lis_grp,lis_co_pay_lvl,lep_prem,lapsed,dn_code,[filename] ");
        q.Append("from #ua_temp where (lis_prem > 0.00 or lep_prem > 0.00) ");
        q.Append("and [filename] = @fn order by name");
        sc.CommandText = q.ToString();
        sc.Parameters.Clear();
        sc.Parameters.Add(new SqlParameter("@fn", sFn));
        sda = new SqlDataAdapter(sc);
        sda.Fill(ds, "LIS LEP Roster");

To answer some of the obvious questions: This program was running fine using the source table, elig_ua_response. The reason for introducing the temp table was that I want to delete some of the rows for this particular report. I put brackets around the column [filename] while testing to be sure it’s not a key word issue. The second SELECT works fine if you replace #ua_temp with elig_ua_response. I’ve tried different names for the temp table. The MessageBox showing the number of rows was just for debugging purposes; it doesn’t affect the problem.

  • 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-23T15:25:18+00:00Added an answer on May 23, 2026 at 3:25 pm

    I think the solution to your problem is to combine the creation of the temp table and selecting from that temp table into one query (see code snippet #3 below). Executing the command twice (as you do in the code in your question) seems to work ok if you are not using command parameters, but fails if they are introduced. I tested a few different approaches and here’s what I found.

    1) WORKS OK: Use same command object, no command parameters, execute command twice:

    using (var conn = new SqlConnection("..."))
    {
        conn.Open();
        using (var cmd = conn.CreateCommand())
        {
            const string query = @"
                CREATE TABLE #temp 
                    ([ID] INT NOT NULL, [Name] VARCHAR(20) NOT NULL)
                INSERT INTO #temp VALUES(1, 'User 1')
                INSERT INTO #temp VALUES(2, 'User 2')";
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = query;
            cmd.ExecuteNonQuery();
    
            cmd.CommandText = "SELECT * FROM #temp";
            using (var sda = new SqlDataAdapter(cmd))
            {
                var ds = new DataSet();
                sda.Fill(ds);
                foreach (DataRow row in ds.Tables[0].Rows)
                    Console.WriteLine("{0} - {1}", row["ID"], row["Name"]);
            }
        }
    }
    

    2) FAILS: Use same command object, command parameters, execute command twice:

    using (var conn = new SqlConnection("..."))
    {
        conn.Open();
        using (var cmd = conn.CreateCommand())
        {
            const string query = @"
                CREATE TABLE #temp 
                    ([ID] INT NOT NULL, [Name] VARCHAR(20) NOT NULL)
                INSERT INTO #temp VALUES(1, @username1)
                INSERT INTO #temp VALUES(2, @username2)
            ";
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = query;
            cmd.Parameters.Add("@username1", SqlDbType.VarChar).Value ="First User";
            cmd.Parameters.Add("@username2", SqlDbType.VarChar).Value ="Second User";
            cmd.ExecuteNonQuery();
    
            cmd.Parameters.Clear();
            cmd.CommandText = "SELECT * FROM #temp";
            using(var sda = new SqlDataAdapter(cmd))
            {
                var ds = new DataSet();
                sda.Fill(ds);
                foreach(DataRow row in ds.Tables[0].Rows)
                    Console.WriteLine("{0} - {1}", row["ID"], row["Name"]);
            }
        }
    }
    

    3) WORKS OK: Use same command object, command parameters, execute command once only:

    using (var conn = new SqlConnection("..."))
    {
        conn.Open();
        using (var cmd = conn.CreateCommand())
        {
            const string query = @"
                CREATE TABLE #temp 
                    ([ID] INT NOT NULL, [Name] VARCHAR(20) NOT NULL)
                INSERT INTO #temp VALUES(1, @username1)
                INSERT INTO #temp VALUES(2, @username2)
                SELECT * FROM #temp
            ";
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = query;
            cmd.Parameters.Add("@username1", SqlDbType.VarChar).Value ="First User";
            cmd.Parameters.Add("@username2", SqlDbType.VarChar).Value ="Second User";
            using (var sda = new SqlDataAdapter(cmd))
            {
                var ds = new DataSet();
                sda.Fill(ds);
                foreach (DataRow row in ds.Tables[0].Rows)
                    Console.WriteLine("{0} - {1}", row["ID"], row["Name"]);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.