0% found this document useful (0 votes)
27 views

Week13 DB Lab - SQL

This document contains code to: 1. Create a view that selects and categorizes product prices as "cheap", "normal", or "pahalé" based on price ranges. 2. Create a table valued function that returns product name, color, and list price for a given color parameter. 3. Create a stored procedure that updates the list price of a product based on product ID and price increase parameters passed to the procedure.

Uploaded by

Azizcan Yalçın
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Week13 DB Lab - SQL

This document contains code to: 1. Create a view that selects and categorizes product prices as "cheap", "normal", or "pahalé" based on price ranges. 2. Create a table valued function that returns product name, color, and list price for a given color parameter. 3. Create a stored procedure that updates the list price of a product based on product ID and price increase parameters passed to the procedure.

Uploaded by

Azizcan Yalçın
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Create VIEW vwprice

as

select Name,Color,ListPrice, case when ListPrice between 0 and 1000 then 'cheap'
when ListPrice between 1001 and 2000 then N'normal'
when Listprice>2000 then N'pahal�'
else 'other'
end as Price
from Production.Product

select*
from vwprice

select *
from Production.Product

-- Table valued functions

--return a table ---you can send a parameter

Create function fnsendcolor


(
@colorr nvarchar(50)
)
returns table
as
return
select Name,Color,ListPrice
from Production.Product
where color=@colorr

select *
from fnsendcolor('white')

----procedures
--you can usse select,insert,update,delete

create procedure prUpdateListPrice


(
@prID int,
@price decimal(18,2)
)
as
begin
update Production.Product
set ListPrice=ListPrice+@price
where ProductID=@prID

select Name,Color,ListPrice
from Production.Product
where ProductID=@prID
end

exec prUpdateListPrice 1,300

-----------------------------------------------------------------------------------
---------
--data types

--numer�c data types


/*

tinyint---->1 byte 0-255


smallint--->2 byte -32.700 +32700
int-------->4 byte -2billion +2billion
bigint----->8 byte 2^63

decimal(5,2) 123.12
decimal(5,2) 1234.5 its not held 1234.50
decimal(5,2) 23.8596 ---> 23. 860
-----------------------------------------------------------------
bit ---> 0 and 1

*/
----------------------------------
/*

DATES AND TIMES

datetime------------>YYYY-MM-DD HH:MI:SS YEARS BETWEEN 1753 AND 9999


smalldatetime------->YYYY-MM-DD HH:MI:SS YEARS BETWEEN 1900 AND 2079
-------------------------------------------------------------------------------
date---------------->YYYY-MM-DD YEARS BETWEEN 0001-9999
time---------------->HH:MI:SS between 00:00:00: and 23:59:59
datetime2------------>YYYY-MM-DD HH:MI:SS YEARS BETWEEN 0001 AND 9999

*/
/*
ch and str

char(10)----------'Ali'------->'Ali__________'
nchar(10)---------'G�ray'------>'G�ray_____'
varchar(10)-------'ali'-------->'Ali'
nvarchar(10)------'G�ray'------>'G�ray'
-----------------------------------------------------------------------------------
--------------------------
*/

/*

DATA CONVERSION
| ^
EXPLICIT CON (MANUAL) | DATES | IMPLICIT CON.
(AUTOM.)
| NUMERIC |
| STRING AND CH. |
V

*/

select *
from Production.Product
where ListPrice= '0'
select OrderDate,
CONVERT(nvarchar,OrderDate,104),
CONVERT(NVARCHAR,OrderDate,105),
CONVERT(NVARCHAR,OrderDate,112),
CONVERT(NVARCHAR,OrderDate,130),
CONVERT(NVARCHAR,OrderDate,131)
from Sales.SalesOrderHeader
-----------------------------------------------------------------------------------
---------

You might also like