I am creating a temporary table, #ua_temp, which is a subset of a regular table. I do not receive an error message, but when I try to SELECT from #ua_temp in the second step, it is not found. If I delete #, a table called ua_temp will be created.
I used the same technique from the created table with SELECT INTO elsewhere. It works 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 obvious questions: this program worked fine using the original elig_ua_response table. The reason for introducing the temporary table was that I want to delete some rows for this particular report. I put brackets around the [file_name] column during testing to make sure this is not a keyword. The second SELECT works fine if you replace #ua_temp with elig_ua_response. I tried different names for temp table. A MessageBox showing the number of lines is for debugging purposes only; this does not affect the problem.