In: Computer Science
Below is what I have to do. This is all performed in SQL. I have written a bunch of code, that I have also provided. Any help is appreciated
Exercises
Complete each of the following exercises. If you are unsure how to accomplish the task, please consult the coursework videos where there are explanations and demos.
Your select statement should include
product id, product name, product category and product
department.
Written Code
use fudgemart_v3
go
--Question 1
--This runs but doesn't supply correct output
select * from fudgemart_products
select right(product_name, charindex(' ', product_name)) as
product_category from fudgemart_products
go
---Runs but returns NULL for product_Category
select product_id, product_name, product_department
from fudgemart_products
order by product_id
declare @product_name as varchar(20)
select right(@product_name, charindex(' ',@product_name)) as
product_category
print len(@product_name)
---question 2-----
drop function dbo.f_vendor_sales
go
declare @vendor_id int
set @vendor_id = 1
select count(*) from fudgemart_products where product_vendor_id =
@vendor_id
go
--Function says it is completed
create function dbo.f_total_vendor_sales(
@vendor_id int --input
) returns int as
begin
declare @count int
set @count = (select count(*) from
fudgemart_products.dbo.product_wholesale_price where
product_vendor_id = @vendor_id)
return @count --output
end
go
---When i attempt function, I get invalid object name
select product_vendor_id, product_wholesale_price,
dbo.f_total_vendor_sales(product_vendor_id) as
total_vendor_sales
from fudgemart_products
----For question 3-------
create procedure p_write_vendor
(
@vendor_name varchar (50),
@vendor_phone varchar (20),
@vendor_website varchar (100)
) as
if exists ( select 1 from fudgemart_vendors
where vendor_name = @vendor_name
or vendor_phone = @vendor_phone
or vendor_website = @vendor_website)
begin
update fudgemart_vendors
set vendor_name
=@vendor_name,
vendor_phone =
@vendor_phone,
vendor_website =
@vendor_website
where vendor_name =
@vendor_name
or vendor_phone =
@vendor_phone
or vendor_website =
@vendor_website
end
else
begin
insert into fudgemart_vendors
values (@vendor_name, @vendor_phone, @vendor_website)
end
--Question 1
select * from fudgemart_products
select right(product_name, charindex(' ', product_name)) as
product_category from fudgemart_products
go
select product_id, product_name, product_department
from fudgemart_products
order by product_id
declare @product_name as varchar(20)
select right(@product_name, charindex(' ',@product_name)) as
product_category
print len(@product_name)
---question 2-----
drop function dbo.f_vendor_sales
go
declare @vendor_id int
set @vendor_id = 1
select count(*) from fudgemart_products where product_vendor_id =
@vendor_id
go
--Function says it is completed
create function dbo.f_total_vendor_sales(
@vendor_id int --input
) returns int as
begin
declare @count int
set @count = (select count(*) from
fudgemart_products.dbo.product_wholesale_price where
product_vendor_id = @vendor_id)
return @count --output
end
select product_vendor_id, product_wholesale_price,
dbo.f_total_vendor_sales(product_vendor_id) as
total_vendor_sales
from fudgemart_products
----For question 3-------
create procedure p_write_vendor
(
@vendor_name varchar (50),
@vendor_phone varchar (20),
@vendor_website varchar (100)
) as
if exists ( select 1 from fudgemart_vendors
where vendor_name = @vendor_name
or vendor_phone = @vendor_phone
or vendor_website = @vendor_website)
begin
update fudgemart_vendors
set vendor_name
=@vendor_name,
vendor_phone =
@vendor_phone,
vendor_website =
@vendor_website
where vendor_name =
@vendor_name
or vendor_phone =
@vendor_phone
or vendor_website =
@vendor_website
end
else
begin
insert into fudgemart_vendors
values (@vendor_name, @vendor_phone, @vendor_website)
end