Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.26 KB | None | 0 0
  1. Create      Database    SamService
  2. GO
  3.  
  4. Use     SamService
  5. GO
  6.  
  7.  
  8. Create      Table       [Stuff](
  9. ID      BigInt      Not Null    Identity    Primary Key,
  10. Title       nVarChar(100)   Not Null,
  11. Model       nVarChar(6) Not Null,
  12. Serial      nVarChar(11)    Not Null,
  13. Constraint  IX_Stuff    Unique      (Serial))
  14.  
  15.  
  16. Create      Table       Customer(
  17. ID      BigInt      Not Null    Identity    Primary Key,
  18. Firstname   nVarChar(50)    Not Null,
  19. Lastname    nVarChar(70)    Not Null,
  20. Code        nVarChar(10)    Not Null,
  21. Tel     nVarChar(10),
  22. [Address]   nVarChar(500),
  23. Constraint  IX_Customer Unique      (Code))
  24.  
  25.  
  26. Create      Table       Purchase(
  27. ID      BigInt      Not Null    Identity    Primary Key,
  28. Stuff_ID    BigInt      Not Null    References  [Stuff](ID),
  29. Customer_ID BigInt      Not Null    References  Customer(ID),
  30. Code        nVarChar(10)    Not Null,
  31. [Date]      DateTime    Not Null    Default(GetDate())
  32. Constraint  IX_Purchase Unique      (Code))
  33.  
  34.  
  35. Create      Table       Delivery(
  36. ID      BigInt      Not Null    Identity    Primary Key,
  37. Customer_ID BigInt      Not Null    References  Customer(ID),
  38. Purchase_ID BigInt      Not Null    References  Purchase(ID),
  39. [Date]      DateTime    Not Null    Default(GetDate()))
  40.  
  41.  
  42. Create      Table       Request(
  43. ID      BigInt      Not Null    Identity    Primary Key,
  44. Customer_ID BigInt      Not Null    References  Customer(ID),
  45. Purchase_ID BigInt      Not Null    References  Purchase(ID),
  46. [Status]    Int     Not Null    Default(1),
  47. ConfrimStatus   Bit     Not Null    Default(0),
  48. [Date]      DateTime    Not Null    Default(GetDate()),
  49. Notes       nVarChar(500),
  50. Constraint  CK_ReplacementRequest       Check       ([Status] > 0 And [Status] < 4))
  51.  
  52.  
  53. -- Function
  54.  
  55. Create      Function    MainQuery()
  56. Returns     Table
  57.         Return
  58.         Select  Customer.Firstname          As  Firstname,
  59.             Customer.Lastname           As  Lastname,
  60.             Customer.[Address]          As  [Address],
  61.             Customer.Tel                As  Tel,
  62.             Customer.Code               As  Code,
  63.             [Stuff].Title               As  Name,
  64.             [Stuff].Model               As  Model,
  65.             [Stuff].Serial              As  Serial,
  66.             Request.[Status]            As  [Status],
  67.             Request.ConfrimStatus           As  ConfrimStatus,
  68.             Request.Notes               As  Notes,
  69.             Request.[Date]              As  RequestDate,
  70.             Request.ID              As  RequestID,
  71.             Purchase.[Date]             As  PurchaseDate,
  72.             Purchase.Code               As  PurchaseCode
  73.             From    Request
  74.             Inner   Join    Customer
  75.                 On  Customer.ID     =   Request.Customer_ID
  76.             Inner Join  Purchase
  77.                 On  Purchase.Customer_ID    =   Request.Customer_ID
  78.             Inner Join  [Stuff]
  79.                 On  [Stuff].ID      =   Purchase.Stuff_ID
  80.            
  81.             Where   Request.[Status]            <>  1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement