我对存储过程很新。我有一个Id
自动增量表。我想在表格中插入,更新和删除行。
我的存储过程出了什么问题?
CREATE PROCEDURE [dbo].[hrm_Languages]
(
@Name varchar(120),
@CreatedBy bigint=0,
@UpdatedBy bigint=0,
@IsDeleted bit=0
@ID int OUTPUT
)
AS
BEGIN
SELECT @ID = ISNULL(MAX(ID), 0) + 1
FROM [dbo].[Languages]
IF @StatementType = 'Insert'
BEGIN
insert into [dbo].[Languages] (Name, CreatedOn, UpdatedOn, CreatedBy, UpdatedBy, IsDeleted)
values(@Name, DateTime(), DateTime, @CreatedBy, @UpdatedBy, @IsDeleted)
SELECT @Name
WHERE NOT EXISTS (SELECT ID, NAME
FROM TableName
WHERE NAME = @Name)
BEGIN
END
END
IF @StatementType = 'Select'
BEGIN
select * from [dbo].[Languages]
END
IF @StatementType = 'Update'
BEGIN
UPDATE [dbo].[Languages]
SET Name = @Name, UpdateOn = DateTime()
WHERE ID = @ID
END
else IF @StatementType = 'Delete'
BEGIN
DELETE FROM [dbo].[Languages] WHERE ID = @ID
END
end
获取这些错误:
Msg 102,Level 15,State 1,Procedure hrm_Languages,Line 7
'@ID'附近的语法不正确。Msg 137,Level 15,State 1,Procedure hrm_Languages,Line 12
必须声明标量变量“@ID”。Msg 137,Level 15,State 2,Procedure hrm_Languages,Line 15
必须声明标量变量“@StatementType”。Msg 195,Level 15,State 10,Procedure hrm_Languages,Line 17
'DateTime'不是公认的内置函数名称。消息156,级别15,状态1,程序hrm_Languages,第24行
关键字“END”附近的语法不正确。Msg 137,Level 15,State 2,Procedure hrm_Languages,Line 26
必须声明标量变量“@StatementType”。Msg 137,Level 15,State 2,Procedure hrm_Languages,Line 31
必须声明标量变量“@StatementType”。Msg 195,Level 15,State 10,Procedure hrm_Languages,Line 34
'DateTime'不是公认的内置函数名称。Msg 137,Level 15,State 2,Procedure hrm_Languages,Line 38
必须声明标量变量“@StatementType”。Msg 137,Level 15,State 2,Procedure hrm_Languages,Line 40
必须声明标量变量“@ID”。
我还想在我的网页上显示消息,使用标签插入名称,Nme已删除,NAme已存在
答案 0 :(得分:2)
好的,您的sproc问题(再次阅读之后),请参阅评论:
-- If you worked for me, I'd be having words about this name, it does not state
-- what this sproc does.
CREATE PROCEDURE [dbo].[hrm_Languages]
(
@Name varchar(120),
@CreatedBy bigint=0,
@UpdatedBy bigint=0,
@IsDeleted bit=0, -- You need a comma here. You hadn't posted your errors when I answered
-- this site isn't really for fixing your compile errors... read
-- the error text, it's quite helpful. Something wrong on line 7...
@ID int OUTPUT
)
AS
BEGIN
-- Problem 1. You are selecting this ID prior to inserting. This won't always be the
-- ID inserted, if your system is getting heavy use.
SELECT @ID = ISNULL(MAX(ID), 0) + 1
FROM [dbo].[Languages]
-- Problem 2. You don't pass in @StatementType - this won't even compile.
IF @StatementType = 'Insert'
BEGIN
insert into [dbo].[Languages] (Name,CreatedOn,UpdatedOn,CreatedBy,UpdatedBy,IsDeleted)
values( @Name, DateTime(), DateTime, @CreatedBy, @UpdatedBy,@IsDeleted)
-- To get the inserted ID, do the following
SELECT @ID = SCOPE_IDENTITY()
-- Not sure what the following is for.
SELECT @Name
WHERE NOT EXISTS (SELECT ID,NAME FROM TableName
WHERE NAME=@Name)
BEGIN
END
END
-- This is where your sproc should end and the following should be three other sprocs.
IF @StatementType = 'Select'
BEGIN
select * from [dbo].[Languages]
END
-- Problem 3. You don't seem to be passing in and ID, just for output, and you set the ID in the
-- first line. If your system is heavily used, this might just end up updating a newly inserted
-- row, but most likely will update nothing.
-- Same goes for the delete below. Create separate sprocs for doing these things.
IF @StatementType = 'Update'
BEGIN
UPDATE [dbo].[Languages] SET
Name = @Name, UpdateOn= DateTime()
WHERE ID = @ID
END
else IF @StatementType = 'Delete'
BEGIN
DELETE FROM [dbo].[Languages] WHERE ID = @ID
END
end
答案 1 :(得分:0)
除了Paddy的答案,您必须使用GetDate()
而不是DateTime()
来获取当前的DateTime。
答案 2 :(得分:-1)
我在语法上纠正了存储过程。我已经就我所做的改变写了评论。正确的sql会为你创建SP。
CREATE PROCEDURE [dbo].[hrm_Languages]
(
@Name varchar(120),
@CreatedBy bigint=0,
@UpdatedBy bigint=0,
@IsDeleted bit=0, -- Insert comma
@StatementType Varchar(20), -- Add variable @StatementType
@ID int OUTPUT
)
AS
BEGIN
IF @StatementType = 'Insert'
BEGIN
-- As @ID will reset again after insertion of record so following two line are not needed
SELECT @ID = ISNULL(MAX(ID), 0) + 1
FROM [dbo].[Languages]
SELECT @Name
WHERE NOT EXISTS (SELECT ID,NAME FROM TableName -- What is table name for
WHERE NAME=@Name)
insert into [dbo].[Languages] (Name,CreatedOn,UpdatedOn,CreatedBy,UpdatedBy,IsDeleted)
values( @Name, SYSDATETIME(), SYSDATETIME(), @CreatedBy, @UpdatedBy,@IsDeleted)
-- SysDateTime is used to get current datetime
SELECT @ID = SCOPE_IDENTITY()
End
ELSE IF @StatementType = 'Select'
Begin
select * from [dbo].[Languages]
END
ELSE IF @StatementType = 'Update'
BEGIN
UPDATE [dbo].[Languages] SET
Name = @Name, UpdatedOn = SYSDATETIME()
WHERE ID = @ID
END
ELSE IF @StatementType = 'Delete'
BEGIN
DELETE FROM [dbo].[Languages] WHERE ID = @ID
END
END