Trigger That updates the status of a video if it gets rented out or returned - database

I am creating a Trigger That updates the status of a video if it gets rented out or returned. At the bottom is my trigger, but it has errors. The following errors are as follows.
LINE/COL ERROR
6/28 PLS-00103: Encountered the symbol "IF" when expecting one of the following: ( - + case mod new not null avg coun t current exists max min prior sql stddev sum variance execute forall merge time timestamp interval date pipe < an alternatively-quoted string literal with c haracter set specification>
6/51 PLS-00103: Encountered the symbol ";" when expecting one of the f ollowing: * & - + / at mod remainder rem then and or || multiset
--Video TABLE
CREATE TABLE Video(
Vid_Num VARCHAR2(10),
Category VARCHAR2(30),
Status VARCHAR2(15),
Title VARCHAR2(30),
Catalog_Num VARCHAR2(10),
Rental_fee NUMBER(8,2),
Cost NUMBER(8,2),
Main_Actors VARCHAR2(100),
Director VARCHAR2 (50),
Rental_Code VARCHAR2(10) CONSTRAINT Rental_ID_FK REFERENCES Rental(Rental_Code),
Num_Copies NUMBER(4),
CONSTRAINT Vid_Num_PK PRIMARY KEY (Vid_Num, Catalog_Num));
--Insert for Video Table
INSERT INTO Video
VALUES ('V101','Action','Available','Expendables','C1','4.99','0','Stallone','Simon West','R101','2');
INSERT INTO Video
VALUES ('V102','Drama','Available','The Judge','C2','4.99','0','Robert Downey Jr','David Dobkin','R101','2');
INSERT INTO Video
VALUES ('V103','Family','Available','Lion King','C3','1.99','0','James Jones','Rogers Allers','R105','3');
INSERT INTO Video
VALUES ('V104','Suspense','Available','Saw II','C4','2.99','0','Tobin Bell','James Wan','R103','2');
INSERT INTO Video
VALUES ('V105','Sci-Fi','Available','Interstellar','C5','4.99','0','Matthew McConaughy','Christopher Nolon','R101','2');
INSERT INTO Video
VALUES ('V106','Action','Available','Hunger Games','C6','4.99','0','Jennifer Lawrence','Gary Ross','R101','2');
INSERT INTO Video
VALUES ('V107','Action','Available','I Am Legend','C7','1.99','0','Will Smith','Francis Lawerence','R105','2');
INSERT INTO Video
VALUES ('V108','Drama','Available','Hancock','C8','1.99','0','Will Smith','Peter Berg','R105','2');
INSERT INTO Video
VALUES ('V109','Comedy','Available','Billy Madison','C9','1.99','0','Adam Sandler','Tamara Davis','R105','2');
INSERT INTO Video
VALUES ('V110','Comedy','Available','Tommy Boy','C10','1.99','0','Chris Farley','Peter Segal','R105','2');
--Transaction Table
CREATE TABLE Transaction(
Rental_Num VARCHAR2(8) CONSTRAINT Rental_Num_PK PRIMARY KEY,
Mem_Num VARCHAR2(10) CONSTRAINT Member_Num_FK REFERENCES Member(Mem_Num),
Full_Name VARCHAR2(50),
Vid_Num VARCHAR2(10),
Title VARCHAR2(50),
Catalog_Num VARCHAR2(10),
Date_Rented_Out DATE,
Date_Returned DATE,
Rental_Code VARCHAR2(10) CONSTRAINT Rental_Code_FK REFERENCES Rental(Rental_Code),
Rental_fee NUMBER(8,2),
CONSTRAINT Video_Num_FK FOREIGN KEY(Vid_Num, Catalog_Num) REFERENCES Video(Vid_Num, Catalog_Num));
--Insert for Transaction Table
INSERT INTO Transaction
VALUES ('1','Mem102','OJ Simpson', 'V101', 'Expendables', 'C1', '14-Nov-14', '16-Nov-14', 'R101', '4.99');
INSERT INTO Transaction
VALUES ('2','Mem105','Donte Stallworth', 'V103', 'Lion King', 'C3', '14-Nov-14', '16-Nov-14', 'R105', '1.99');
INSERT INTO Transaction
VALUES ('3','Mem103','Ray Rice', 'V102', 'The Judge', 'C2', '14-Nov-14', '16-Nov-14', 'R101', '4.99');
INSERT INTO Transaction
VALUES ('4','Mem101','Mike Vick', 'V105', 'Intersteller', 'C5', '14-Nov-14', '16-Nov-14', 'R101', '4.99');
INSERT INTO Transaction
VALUES ('5','Mem104','Pacman Jones', 'V104', 'Saw II', 'C4', '14-Nov-14', '16-Nov-14', 'R103', '2.99');
INSERT INTO Transaction
VALUES ('6','Mem104','Pacman Jones', 'V106', 'Hunger Games', 'C6', '14-Nov-14', '16-Nov-14', 'R101', '4.99');
INSERT INTO Transaction
VALUES ('7','Mem104','Pacman Jones', 'V107', 'I Am Legend', 'C7', '14-Nov-14', '16-Nov-14', 'R105', '1.99');
INSERT INTO Transaction
VALUES ('8','Mem104','Pacman Jones', 'V108', 'Hancock', 'C8', '14-Nov-14', '16-Nov-14', 'R105', '1.99');
INSERT INTO Transaction
VALUES ('9','Mem104','Pacman Jones', 'V109', 'Billy Madison', 'C9', '14-Nov-14', '16-Nov-14', 'R105', '1.99');
INSERT INTO Transaction
VALUES ('10','Mem104','Pacman Jones', 'V110', 'Tommy Boy', 'C10', '14-Nov-14', '16-Nov-14', 'R105', '1.99');
--Trigger
CREATE or REPLACE Trigger Available_Rule
BEFORE INSERT ON Transaction
FOR EACH ROW
BEGIN
SELECT Date_Rented_Out,Date_Returned, Status
FROM Transaction, Video;
IF Date_Rented_Out > 0 AND IF Date_Returned = NULL;
THEN Status = Unavailable;
End IF;
ELSE
IF Date_Rented_Out > 0 AND IF Date_Rented > 0;
THEN Status = Available;
End IF;
END;
/
show errors;

The context of the current trigger's row values are available with the default, :new property. You
do not perform a select on the table you are triggered on. Instead try this (you may have to tweak a bit, but basic concept). Note that DATE data types are not checked against zero. It should either have a NULL value, or some other valid value. You will need to fix that. I would suggest spending some time reading about PL/SQL syntax also.
CREATE or REPLACE Trigger Available_Rule
BEFORE INSERT ON Transaction
FOR EACH ROW
BEGIN
IF :new.Date_Rented_Out is not null AND IF :new.Date_Returned = NULL
THEN
update video
set status = 'Available'
where vid_num = :new.vid_num;
End IF;
ELSE
-- etc. checks ---
END;
/

Related

Trigger throws primary key violation error: Cannot insert duplicate key in object

create table Hotel
(
hotel_id integer primary key NOT NULL,
hotel_name varchar(50) NOT NULL UNIQUE,
location_ varchar(50) NOT NULL,
rates varchar(10) check(rates in ('5star','4star','3star','2star','1star')),
);
create table Room
(
room_no integer primary key NOT NULL,
total_rooms integer NOT NULL,
room_price real check (room_price >= 0),
hotel_id integer foreign key references Hotel
);
insert into Hotel values(1,'sevensay','gamapaha','4star')
insert into Hotel values(2,'sarasvi','gamapaha','3star')
insert into Hotel values(3,'galadari','colombo','5star')
insert into Hotel values(4,'kingsbary','colombo','4star')
insert into Hotel values(5,'niramliii','gamapaha','5star')
insert into Hotel values(6,'sadalnka','kandy','3star')
insert into Hotel values(7,'sri lnkani','kandy','5star')
insert into Room values(100,10000,1)
insert into Room values(220,20000,2)
insert into Room values(160,1000,3)
insert into Room values(100,12000,4)
insert into Room values(50,15000,5)
insert into Room values(80,10000,6)
insert into Room values(100,20000,7)
drop table Room
drop table Hotel
select * from Hotel
select * from Room
create trigger rooms_availability
on Room
for insert
as
begin
declare #hotel_id integer
declare #total_rooms integer
select #hotel_id = hotel_id from inserted
select #total_rooms = count(*) from Room where hotel_id = #hotel_id
rollback transaction
if #total_rooms > 80
begin
print 'we have only 80 rooms .we cannot book the other rooms'
end
end
insert into Room values(300,10000,6)
How can I handle this error?
Msg 2627, Level 14, State 1, Line 25
Violation of PRIMARY KEY constraint 'PK__Room__1967F4191F8BEC00'. Cannot insert duplicate key in object 'dbo.Room'. The duplicate key value is (506).
The statement has been terminated.
The error you are showing is caused from trying to insert a row into Room with a duplicate primary key. However the code you provide doesn't have that error. And if you use an identity column (recommended for primary keys) you will never have that issue.
The more important issue is in your trigger, where you are not handling that fact that Inserted can have multiple rows. You can handle this correctly using a set based approach:
create trigger rooms_availability
on Room
for insert
as
begin
if exists (
select 1
from Room
where hotel_id in (select hotel_id from Inserted)
group by hotel_id
having count(*) > 80
)
begin
print 'We have only 80 rooms. We cannot book the other rooms.'
rollback;
end;
end
Note: I assume Print is being used for debugging. Once its working you will want to use throw.

t-sql insert statement where primary key is a decimal

2 years ago I created a table that has 22 rows. Each row is a step/page in filing an application for hire. I realized back then I would most likely be asked to insert steps as the business grew. I was right. I need to insert a new step between step 21 & 22. So I want to create a new row in that table with stepId = 21.5. But the insert statement fails.
INSERT INTO frznStep (
stepId
,myField1
,myField2
,myField3
)
VALUES (
21.5
,'xxx'
,'yyy'
,'zzz'
)
the error msg is:
Violation of PRIMARY KEY constraint 'PK_frznStep'. Cannot insert
duplicate key in object 'dbo.frznStep'. The duplicate key value is
(22).
I suspect when you script out the table, you'll see that the precision of your decimal column is 0, so something like stepId decimal(9,0)
If you have a non-zero value for the decimal precision, the following repro works
USE tempdb
DROP TABLE IF EXISTS #frznStep;
CREATE TABLE #frznStep
(
stepId decimal(9, 1) NOT NULL
, myField1 varchar(255) NOT NULL
, myField2 varchar(255) NOT NULL
, myField3 varchar(255) NOT NULL
, CONSTRAINT PK_frznStep PRIMARY KEY (stepId)
);
insert into #frznStep (stepId, myField1, myField2, myField3) values (21, 'www', 'yyy', 'zzz');
insert into #frznStep (stepId, myField1, myField2, myField3) values (22, 'yyy', 'yyy', 'zzz');
insert into #frznStep (stepId, myField1, myField2, myField3) values (21.5, 'xxx', 'yyy', 'zzz');
GO
When you use a 0 scale, you'll get 21 and 22 into the table but 21.5 will be implicitly converted to decimal(x,0) which then violates the primary key constraint.
-- Redeclare as 0 precision
DROP TABLE IF EXISTS #frznStep;
CREATE TABLE #frznStep
(
stepId decimal(9, 0) NOT NULL
, myField1 varchar(255) NOT NULL
, myField2 varchar(255) NOT NULL
, myField3 varchar(255) NOT NULL
, CONSTRAINT PK_frznStep PRIMARY KEY (stepId)
);
insert into #frznStep (stepId, myField1, myField2, myField3) values (21, 'www', 'yyy', 'zzz');
insert into #frznStep (stepId, myField1, myField2, myField3) values (22, 'yyy', 'yyy', 'zzz');
--Msg 2627, Level 14, State 1, Line 36
--Violation of PRIMARY KEY constraint 'PK_frznStep'. Cannot insert duplicate key in object 'dbo.#frznStep'. The duplicate key value is (22).
--The statement has been terminated.
insert into #frznStep (stepId, myField1, myField2, myField3) values (21.5, 'xxx', 'yyy', 'zzz');
You options are either to change your data type to include the scale (which will require dropping and recreating the primary key as the column is part of it) Or scale everything up by a factor of 10 and then you can insert into the 215 nicely between 210 and 220. (A "trick" I learned the hard way programming Apple Basic ages ago)
The first intuitive answer off the bat would be to convert your primary key to a numeric type, such as decimal.
However, is there really a reason to think of the step as 21.5? Or are you just trying to fit it between 21 and 22? I say this because the more ideal situation would be to have a primary key that simply serves as an identity. Then have a separate column that identifies the step number. That way, instead of having the step be 21.5, you'll simply have it be step 22, and then you can change step 22 to step 23.
alter table frznStep add column stepOrd int null;
update frznStep set stepOrd = stepId;
update frznStep set stepOrd = 23 where stepOrd = 22;
insert frznStep (stepId, stepOrd, ...) values (100, 22, ...);
You could also convert stepId to autoincrement. Though I believe you'll have to drop the table and recreate it in that case.
You’re getting the error because your id column is effectively integer and your attempted insert value is being rounded to integer, thus colliding with an existing key value.
Rather than using the id column as both unique identifier and step order, which is a design flaw (overloading a column), specify the steps as a chain, like a linked list, by introducing a column, perhaps called nextStepId, that stores the id of the next step to run.
This would separate the concerns of primary key being the row identifier and step order, giving control of step order independent of id values being any particular value relative to each other.

How to create a Trigger on table to identify whether a particular record been deleted/updated and store the information of entity updating it?

i need to save information in another temp table say , TableTemp having the records being modified and with one more column defining which entity updated it.
You look like you're just discovering, and ask very wide questions. However, here is a possible solution, assuming the below:
a_sqnc is the sequence you will use in TableTemp to keep track of the order of actions in column NO_ORD (even though there is also a D_UPD column with the modification time).
create sequence a_sqnc
minvalue 1
maxvalue 99999999
start with 1
increment by 1
nocache;
TableTemp will have a TABLE_NAME column in order to track changes from different tables. It also have a PK_VALUE and ROW_VALUE where we store the data that changed. Here is the table creation with useful indexes:
create table TableTemp (
table_name VARCHAR2(50) not null,
action VARCHAR2(240) not null,
no_ord NUMBER(12) not null,
nature VARCHAR2(3) not null,
pk_value VARCHAR2(4000),
row_value VARCHAR2(4000),
ori VARCHAR2(250),
c_user VARCHAR2(20),
d_upd DATE
);
create index AP_D_UPD on TableTemp (D_UPD);
create index AP_NO_ORD on TableTemp (NO_ORD);
create index AP_TABLE_NAME on TableTemp (TABLE_NAME);
Say you have a simple table BANK with two columns PK_val (the primary key) and val:
create table BANK (
pk_val VARCHAR2(50) not null,
val VARCHAR2(240) not null
);
alter table BANK
add constraint BK_PK primary key (pk_val)
using index ;
Use DBMS_APPLICATION_INFO.READ_MODULE(w_sess_mod, w_sess_act) to know what module and what action operates: I concatenate both in column ORI in TableTemp;
user Oracle session variable will allow you tracking who did the change in column c_user;
Here is how to create trigger AUD_BNK to track changes in table BANK; it will categorize in 3 actions: DELETE, UPDATE, INSERT (you can remove the INSERT case if needed).
CREATE OR REPLACE TRIGGER "AUD_BNK"
AFTER DELETE OR INSERT OR UPDATE
ON BANQUE
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
w_a VARCHAR2(10);
W_ERRM VARCHAR2(1000);
W_CODE VARCHAR2(1000);
w_n VARCHAR2(200) := 'BANK';
w_id NUMBER := a_sqnc.nextval;
w_act v$session.action%type;
w_mod v$session.module%type;
w_ori TableTemp.ORI%TYPE;
BEGIN
DBMS_APPLICATION_INFO.READ_MODULE(w_mod, w_act);
w_ori := 'Module : '||w_mod ||' ; Action : '||w_act;
----------------------------------
-- test which action is for change
----------------------------------
IF UPDATING
THEN
w_a := 'UPDATE';
ELSIF DELETING
THEN
w_a := 'DELETE';
ELSIF INSERTING
THEN
w_a := 'INSERT';
END IF;
----------------------------------
-- Insert into TableTemp
----------------------------------
If w_a in ('UPDATE', 'DELETE') then
Insert into TableTemp
Select w_n, w_a, w_id, 'OLD', :OLD.pk_val, :OLD.val
, w_ori, user, sysdate
From Dual;
End if;
-- if you update, there is a new value and an old value
If w_a in ('UPDATE', 'INSERT') then
Insert into TableTemp
Select w_n, w_a, w_id, 'NEW', :NEW.pk_val, :NEW.val
, w_ori, user, sysdate
From Dual;
End if;
Exception
When others then
Begin
W_ERRM := SQLERRM;
W_CODE := SQLCODE;
-- try inserting in case of error anyway
Insert into TableTemp
Select w_n, w_a, -1, 'ERR', 'Grrr: '||W_CODE, W_ERRM
, w_ori, user, sysdate
From Dual;
End;
End;
/
Beware!
This way of tracking every change on the table will deeply impair performances if table changes. But it is great for parameter tables that scarcely change.

SQL - How to INSERT a foreign key as a value for a column

I know this is rather basic, and i've searched for answers for quite some time, but I'm troubled.
I don't know how to make my coding readable on here but here it is.
Here's the query for making the table in question:
CREATE TABLE customer
( customer_id INT NOT NULL CONSTRAINT customer_pk PRIMARY KEY IDENTITY,
first_name VARCHAR(20) NOT NULL,
surname VARCHAR(20) NOT NULL,
dob DATETIME NOT NULL,
home_address VARCHAR(50) NOT NULL,
contact_number VARCHAR(10) NOT NULL,
referrer_id INT NULL FOREIGN KEY REFERENCES customer(customer_id),
);
And here's the problem code:
--fill customer table
INSERT INTO customer
VALUES ( 'Harold', 'Kumar', '2010-07-07 14:03:54', '3 Blue Ln, Perth', 0812391245, NULL )
INSERT INTO customer
VALUES ( 'Bingo', 'Washisnameoh', '2010-09-21 12:30:07', '3 Red St, Perth', 0858239471, NULL )
INSERT INTO customer
VALUES ( 'John', 'Green', '2010-11-07 14:13:34', '4 Blue St, Perth', 0423904823, NULL )
INSERT INTO customer
VALUES ( 'Amir', 'Blumenfeld', '2010-11-01 11:03:04', '166 Yellow Rd, Perth', 0432058323, NULL)
INSERT INTO customer
VALUES ( 'Hank', 'Green', '2010-07-07 16:04:24', '444 Orange Crs, Perth', 0898412429, 8)
(Specifically the line with the 8 value at the end.)
When executing the second query it responds with this:
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted
with the FOREIGN KEY SAME TABLE constraint
"FK_customer_referr__5772F790". The conflict occurred in database
"master", table "dbo.customer", column 'customer_id'. The statement
has been terminated.
Appreciate your help with this.
1)
You have a primary key on customer_id - and your insert statements do not have value for customer id
2)
You have a self referencing foreign key in the form of referrer_id referring to customer_id.
When you are inserting a record with referrer_id which is not null, in your case which is '8', make sure you already inserted a record with customer_id '8'
How do you know that the referrer_id is supposed to be 8 ??
What you need to do is catch the value of the customer_id inserted, and then used that in your second query:
DECLARE #referToID INT
INSERT INTO dbo.Customer(first_name, surname, dob, home_address, contact_number, referrer_id)
VALUES ('Harold', 'Kumar', '2010-07-07 14:03:54', '3 Blue Ln, Perth', 0812391245, NULL)
SELECT #ReferToID = SCOPE_IDENTITY() ; -- catch the newly given IDENTITY ID
INSERT INTO dbo.Customer(first_name, surname, dob, home_address, contact_number, referrer_id)
VALUES ('Hank', 'Green', '2010-07-07 16:04:24', '444 Orange Crs, Perth', 0898412429, #ReferToID)
I don't know which row you want to refer to (you didn't specify) - but I hope you understand the mechanism:
insert the new row into your table
get the newly inserted ID by using SCOPE_IDENTITY
insert the next row which refers to that first row and use that value returned by SCOPE_IDENTITY
Update: if you really want to have a given row reference itself (strange concept.....), then you'd need to do it in two steps:
insert the new row into your table
get the newly inserted ID by using SCOPE_IDENTITY
update that row to set the referrer_id
Something like this:
DECLARE #NewCustomerID INT
INSERT INTO dbo.Customer(first_name, surname, dob, home_address, contact_number)
VALUES ('Hank', 'Green', '2010-07-07 16:04:24', '444 Orange Crs, Perth', 0898412429)
SELECT #NewCustomerID = SCOPE_IDENTITY() ; -- catch the newly given IDENTITY ID
UPDATE dbo.Customer
SET referrer_id = #NewCustomerID
WHERE customer_id = #NewCustomerID
The only problem you have here is the identity must have a seed value which can be like Identity(1,1) where the first 1 is the starting point and the send 1 is the auto seed number...the re run your insert statement

Create #TableVariable based on an existing database table?

I want to use table variables in stored procedures but here is an issue. My tables are very large and declaring a table variable need a long code to write and debug as well.
Kindly advice me some way to declare table variables quickly, is it possible to create table variable based on an existing table ?
Or please share any tip to create code for creating table variable.
Thanks
Right click the table, choose Script As Create.
Replace create table xxx with declare #xxx table.
As discussed in this SO Question you can't select into a table variable.
When you say "large", if you mean a lot of columns, the best approach for you would probably be to script that table as create and save the definition and use that in your Declare statement.
If you mean large as far as the number of rows you'll have in the table variable, you may want to consider using a temporary table which you could then do a SELECT INTO statement to create it based off of the original.
SELECT * INTO #tmpTable FROM srcTable
The simple answer is "No you cannot create a variable table based on other table"
But, you can generalise a bit by using a type table.
For example (note: you can add documentation to the type table and columns, which can be useful for future reference):
PRINT 'type: [dbo].[foo_type]'
PRINT ' - Check if [dbo].[foo_type] TYPE exists (and drop it if it does).'
GO
IF EXISTS (SELECT 1 FROM sys.types WHERE name = 'foo_type' AND is_table_type = 1 AND SCHEMA_ID('dbo') = schema_id)
BEGIN
-- Create the proc
PRINT ' - Drop TYPE [dbo].[foo_type]';
DROP TYPE [dbo].[foo_type];
END;
GO
PRINT ' - create [dbo].[foo_type] TYPE.'
GO
CREATE type [dbo].[foo_type] as Table
(
[id] int identity(1,1) PRIMARY KEY
, [name] varchar(255) NOT NULL
, [description] varchar(255)
, numeric_data numeric(26, 6)
, datetimestamp datetime default getdate()
, Unique_Indicator float unique not null default cast(getdate() as float)
, CHECK (Unique_Indicator > 0)
);
GO
PRINT ' - done.'
GO
-- Adding the descriptions
PRINT ' - Adding Type level Description'
EXEC sys.sp_addextendedproperty #name=N'MS_Description', #value=N'describe the usage of this type.' , #level0type=N'SCHEMA',#level0name=N'dbo', #level1type=N'TYPE',#level1name=N'foo_type'
GO
PRINT ' - Adding Column level Descriptions'
PRINT ' - column: id'
EXEC sys.sp_addextendedproperty #name=N'MS_Description', #value=N'ID of the record...' , #level0type=N'SCHEMA',#level0name=N'dbo', #level1type=N'TYPE',#level1name=N'foo_type', #level2type=N'COLUMN',#level2name=N'ID';
GO
------------------------------------------------------------------------------------------------
-- use the type defined above to manipulate the variable table:
declare #foo_table foo_type;
--insert using the default value for the for the unique indicator.
insert into #foo_table (name, [description], numeric_data, datetimestamp)
values('babar', 'this is the king of the elephants', 12.5, '1931-01-01')
;
-- insert the records one by one to use the scope_identity() for the unique indicator.
insert into #foo_table (name, [description], numeric_data, datetimestamp, Unique_Indicator )
values('zephyr', 'Babar''s monkey friend', 5.5, '1932-01-01', scope_identity())
;
insert into #foo_table (name, [description], numeric_data, datetimestamp, Unique_Indicator )
values ('Celeste', 'Babar''s sister', 19.5, '1932-01-01', scope_identity())
;
-- insert using a list of values
insert into #foo_table (name, [description], numeric_data, datetimestamp, Unique_Indicator )
values('Babur', 'Not Babar!!!', 1483, '1983-02-14', 10)
, ('Mephistopheles', 'Not Babar either...', 666, '1866-01-01',11)
;
-- insert using a select
insert into #foo_table (name, [description], numeric_data, datetimestamp, Unique_Indicator)
(select 'Conan', 'The Cimmerian barbarian', 850, '1932-12-01',99 union
select 'Robert E. Howard', 'Conan''s creator', 30, '1906-01-22', 100
);
-- check the data we inserted in the variable table.
select * from #foo_table;
-- Clean up the example type
DROP TYPE [dbo].[foo_type];

Resources