I have created an database file inside my asp.net application. Now from server explorer I tried to write a Stored procedure as follows
CREATE PROCEDURE insertData
(
@ID int,
@Name varchar(50),
@Address varchar(50),
@bit BIT OUTPUT
)
as
begin
declare @oldName as varchar(45)
declare @oldAddress as varchar(45)
set @oldName=(select EmployeeName from Employee where EmployeeName=@Name)
set @oldAddress=(select Address from Employee where Address=@Address)
if(@oldName <> @Name | @oldAddress <> @Address)
insert into Employee(EmpID,EmployeeName,Address)values(@ID,@Name,@Address)
SET @bit = 1
END
But this is giving me an error when I am saving it like Incorrect syntax near <
..
There are several things wrong here
if(@oldName <> @Name | @oldAddress <> @Address)
Won't work - maybe try
if @oldName <> @Name OR @oldAddress <> @Address
Of course, this will never be true because of the way the two queries above (which could and should have just been one query assigning both variables) make sure that the variables are always equal.
Ie:
set @oldName=(select EmployeeName from Employee where EmployeeName=@Name)
What can @oldName
be, if not equal to @Name
? (Okay, it could be NULL
, but then <>
is the wrong operator to use if NULL
is what you're checking for)
I think that what you wanted to write here was:
select @oldName=EmployeeName,@oldAddress = Address from Employee where EmpID = @ID
You should use OR
and not |
You can also do this instead of querying and checking each value separately, this will insert new row if name
and/or address
do not match for given empid
IF NOT EXISTS (
select * from Employee
where EmpID = @ID AND EmployeeName = @Name AND Address = @Address)
insert into Employee(EmpID,EmployeeName,Address)values(@ID,@Name,@Address)
SET @bit = 1
END
您可以使用!=代替“ <>”,也可以使用Or代替“ |”。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.