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?
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.
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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With