Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating a Temp Table Programmatically

Posted on 2009-03-29
1
Medium Priority
?
2,139 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 1500 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

705 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