Solved

Creating a Temp Table Programmatically

Posted on 2009-03-29
1
2,123 Views
Last Modified: 2012-05-06
I can create a temp table in Management Studio like this:

CREATE TABLE #ORDER_TEMP ([Order_ID] [int],[Customer_Number_FK] [nchar](10),[Product_ID_FK] [int])

However, using the same code from my C# app does not produce the same result.  Here's how I define the table and send the sql:

            sql = "CREATE TABLE #ORDER_TEMP (";
              sql = sql + " [Order_ID] [int] ";
            sql = sql + " ,[Customer_Number_FK] [nchar](10) ";
            sql = sql + " ,[Product_ID_FK] [int]) ";
            Program.ExecuteSQL(Program.wholesaleOrderingDatabase,sql);

            Program.InsertRecord(Program.wholesaleOrderingDatabase, "#ORDER_TEMP", "Order_ID,Customer_ID_FK,Product_ID_FK", "1,1," + iOrderNumber);


Here's the method that should build it:


    public static void ExecuteSQL(string connectionString, string sql)
    {
        //Executes whatever SQL string is passed in
        int recordsReturned;

        SqlConnection thisConnection = new SqlConnection(connectionString);
        thisConnection.Open();
        SqlCommand thisCommand = new SqlCommand(sql, thisConnection);
        recordsReturned = thisCommand.ExecuteNonQuery();
        thisConnection.Close();

    }


Why does this not work?  (I don't get an error message).
0
Comment
Question by:NigelRocks
1 Comment
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 24013746
Temp tables have a scope of a session. Each time you open a new connection (like you do), a new session is started with its own temp tables.

So you have some options:
-re-use the same connection instead of creating a new one each time.
-use global temp tables (##ORDER_TEMP)
-Don't use temp tables
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question