How do I fix the subtotal function correctly and can be inserted into the procedure? I have tried to make it but the result is not suitable - sql

CREATE TABLE product(
codeproduct CHAR(5),
nameproduct VARCHAR(50),
unit VARCHAR(20),
price INT
);
CREATE TABLE producer(
codeproducer CHAR(5),
nameproducer VARCHAR(50),
address VARCHAR(20),
city VARCHAR(20),
province VARCHAR(20)
);
CREATE TABLE transaction(
codeproduct CHAR(5),
codeproducer CHAR(5),
qty INT
);
CREATE FUNCTION [dbo].[subtotal](#codeproduct AS CHAR(5))
RETURNS INT
AS
BEGIN
DECLARE #subtotal INT
SELECT #subtotal = product.price * transaction.qty FROM transaction inner join product
ON product.codeproduct=transaction.codeproduct
WHERE product.codeproduct=#codeproduct
RETURN #subtotal
END
GO
CREATE PROCEDURE showdata
AS
BEGIN
SET NOCOUNT ON;
SELECT producer.nameproducer, product.nameproduct, product.unit, transaction.qty,
product.price, [dbo].[subtotal](product.codeproduct) AS 'subtotal'
FROM product join transaction on product.codeproduct=transaction.codeproduct
join producer on producer.codeproducer=transaction.codeproducer
ORDER BY producer.nameproducer;
END
GO
EXEC showdata

Related

In my stored procedure insert into two different tables with if else condition

create procedure sp_student
#student varchar(50);
as
if (#student = marks)
begin
insert into marks
values (student_id int not null, terms varchar(10), subject1 varchar(100), subject2 varchar(100), total varchar(100))
end
else (#student = students)
begin
insert into students
values(student_id int not null, student_name varchar(100), student_age int, mobile_no varchar(20))
end
I didn't get a answer above I mentioned query.
Too many errors. Such a number of errors makes me feel, that you don't really use IBM Db2 product...
create procedure sp_student (
#student varchar(50),
#student_id int,
#terms varchar(10),
#subject1 varchar(100),
#subject2 varchar(100),
#total varchar(100),
#student_name varchar(100),
#student_age int,
#mobile_no varchar(20)
)
BEGIN
if (#student = 'marks') then
insert into marks
values (#student_id, #terms, #subject1 , #subject2 , #total);
elseif (#student = 'students') then
insert into students
values (#student_id ,#student_name, #student_age, #mobile_no);
end if;
END
fiddle
you can't set dataType in value(..) :
create procedure sp_student
#student varchar(50),
#student_id int,
#terms varchar(10),
#subject1 varchar(100),
#subject2 varchar(100),
#total varchar(100),
#student_name varchar(100),
#student_age int,
#mobile_no varchar(20)
as
if (#student = 'marks') then
insert into marks
values (#student_id, #terms, #subject1 , #subject2 , #total);
elseif (#student = 'students') then
insert into students
values (#student_id ,#student_name, #student_age, #mobile_no);
end if;
end

how to add a database constraint 18 or above

i have create a table and I need to add a database constraint "A customer with an age under 18 cannot rent a movie rated “18 or above”." How do I use SQL to add this in my table
You can use a trigger. When you want to enter information in the system to order a movie, do not allow the user to create a record if the age is less than the minimum required.
The following code snippet implements this scenario completely.
CREATE TABLE Customers
(
ID int identity,
FirstName nvarchar(100),
LastName nvarchar(100),
Age int,
primary key(ID))
CREATE TABLE Films
(
ID int identity,
FilmName nvarchar(100),
MinimumAge int,
primary key(ID))
CREATE TABLE OrderFilms
(
ID int identity,
CustomerID int,
FilmID int,
primary key(ID),
foreign key(CustomerID) references Customers,
foreign key(FilmID) references Films)
create trigger trigger_for_insert_into_OrderFilms
On OrderFilms
For Insert
AS
Declare #film int
Declare #customer int
Declare #filmage int
Declare #customerage int
set #film = (SELECT top 1 FilmID FROM inserted)
set #customer = (SELECT top 1 CustomerID FROM inserted)
set #filmage = (SELECT MinimumAge FROM Films WHERE ID = #film)
set #customerage = (SELECT Age FROM Customers WHERE ID = #customer)
IF(#filmage > 18 AND #customerage < 18)
BEGIN
PRINt 'ERROR, CUSTOMER AGE THE CUSTOMER IS YOUNG'
RollBack
END

Using int identity(1,1) as a way to join temporary tables

I'm rewriting a stored procedure that uses int identity(1,1) in >10 temp tables as a way to join these temp tables in the select statement. Below a shorter version of how how the code structure but in the stored procedure I'm editing there this structure is used to return data from each temp table.
CREATE TABLE #temp_MemberInfo
( RecID int identity(1,1),
MemberMBI varchar(13),
MemberName varchar(50),
MemberAddress varchar(150), -- DMP
MemberAddress2 varchar(100), -- DMP
MemberCity varchar(50),
MemberState varchar(3),
MemberZip varchar(11),
MemberMedicalRecordNo varchar(20),
MemberIsMale varchar(10),
MemberIsFemale varchar(10)
)
CREATE TABLE #temp_ProviderInfo
(
RecID int identity(1,1),
ProviderNPI varchar(13),
ProviderName varchar(60), -- DMP
ProviderAddress varchar(150), -- DMP
ProviderAddress2 varchar(100), -- DMP
ProviderCity varchar(40),
ProviderState varchar(3),
ProviderZip varchar(11),
ProviderTelephone varchar(15) )
Select *
from #temp_MemberInfo
Inner Join #temp_ProviderInfo on #temp_ProviderInfo.RecID = #temp_MemberInfo.RecID
The alternative I can think of would be to use an existing primary key in the date base but I would have to join an extra table in the update statements. Is this better from a performance standpoint? Are there other alternatives I'm not considering?

Error converting data type varchar to int.?

My table is below
CREATE TABLE Customers
(
CustomerID int identity(1,1) not null primary key,
Name varchar(50) not null,
PhoneNumber varchar(20) not null
constraint chk_PhoneNumber check(PhoneNumber not like '%[^0-9]%'),
DoorNo varchar(50) not null,
StreetName varchar(50) not null,
City varchar(50) not null,
Statee varchar(50) not null,
Zipcode int not null
)
My stored procedure:
ALTER PROCEDURE stp_customers_insert
(#customerid int,
#name varchar(50),
#phone varchar(50),
#doorno varchar(50),
#streetname varchar(50),
#city varchar(50),
#state varchar(50),
#zip int)
AS
BEGIN
IF EXISTS (SELECT CustomerID FROM Customers WHERE CustomerID = #customerid)
BEGIN
RAISERROR ('employee id already exists', 1, 1)
END
ELSE
BEGIN
INSERT INTO Customers (Name, PhoneNumber, DoorNo, StreetName, City, Statee, Zipcode)
VALUES (#name, #phone, #doorno, #streetname, #city, #state, #zip)
END
END
Sample call:
exec stp_customers_insert 'ram', '674673932', '122', '5th cross', 'trichy', 'tamilnadu', 620001
I get this error:
Msg 8114, Level 16, State 5, Procedure stp_customers_insert, Line 23
Error converting data type varchar to int.
The problem appears to be that your stored procedure expects 8 parameters:
stp_customers_insert(#customerid int, #name varchar(50), #phone varchar(50),
#doorno varchar(50), #streetname varchar(50), #city varchar(50),
#state varchar(50), #zip int)
but you are only passing 7 parameters when you actually call the proc:
exec stp_customers_insert 'ram','674673932','122','5th cross','trichy','tamilnadu',620001
If you don't know or don't want to perform the duplicate check on the CustomerID, then you could slightly modify your call to just pass NULL:
exec stp_customers_insert NULL, 'ram','674673932','122','5th cross','trichy','tamilnadu',620001
As an aside, if the proc is not even inserting the CustomerID, and this field is auto increment, then I don't see the point of passing it. Instead, you might want to consider using a unique constraint to achieve the same.
exec stp_customers_insert 1,'ram','674673932','122','5thcross','trichy','tamilnadu',620001
You have to pass #customerid value in procedure parameters - then it will execute without error.
In your table structure CustomerID is defined as INT. But as your stored procedure is defined in this format:
stp_customers_insert(#customerid int,#name ....
You are sending ram as value for customerid in
exec stp_customers_insert 'ram','674673932'....
Correct this to:
exec stp_customers_insert '*enter the CustId value here*','ram','674673932'....
Replace *enter the CustId value here* with the CustomerID
Also change:
insert into Customers(Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(#name,#phone,#doorno,#streetname,#city,#state,#zip)
To include CustomerID as:
insert into Customers(CustomerID,Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(#customerid,#name,#phone,#doorno,#streetname,#city,#state,#zip)
I suggest remove #customerid from sp CustomerID is auto increment field so no need to pass any value for CustomerID. It should be like this:
alter procedure stp_customers_insert(#name varchar(50),#phone varchar(50),#doorno varchar(50),#streetname varchar(50),#city varchar(50),#state varchar(50),#zip int)
as
begin
if exists(select CustomerID from Customers where Name = #name ,phone = #phone ,doorno = #doorno ,streetname = #streetname ,city= #city,state= #state , zip = #zip )
begin
raiserror('employee id already exists',1,1)
end
else
begin
insert into Customers(Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(#name,#phone,#doorno,#streetname,#city,#state,#zip)
end
end
exec stp_customers_insert 'ram','674673932','122','5th cross','trichy','tamilnadu',620001

Stored procedure to insert a list of different types of items in to a table

I have following two tables
CREATE TABLE Orders
(
OrderID int IDENTITY NOT NULL,
StaffID int NOT NULL,
TotalPrice money NOT NULL,
OrderDateTime dateTime NOT NULL
PRIMARY KEY (OrderID),
FOREIGN KEY (StaffID)
REFERENCES Staff(StaffID)
)
CREATE TABLE OrderDetails
(
OrderDetailID int IDENTITY NOT NULL,
OrderID int NOT NULL,
ItemID int,
ExtrasID int,
ItemQuantity int,
ExtrasQuantity int
PRIMARY KEY (OrderDetailID)
FOREIGN KEY (OrderID)
REFERENCES Orders(OrderID),
FOREIGN KEY (ExtrasID)
REFERENCES Extras(ExtrasID),
FOREIGN KEY (ItemID)
REFERENCES Item(ItemID)
)
I would like to create a stored procedure which will create a new order by inserting data in to both tables. This procedure should take the parameters for the StaffID, TotalPrice and the bought products. Products can be a many Items, many Extras or both.
Is there any way I can have a list of ItemID's and a list of ExtrasID's as the parameters which will then be inserted in to the OrderDetails table correctly?
Create a type first
CREATE TYPE dbo.ty_Product_Orders AS Table
(
ItemID INT
,ExtrasID INT
,ItemQuantity INT
,ExtrasQuantity INT
)
GO
Procedure Definition
Now make your procedure accept a parameter of that type.
CREATE PROCEDURE usp_Place_Order
#StaffID INT
,#TotalPrice MONEY
,#Order dbo.ty_Product_Orders READONLY
,#OrderPlaced BIT = 0 OUTPUT
AS
BEGIN
SET NOCOUNT ON;
-- table variable to hold the identity values
DECLARE #OrderDetails TABLE
(
OrderID INT,
ItemID INT
,ExtrasID INT
,ItemQuantity INT
,ExtrasQuantity INT
)
BEGIN TRY
BEGIN TRANSACTION;
-- Insert Orders
INSERT INTO Orders (StaffID , TotalPrice , OrderDateTime)
OUTPUT inserted.OrderID , inserted.ItemID ,inserted.ExtrasID ,
inserted.ItemQuantity ,inserted.ExtrasQuantity
INTO #OrderDetails(OrderID , ItemID , ExtrasID
,ItemQuantity ,ExtrasQuantity)
SELECT #StaffID , #TotalPrice , GETDATE()
FROM #Order
-- insert OrderDetails
INSERT INTO OrderDetails ((OrderID , ItemID , ExtrasID ,ItemQuantity ,ExtrasQuantity)
SELECT OrderID , ItemID , ExtrasID,ItemQuantity ,ExtrasQuantity
FROM #OrderDetails
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF (##TRANCOUNT <> 0)
ROLLBACK TRANSACTION;
-- Other error logging here
END CATCH
END