Advertisement
ayurchyk1998

xml20316

Jan 9th, 2021
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.68 KB | None | 0 0
  1. use TSQL2012;
  2. GO
  3. /*1. Utworzyć XML (wyeksportować dane) na podstawie danych o produktach z tabeli Production.Products zawierające
  4. productname, z category name, z supplier companyname i dodatkowo dane o liczbie zamówień dla tego produktu oraz wartości tych zamówień.*/
  5. SELECT pp.productname AS [ProductName],
  6.        pc.categoryname AS [Category],
  7.        ps.companyname AS [Supplier],
  8.        sum(sod.qty) AS [ProductInfo/CountProduct],
  9.        sum(sod.qty*sod.unitprice) AS [ProductInfo/FullPrice]
  10. FROM Production.Products pp, Production.Categories pc, Production.Suppliers ps, Sales.OrderDetails sod
  11. WHERE pp.categoryid = pc.categoryid AND pp.supplierid = ps.supplierid AND sod.productid = pp.productid
  12. GROUP BY pp.productname, pc.categoryname, ps.companyname
  13. ORDER BY pp.productname
  14. FOR XML PATH ('Product'), ROOT ('Products')
  15. /*2. Zaimportować dane produktów na podstawie XML (z informacjami jak w zad. 1 oczywiście zignorować liczbę i wartość zamówień).
  16. Gdy productname jest w bazie zrobić update rekordu używając nowych danych z XML, w przeciwnym wypadku dodać nowy produkt (można zastosować MERGE).
  17. Gdy nie ma companyname w Production.Suppliers zignorować dodanie (aktualizację) produktu.
  18. Gdy nie ma categoryname w Production.Categories dodać nową kategorię z dodaniem (aktualizacją) produktu.*/
  19. DECLARE @products xml
  20. SELECT @products = P
  21. FROM OPENROWSET (BULK 'C:\Users\alexa\OneDrive\Документы\SQL Server Management Studio\ProductsZ2.xml', SINGLE_BLOB) AS Products(P)
  22. SELECT @products
  23.  
  24. CREATE TABLE MyProducts(
  25.     ProductName VARCHAR(30),
  26.     CategoryName VARCHAR(30),
  27.     SupplierName VARCHAR(30)
  28. );
  29.  
  30. DECLARE @hdoc INT
  31. EXEC sp_xml_preparedocument @hdoc OUTPUT, @products
  32. INSERT INTO MyProducts (ProductName, CategoryName, SupplierName)
  33. SELECT ProductName, Category, Supplier  FROM OPENXML (@hdoc, '/Products/Product', 2)
  34. WITH(
  35.     ProductName NVARCHAR(30),
  36.     Category NVARCHAR(30),
  37.     Supplier NVARCHAR(30)
  38.     )
  39. EXEC sp_xml_removedocument @hdoc
  40. SELECT*FROM MyProducts
  41. --------------------------------------------------------------------------------------------------
  42. --category table merge
  43. DECLARE @idcat INT = 0
  44. MERGE Production.Categories AS target
  45. USING (SELECT CategoryName FROM MyProducts) AS source
  46. ON (target.categoryname = source.CategoryName)
  47. WHEN NOT MATCHED
  48.     THEN INSERT VALUES(source.CategoryName, 'New description num.'+cast((@idcat+1) AS NVARCHAR));
  49.  
  50. select*from Production.Categories
  51. -----------------------------------------------------------------------------------------------------------
  52. --products table merge
  53. MERGE Production.Products AS target
  54. USING (SELECT * FROM MyProducts) AS source
  55. ON (TARGET.productname = source.ProductName)
  56. WHEN MATCHED AND EXISTS (SELECT supplierid from Production.Suppliers where source.SupplierName = companyname)
  57.     THEN
  58.         UPDATE SET  target.supplierid = (SELECT supplierid FROM Production.Suppliers s WHERE source.SupplierName = s.companyname),
  59.                     target.categoryid = (SELECT categoryid FROM Production.Categories c WHERE source.CategoryName = c.categoryname)
  60. WHEN NOT MATCHED AND EXISTS (SELECT supplierid from Production.Suppliers where source.SupplierName = companyname)-- AND supplierid IS NOT NULL) /*(SELECT FROM WHERE p.supplierid = ) IS NOT NULL*/
  61.     THEN
  62.         INSERT VALUES (source.ProductName,
  63.                        (SELECT supplierid FROM Production.Suppliers s WHERE source.SupplierName = s.companyname),
  64.                        (SELECT categoryid FROM Production.Categories c WHERE source.CategoryName = c.categoryname),100,1);
  65.  
  66. SELECT*FROM Production.Products
  67. SELECT*FROM MyProducts
  68. -----------------------------------------------------------------------------------------------------------------------------------
  69. DROP TABLE MyProducts;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement