存储过程
CREATE PROCEDURE Insert_EstateAge
@EstateAge nvarchar(64)
AS
IF EXISTS(SELECT EstateAgeID FROM EstateAge WHERE EstateAge = @EstateAge)
BEGIN
SELECT -1
RETURN
END
INSERT INTO EstateAge(EstateAge)
VALUES(@EstateAge)
SELECT @@IDENTITY
RETURN
这是我写的存储过程,当输入 <1 或 2-3 这样的字符串就有问题了,怎么改啊?
------解决方案--------------------CREATE PROCEDURE Insert_EstateAge
@EstateAge nvarchar(64)
AS
begin
declare @sql varchar(4000)
set @sql =
'
IF EXISTS(SELECT EstateAgeID FROM EstateAge WHERE EstateAge ' + convert(nvarchar(20), @EstateAge) + ')
BEGIN
SELECT -1
RETURN
END
INSERT INTO EstateAge(EstateAge) VALUES(@EstateAge)
SELECT @@IDENTITY
'
exec sp_executesql @sql
end
go
注意这样改的话你传入的应该是一个表达式。
比如查找等于23的情况:exec Insert_EstateAge '=23 '