You are on page 1of 3

Precedure

***
USE [WN64866ES_05]
GO
/****** Object: StoredProcedure [dbo].[create_table] Script Date:
1/12/2022 4:38:27 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[create_table]
AS
BEGIN
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'question'))
BEGIN
drop table question
END

create table question (


id int identity primary key,
id_student int,
No_q int
)
END
GO
/****** Object: StoredProcedure [dbo].[exam] Script Date: 1/12/2022
4:38:27 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE procedure [dbo].[exam]
@id_student int, @max_q int = 99, @i_max int = 5
AS
BEGIN
declare @i int = 1;
declare @z int;
--if (select count(id) from question where id_student=@id_student)=0
--begin
while @i<=@i_max and (select count(id) from question where
id_student=@id_student)<@i_max
begin
exec rnd_v @max_q, 1, @z output;
if (not exists (select id from question where
id_student=@id_student and no_q=@z))
begin
insert into question values (@id_student, @z);
set @i=@i+1;
end
end
--end
select * from question where id_student=@id_student;
END
GO
/****** Object: StoredProcedure [dbo].[rnd_v] Script Date: 1/12/2022
4:38:27 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[rnd_v]
@x int, @y int, @z int output
AS
BEGIN
set @z=cast(rand()*(@x-@y+1)+@y as int)
END
GO

***

create database [WN64866ES_05];

use [WN64866ES_05];

Create PROCEDURE [dbo].[rnd_v]


@x int, @y int, @z int output
AS
BEGIN
set @z=cast(rand()*(@x-@y+1)+@y as int)
END

create procedure create_table


AS
BEGIN
create table question (
id int identity primary key,
id_student int,
No_q int
)
END

exec create_table;

SELECT * FROM INFORMATION_SCHEMA.TABLES

IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES


WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'question'))
BEGIN
drop table question
END

create procedure [dbo].[exam]


@id_student int, @max_q int, @i_max int = 5
AS
BEGIN
declare @i int = 1;
declare @z int;
while @i<=@i_max
BEGIN
exec rnd_v @max_q, 1, @z output
insert into question values (@id_student, @z)
set @i=@i+1;
END
END

exec create_table;

exec exam 45866, 50;

select * from question;

exec exam 96666;

You might also like