In this post we will do some example about inserting data to a table with stored procedure. Advantages of using sp when adding data is…
- More faster
- We can control data
- We can do more process in one time
Example -1: Add a type named “informatics” to the types table with strored procedure.
1 2 3 4 |
Create Procedure sp_Add_DataToTypes(@typename varchar(30)) as Begin Insert into types(name) values(@typename) End |
Firstly, we must execute the code above and get successful massage. Then we can add every data we want like below.
1 |
Execute sp_Add_DataToTypes 'Informatics' |
First, we must write execute, secondly the name of procedure and then we must write parameters. If there is more than 1 parameter, we separate it with a comma.
Result: (1 row affected)
Example -2: Create a stored procedure that add data to types table. when adding data it must control the typename. If typename data’s letter count less then 3, the data must not add.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Create Procedure sp_Add_DataToTypes(@typename varchar(30)) as Begin If (len(@typename)>3) Begin Insert into types(name) values(@typename) Print 'Successfull' End Else Begin Print 'Typename must be more than 3 letters' End End Execute sp_Add_DataToTypes 'Informatics' --Successfull Execute sp_Add_DataToTypes 'AB' --Typename must be more than 3 letters |