Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the # (hashes) in Dapper sample

Tags:

c#

dapper

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? :)

like image 950
asgerhallas Avatar asked Oct 12 '12 13:10

asgerhallas


1 Answers

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.

like image 156
Marc Gravell Avatar answered Sep 23 '22 16:09

Marc Gravell