0

This code shown here doesn't throw any errors, but also doesn't return any result. It should create 2 temp tables in tempDB but doesn't because of passing the values to parameters with AddWithValue.

Here is my code:

private void createTempTables(int sipID, int frmID)
{
    const string str =
            @"

         BEGIN
            IF OBJECT_ID('tempdb..#temp_sip1') IS NOT NULL
                 DRIO TABLE #temp_sip1;
            
            WITH temp_sip AS
            ( 
                SELECT DISTINCT 
                    ehID, ehSira, ehStkID, ehAdet, ehAdetN, ehTutar, 
                    ehIndirim, ehKDV, ehi1, ehi2, ehi3, ehi4, ehi5, ehiT6, ehDurum,
                    ehTutarKDV, ehBirim, ehSevkAdet, ehTutarDvz, 
                    ehSevkKontrol, ehNot, ehSipBag, ehAltStkID, 
                    ROW_NUMBER() OVER (PARTITION BY ehStkID ORDER BY ehAdet DESC) AS rank
                FROM 
                    v_emp(@sipID, @frmID)    //THIS PART.
            )
            SELECT *
            INTO #temp_sip1
            FROM temp_sip
            WHERE rank = 1

            IF OBJECT_ID('tempdb..#temp_sip2') IS NOT NULL
                DROP TABLE #temp_sip2;                  
            
            WITH temp_sip AS
            ( 
                SELECT DISTINCT
                    ehID, 
                    ROW_NUMBER() OVER (PARTITION BY ehID ORDER BY ehStkID DESC) ehSira, 
                    ehStkID, ehAdet, ehAdetN, ehTutar, ehIndirim, 
                    ehKDV, ehi1, ehi2, ehi3, ehi4, ehi5, ehiT6, 
                    ehDurum, ehTutarKDV, ehBirim, ehSevkAdet, 
                    ehTutarDvz, ehSevkKontrol, ehNot, ehSipBag, ehAltStkID
                FROM
                    #temp_sip1
            )
            SELECT *
            INTO #temp_sip2
            FROM temp_sip
        END";

    using (SqlConnection connection = new SqlConnection(constring))
    {
        using (SqlCommand cmd = new SqlCommand(str, connection))
        {                
            cmd.Parameters.Add("@sipID", SqlDbType.Int).Value = sipID;
            cmd.Parameters.Add("@frmID", SqlDbType.Int).Value = frmID;
            //cmd.Parameters.AddWithValue("@frmID", frmID);
            //cmd.Parameters.AddWithValue("@sipID", sipID);

            connection.Open();
            cmd.CommandType = CommandType.Text;

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                MessageBox.Show($"Failed to create. Error message: {e.Message}");
            }
        }
    }
}

I tried not passing any values and gave the values manually like;

v_emp(18846,201113))

And it created 2 temp tables successfully.

Did I miss something? What should I change? Thanks in advance.

UPDATE

This is the SQL Server Profiler output:

exec sp_executesql N'
         BEGIN
            if OBJECT_ID(''tempdb..#temp_sip1'') is not null
                 drop table #temp_sip1;
            
            WITH temp_sip as( 
            select distinct ehID, ehSira, ehStkID, ehAdet, ehAdetN, ehTutar, ehIndirim, ehKDV, ehi1, ehi2, ehi3, ehi4, ehi5, ehiT6, ehDurum,
             ehTutarKDV, ehBirim, ehSevkAdet, ehTutarDvz, ehSevkKontrol, ehNot, ehSipBag, ehAltStkID, ROW_NUMBER() OVER(PARTITION BY ehStkID ORDER BY ehAdet DESC) AS rank
            from v_emp(@sipID, @frmID))
            SELECT *
                into #temp_sip1
               FROM temp_sip
             WHERE rank = 1

            if OBJECT_ID(''tempdb..#temp_sip2'') is not null
                drop table #temp_sip2;                 
            
            WITH temp_sip as( 
            select distinct ehID, ROW_NUMBER() OVER(PARTITION BY ehID ORDER BY ehStkID DESC) ehSira, ehStkID, ehAdet, ehAdetN, ehTutar, ehIndirim, ehKDV, ehi1, ehi2, ehi3, ehi4, ehi5, ehiT6, ehDurum,
             ehTutarKDV, ehBirim, ehSevkAdet, ehTutarDvz, ehSevkKontrol, ehNot, ehSipBag, ehAltStkID
            from #temp_sip1)
            SELECT *
                into #temp_sip2
               FROM temp_sip
        END',N'@sipID int,@frmID int',@sipID=18846,@frmID=201113
  • If you [profile](https://learn.microsoft.com/en-us/sql/relational-databases/extended-events/use-the-ssms-xe-profiler?view=sql-server-ver15) while executing that code does it give you any clues? – Crowcoder May 01 '22 at 11:20
  • I updated my qustion with sql profiler output. And I couldn't find anything amiss. – Yunus Derici May 01 '22 at 11:27
  • Note your code doesn't execute `WITH .. SELECT..` if tempdb..#temp_sip1 exists. It only drops the tmp table. – Serg May 01 '22 at 11:32
  • 1
    I'm not sure where you are checking to see if the temp tables are populated, but once you are outside the using (sql_connection) clause, the temp tables may be dropped. I say "may" because connection pooling makes this scenario non-deterministic. – Aheho May 01 '22 at 11:37
  • I did remove else before. But it wants seperator like GO. Else it gives the CTE, xmlnamespaces error. @Serg – Yunus Derici May 01 '22 at 11:40
  • Does it work if you make the temp tables global? – Crowcoder May 01 '22 at 11:41
  • @Crowcoder No, I did try now and it doesn't. – Yunus Derici May 01 '22 at 11:45
  • I think I sould remove else statments first. But what can i use instead? WITH needs a seperator like GO. – Yunus Derici May 01 '22 at 11:46
  • What are you trying to accomplish? Temp tables are only available to the session that created them so what is the point of creating them and then disposing the connection anyway? Do you have more work to do that you just haven't built yet? – Crowcoder May 01 '22 at 11:46
  • 3
    `WITH` doesn't need GO, it just needs `;` – Crowcoder May 01 '22 at 11:47
  • Yes, I did desing in TSQL. First creating fuction that returns table (v_emp). Then creating two temporary tables. And then inserting to another table from #temp_sip2. #temp_sip2 uses #temp_sip1. – Yunus Derici May 01 '22 at 11:50
  • @Crowcoder Yesi it did work. After I closed IFs with ; it stopped giving errors. But we are back to beginnig. It doesn't create the tables. I still think the problem is addwithvalue. When I don't pass them with AddWithValue and just writing the values manually in string, it works. – Yunus Derici May 01 '22 at 11:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244372/discussion-between-yunus-derici-and-crowcoder). – Yunus Derici May 01 '22 at 11:58
  • 2
    Because you are using parameters, the SQL gets passed to `sp_executesql` which runs in its own scope. Therefore the temp tables are dropped at the end. You can solve this by either creating the temp tables first in a separate command with *no* parameters, or better don't use temp tables at all (unclear why you need them) – Charlieface May 01 '22 at 13:50
  • Yeah, why was I so stubborn about temp tables. Selecting into normal tables do the job. Thanks @Charlieface. – Yunus Derici May 01 '22 at 14:27
  • Why use any tables? Why don't you just use derived tables/subqueries and select the data straight back to the client – Charlieface May 01 '22 at 14:33
  • I didn't know the derived tables. I made a simple google search and understood what you are trying to say. Are derived tables disposed after sp_executesql like temp tables? @Charlieface – Yunus Derici May 01 '22 at 14:43
  • 2
    @YunusDerici Derived tables are just any table expressions like subqueries and CTEs. They don't ever actually exist as tables, but they can be treated as a table/datasource by a SQL query. So your `WITH...` clauses are an example of a derived table. What Charlieface is suggesting is that rather than temp tables, you should just make one big query with your current temp tables as subqueries or CTEs. – RBarryYoung May 01 '22 at 15:57
  • 1
    @RBarryYoung I see, thank you guys. With your help, I took a big step in my project. – Yunus Derici May 02 '22 at 06:22

0 Answers0