Untitled
Guest 962 25th Jun, 2023
1.
CREATE TABLE Ksiazki (
KsiazkaID INT PRIMARY KEY,
Tytul VARCHAR(100) NOT NULL,
Autor VARCHAR(100) NOT NULL,
RokWydania INT,
ISBN VARCHAR(20) UNIQUE,
Cena DECIMAL(10, 2),
CONSTRAINT CK_RokWydania CHECK (RokWydania >= 0),
CONSTRAINT CK_Cena CHECK (Cena >= 0)
);
INSERT INTO Ksiazki (KsiazkaID, Tytul, Autor, RokWydania, ISBN, Cena)
VALUES
(1, 'Harry Potter i Kamień Filozoficzny', 'J.K. Rowling', 1997, '9788372780529', 49.99),
(2, 'Zabić drozda', 'Harper Lee', 1960, '9788324792491', 29.99),
(3, 'Wielki Gatsby', 'F. Scott Fitzgerald', 1925, '9788376902858', 39.99);
select * from Ksiazki
2.
SELECT *
INTO Customers_new
FROM SalesLT.Customer
WHERE CustomerID IN (
SELECT DISTINCT CustomerID
FROM SalesLT.SalesOrderHeader
);
UPDATE Customers_new
SET EmailAddress = CONCAT(EmailAddress, '@example.com')
WHERE SUBSTRING(FirstName, LEN(FirstName), 1) IN ('a', 'b', 'c', 'd', 'e');
DELETE FROM Customers_new
WHERE CustomerID IN (
SELECT CustomerID
FROM SalesLT.Customer
WHERE Title = 'Mr.'
);
select * from Customers_new
3.
CREATE VIEW SalesSummary AS
SELECT P.Name AS ProductName, ISNULL(SUM(SOD.OrderQty), 0) AS QuantitySold, ISNULL(SUM(SOD.LineTotal), 0) AS TotalValue
FROM SalesLT.Product AS P
LEFT JOIN SalesLT.SalesOrderDetail AS SOD ON P.ProductID = SOD.ProductID
WHERE P.ProductID NOT IN (708, 710)
GROUP BY P.Name;
select * from SalesSummary
4.
CREATE PROCEDURE DeleteCustomerAndOperations
@CustomerID INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRANSACTION;
-- Usuwanie operacji klienta
DELETE FROM SalesLT.SalesOrderDetail
WHERE SalesOrderID IN (
SELECT SalesOrderID
FROM SalesLT.SalesOrderHeader
WHERE CustomerID = @CustomerID
);
-- Usuwanie klienta
DELETE FROM SalesLT.Customer
WHERE CustomerID = @CustomerID;
COMMIT;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK;
SET XACT_ABORT OFF;
END
DECLARE @ErrorMessage NVARCHAR(4000);
SELECT @ErrorMessage = ERROR_MESSAGE();
RAISERROR(@ErrorMessage, 16, 1);
END CATCH;
END;
EXEC DeleteCustomerAndOperations @CustomerID = 1;
5.
-- Tworzenie tabeli tymczasowej
SELECT
Name,
ProductNumber,
ListPrice * 2 AS Price,
Weight
INTO Temp
FROM [SalesLT].[Product]
WHERE Weight IS NOT NULL;
-- Aktualizacja danych w tabeli [SalesLT].[Product] na podstawie tabeli tymczasowej [Temp]
UPDATE P
SET
P.ListPrice = T.Price,
P.Weight = T.Weight
FROM [SalesLT].[Product] P
JOIN Temp T ON P.ProductNumber = T.ProductNumber;
-- Usuwanie tabeli tymczasowej
DROP TABLE Temp;
To share this paste please copy this url and send to your friends
RAW Paste Data