带有输入和输出sql的存储过程

oug3syen  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(420)

我正在尝试创建一个存储过程,它接受一个名称/或数字,并返回该名称/数字的特定值
因此,在这段代码中,我尝试编写一个存储过程taht获取公司名称或cvr编号,并返回该公司的符合程度

DROP DATABASE IF EXISTS DRCS;
    CREATE DATABASE DRCS;
    USE DRCS; 

    CREATE TABLE Companies ( 
            ID INT not null,
            Name varchar(255), 
            primary key (ID) 
    ); 

    CREATE TABLE CVR (
            ID INT not null,
            CVR_Number INT 
    ); 

    CREATE TABLE Compliance (
            ID INT not null,
            Complaince_level varchar(255)
    ); 

    CREATE TABLE GDPR (
            ID INT not null,
            GDPR_steps varchar(255) 
    ); 

                INSERT INTO Companies VALUES 
                (1, 'TDC'),
                (2, 'MAERSK'),
                (3, 'LEGO'), 
                (4, 'SAS'),
                (5, 'Carlsberg');

                INSERT INTO CVR VALUES 
                (11, '14773908'),
                (12, '32345794'),
                (13, '47458714'),
                (14, '56994912'),
                (15, '61056416');

                INSERT INTO Compliance VALUES 
                (21, '10%'),
                (22, '20%'),
                (23, '40%'),
                (24, '60%'),
                (25, '80%');

                INSERT INTO GDPR VALUES 
                (31, '1. Awareness'),
                (32, '2. Information you hold'),
                (33, '3. Communication privacy information'),
                (34, '4. Individuals rights'),
                (35, '5. Subject access requests'); 

# HERE MY PROBLEM STARTS

    DELIMITER $$
        CREATE PROCEDURE DoC() 
        BEGIN 
        SELECT * FROM Companies , CVR, Compliance, GDPR; 

        END $$
    DELIMITER ;

    CALL DoC;
4uqofj5v

4uqofj5v1#

四张table之间没有关系!为所有表创建公共id为id=1,2,3,4的行,然后执行联接,然后尝试列出要查看的字段。
插入公司价值(1,“tdc”),(2,“马士基”),(3,“乐高”),(4,“sas”),(5,“嘉士伯”);

INSERT INTO CVR VALUES 
            (1, '14773908'),
            (2, '32345794'),
            (3, '47458714'),
            (4, '56994912'),
            (5, '61056416');

            INSERT INTO Compliance VALUES 
            (1, '10%'),
            (2, '20%'),
            (3, '40%'),
            (4, '60%'),
            (5, '80%');

            INSERT INTO GDPR VALUES 
            (1, '1. Awareness'),
            (2, '2. Information you hold'),
            (3, '3. Communication privacy information'),
            (4, '4. Individuals rights'),
            (5, '5. Subject access requests');

从公司c,cvr v中选择c.id,v.cvr\U编号,其中c.id=v.id
同样地,通过用id连接来添加更多的表。您不需要一个过程来实现这一点,并且一个过程不是为此而设计的。

zyfwsgd6

zyfwsgd62#

可以使用union查询所有表。但是克隆数的所有select查询都应该相同。

DELIMITER $$
    CREATE PROCEDURE DoC() 
  SELECT A.id,A.name FROM
   ( SELECT id as id , name as name FROM Companies 
    UNION ALL
    SELECT id as id , name as name FROM CVR
    UNION ALL
    SELECT id as id , name as name FROM Compliance
    UNION ALL
    SELECT id as id , name as name FROM  GDPR) as A

  END $$
DELIMITER ;

相关问题