我想创建一个存储过程以在表'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
但是这里显示错误(@Term_en ='Cat')错误的语法 有什么帮助吗?
答案 0 :(得分:8)
我假设您要将值cat等插入表中;为此,您需要使用过程变量中的值。我不会把你的程序称为与你的表相同的名称,它会引起各种混乱;你可以找到一些很好的资源来命名标准(或来自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
并测试它
exec dbo.terms
@Term_en = 'Cat' ,
@Createdate = '2013-12-12' ,
@Writer = 'Fadi' ,
@Term_Subdomain = 'English'
答案 1 :(得分:0)
以下是如何设置proc中参数的默认值:
CREATE PROCEDURE dbo.terms
@Term_en NVARCHAR(50) = 'Cat',
@Createdate DATETIME = '2013-12-12',
@Writer NVARCHAR(50) = 'Fadi',
@Term_Subdomain NVARCHAR(50) = 'English'
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.terms
(
Term_en ,
Createdate ,
Writer ,
Term_Subdomain
)
VALUES
(
@Term_en,
@Createdate,
@Writer,
@Term_Subdomain
)
END
GO
答案 2 :(得分:0)
您的代码不正确。 你把值放在插入部分。您应该在执行部分
中输入值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
由此执行
exec dbo.terms
@Term_en = 'Cat' ,
@Createdate = '2013-12-12' ,
@Writer = 'Fadi' ,
@Term_Subdomain = 'English'
GO
答案 3 :(得分:0)
-- =============================================
-- Author: xxxx
-- Create date: xx-xx-xxxx
-- Description: Procedure for Inserting Data in table
-- =============================================
CREATE PROCEDURE [dbo].[SP_Emp_Insert]
(
@Empname nvarchar(250)=null,
@Status int=null,
@LoginUserId nvarchar(50)=null,
@Msg nvarchar(MAX)=null OUTPUT
)
AS
BEGIN TRY
INSERT INTO tbl_Employee
VALUES
(
@Empname ,
@Status,
GETDATE(),
GETDATE(),
@LoginUserId
)
SET @Msg='Table Detail Saved Successfully.'
END TRY
BEGIN CATCH
SET @Msg=ERROR_MESSAGE()
END CATCH
GO