Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a stored procedure to insert new data into a table

I want to create a stored procedure to insert a new row in a table 'dbo.Terms'

CREATE PROCEDURE dbo.terms 
       @Term_en                      NVARCHAR(50)  = NULL   , 
       @Createdate                   DATETIME      = NULL   , 
       @Writer                       NVARCHAR(50)  = NULL   , 
       @Term_Subdomain               NVARCHAR(50)  = NULL  
AS 
BEGIN 
     SET NOCOUNT ON 

     INSERT INTO dbo.terms
          (                    
            Term_en                     ,
            Createdate                  ,
            Writer                      ,
            Term_Subdomain                 
          ) 
     VALUES 
          ( 
            @Term_en    = 'Cat'               ,
            @Createdate   = '2013-12-12'      ,
            @Writer         = 'Fadi'          ,
            @Term_Subdomain = 'English'                    
          ) 

END 

GO

But is shows me an error here ( @Term_en = 'Cat') incorrect syntax Any help?

like image 427
Ameen Chaabani Avatar asked Dec 12 '13 13:12

Ameen Chaabani


People also ask

Can stored procedure insert into table?

A stored procedure is a set of SQL code specifically written for performing a task. We can write a stored procedure and execute it with a single line of SQL code. One of the tasks that you can perform with the stored procedures is to insert rows in a table.

How do you insert data into a procedure?

To execute this stored procedure, we can either use EXEC statement as explained above or right click the stored procedure and choose Execute Stored Procedure… option. This will bring Execute Procedure dialog box with equal number of rows and value textbox as the stored procedure parameters.

How do you insert data into a table?

To insert records into a table, enter the key words insert into followed by the table name, followed by an open parenthesis, followed by a list of column names separated by commas, followed by a closing parenthesis, followed by the keyword values, followed by the list of values enclosed in parenthesis.


Video Answer


1 Answers

I presume you want to insert the values cat etc into the table; to do that you need to use the values from your procedures variables. I wouldn't call your procedure the same name as your table it will get all kinds of confusing; you can find some good resources for naming standards (or crib from Adventureworks)

CREATE PROCEDURE dbo.terms 
       @Term_en                      NVARCHAR(50)  = NULL   , 
       @Createdate                   DATETIME      = NULL   , 
       @Writer                       NVARCHAR(50)  = NULL   , 
       @Term_Subdomain               NVARCHAR(50)  = NULL  
AS 
BEGIN 
     SET NOCOUNT ON 

     INSERT INTO dbo.terms
          (                    
            Term_en                     ,
            Createdate                  ,
            Writer                      ,
            Term_Subdomain                 
          ) 
     VALUES 
          ( 
            @Term_en,
            @Createdate,
            @Writer,
            @Term_Subdomain
          ) 

END 

GO

And to test it

exec dbo.terms 
    @Term_en    = 'Cat'               ,
    @Createdate   = '2013-12-12'      ,
    @Writer         = 'Fadi'          ,
    @Term_Subdomain = 'English' 
like image 70
u07ch Avatar answered Sep 23 '22 15:09

u07ch