Why the # (hashes) in Dapper sample

c# dapper

Question

I'm just reading this sample from the Dapper "manual":

connection.Execute(@"
  set nocount on 
  create table #t(i int) 
  set nocount off 
  insert #t 
  select @a a union all select @b 
  set nocount on 
  drop table #t", new {a=1, b=2 })
   .IsEqualTo(2);

Are the #t's a special syntax for something? Or are they just there to confuse me? :)

Accepted Answer

Yes, # means something important in TSQL - a table named foo is permenant, for that db/schema. A table named #foo is a temporary table - it only exists for that connection, and is removed when the connection is closed or reset. A table named ##foo is a global temporary table, and exists everywhere, but is intended to be temporary. This is mainly used when bulk-shifting data.

The use of #t here is so that the table only exists on that connection, so we can re-run the test trivially.

Also, a table named @foo is either a table-variable, or a table-valued-parameter, and only exists for that command / sproc.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why