简体   繁体   中英

Stored procedure or view?

Currently i am using a lot of stored procedures such as the following:

CREATE PROCEDURE `get_bindings_chart`(IN in_layout_id TINYINT(3), IN in_game_id SMALLINT(5))
BEGIN
    SELECT  b.normal_group, b.normal_action, b.shift_group, b.shift_action, b.ctrl_group, b.ctrl_action, b.alt_group, b.alt_action, b.altgr_group, b.altgr_action, b.extra_group, b.extra_action, b.image_file, b.key_number
    FROM    bindings as b
    WHERE   b.layout_id = in_layout_id
    AND     b.game_id = in_game_id;
END

Could I create a view to do this? Which is better? Thanks.

It is concept about view and stored procedure

Views

1.Does not accepts parameters

2.Can be used as a building block in large query.

3.Can contain only one single Select query.

4.Can not perform modification to any table.

5.Can be used (sometimes) as the target for Insert, update, delete queries.

Stored Procedure

1.Accept parameters

2.Can not be used as a building block in large query.

3.Can contain several statement like if, else, loop etc.

4.Can perform modification to one or several tables.

5.Can not be used as the target for Insert, update, delete queries.

A view is probably better since there's no real logic involved here.

  1. A view can be used in other queries (joins, where clauses, etc.)
  2. A view can have an additional where clause applied to refine results
  3. A view can have an order by clause applied to, well, order the results appropriately for different situations.

You might consider a stored procedure to add more logic...perhaps doing some aggregations and calculations that cannot be performed in a single statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM