Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.86 KB | None | 0 0
  1. create table contact (
  2.     id uniqueidentifier primary key,
  3.     first_name nvarchar(50) not NULL,
  4.     last_name nvarchar(50) NOT NULL,
  5.     age int
  6. )
  7. create table deal (
  8.     id uniqueidentifier primary key,
  9.     ref_created_by uniqueidentifier not NULL,
  10.     name nvarchar(200) NOT NULL
  11. )
  12. create table activity (
  13.     id uniqueidentifier primary key,
  14.     ref_created_by uniqueidentifier not NULL,
  15.     ref_for_deal uniqueidentifier not null
  16. )
  17.  
  18. alter table activity add [subject] nvarchar(200)
  19.  
  20. insert into contact (id, first_name, last_name)
  21. values
  22. (newid(), 'john', 'smith'),
  23. (newid(), 'tamara', 'jones')
  24.  
  25. select * from contact
  26. insert into deal (id, name, ref_created_by)
  27. values
  28. (newid(), 'deal 1', '5BA73DFB-8153-4CBE-80EA-7C5CB2236EF4'),
  29. (newid(), 'deal 2', '5BA73DFB-8153-4CBE-80EA-7C5CB2236EF4')
  30.  
  31. select * from deal
  32. insert into activity (id, [subject], ref_created_by, ref_for_deal)
  33. values
  34. (newid(), 'meeting (7/18/2017)', '5BA73DFB-8153-4CBE-80EA-7C5CB2236EF4', '88DCB7F1-9A64-407D-BCC1-54004B2F595A'),
  35. (newid(), 'phone call (6/16/2016)', 'B75F0C1E-541E-48CC-9026-9861F28EA077','88DCB7F1-9A64-407D-BCC1-54004B2F595A')
  36.  
  37. select
  38.     a.subject
  39.     , created_by.first_name + ' ' + created_by.last_name  as [created_by]
  40.     , d.name
  41.     , deal_created_by.first_name + ' ' + deal_created_by.last_name  as [deal_created_by]
  42. from activity as a
  43. left join contact as created_by on a.ref_created_by = created_by.id
  44. left join deal as d on d.id = a.ref_for_deal
  45. left join contact as deal_created_by on deal_created_by.id = d.ref_created_by
  46.  
  47. ------ other
  48.  
  49.  
  50. --newid()
  51. --getdate() '2010-5-5'
  52.  
  53. drop table contact
  54. create table company (
  55.     id uniqueidentifier primary key,
  56.     [name] nvarchar(200) NOT NULL,
  57.     [date_founded] datetime
  58. )
  59. drop table company
  60. select * from contact
  61. select contact.id, first_name, last_Name, age, name AS company_name
  62. from contact
  63. inner join company on contact.ref_company = company.id
  64.  
  65.  
  66. select * from company
  67.  
  68. insert into company (id, name, date_founded)
  69. values (newid(), 'Dynamo Software', '1998-10-10')
  70. insert into company (id, name, date_founded)
  71. values (newid(), 'Microsoft', '1985-5-5')
  72.  
  73.  
  74. insert into contact (id, first_name, last_name, age, ref_company)
  75. values (newid(), 'John', 'Smith', 20, '5258FD16-1A45-4F0B-8C18-05D21E4D6A3D')
  76.  
  77. insert into contact (id, first_name, last_name, age, ref_company)
  78. values (newid(), 'Tamara', 'Jones', 22, 'C5B8086E-81D1-4016-9C76-BDE4EF9FD3FC')
  79.  
  80. insert into contact (id, first_name, last_name, age, ref_company)
  81. values (newid(), 'Scott', 'Gu', 30, NULL)
  82.  
  83. insert into contact (id, first_name, last_name, age, ref_company)
  84. values (newid(), 'Scott', 'Hans', 30, 'C5B8086E-81D1-4016-9C76-BDE4EF9FD3FF')
  85.  
  86.  
  87. DELETE FROM contact WHERE ID IN ('7E391E1D-3B05-4B3C-B8A3-8821B3EB24F7')
  88. DELETE FROM contact WHERE ID = '7E391E1D-3B05-4B3C-B8A3-8821B3EB24F7'
  89. UPDATE Contact SET last_name = 'Cage' WHERE ID = '7E391E1D-3B05-4B3C-B8A3-8821B3EB24F7'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement