Solved

Creating a Temp Table Programmatically

Posted on 2009-03-29
1
2,130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

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…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

738 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