SQL Server:如果条件在存储过程中不起作用
我在存储过程中有以下条件来确定要使用哪个WHERE子句:SQL Server:如果条件在存储过程中不起作用,sql,sql-server,stored-procedures,Sql,Sql Server,Stored Procedures,我在存储过程中有以下条件来确定要使用哪个WHERE子句: IF (@communityDesc = 'All Areas') BEGIN WHERE V_CONSTAT_ACTUAL_DATES.AREA_DESC IN (SELECT name FROM dbo.splitstring(@communityDesc)) AND V_CONSTAT_ACTUAL_DATES.DA
IF (@communityDesc = 'All Areas')
BEGIN
WHERE V_CONSTAT_ACTUAL_DATES.AREA_DESC IN (SELECT name
FROM dbo.splitstring(@communityDesc))
AND V_CONSTAT_ACTUAL_DATES.DATE_TO_END >= GETDATE()
END
ELSE
BEGIN
WHERE V_CONSTAT_ACTUAL_DATES.DATE_TO_END >= GETDATE()
END
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
但我有一长串的错误:
Msg 156,级别15,状态1,过程GetProductionSchedule,第256行
关键字“WHERE”附近的语法不正确
Msg 156,级别15,状态1,过程GetProductionSchedule,第256行
关键字“AND”附近的语法不正确
我做错了什么?不能用条件分隔查询。你得做些类似的事情
if(@communityDesc = 'All Areas')
BEGIN
SELECT * FROM Table
WHERE V_CONSTAT_ACTUAL_DATES.AREA_DESC IN
(select name from dbo.splitstring(@communityDesc))
AND V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
END
else
BEGIN
SELECT * FROM Table
WHERE V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
END
您的另一个选择是有条件地生成查询:
DECLARE @Query VARCHAR(1000)
SET @Query = 'SELECT * FROM TABLE '
if(@communityDesc = 'All Areas')
BEGIN
SET @Query = @Query +
'WHERE V_CONSTAT_ACTUAL_DATES.AREA_DESC IN (select name from dbo.splitstring(@communityDesc)) AND V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE() '
END
ELSE
SET @Query = @Query +
'WHERE V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE() '
BEGIN
SET @Query = @Query + 'ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END'
END
exec sp_executesql @Query
不能通过条件分隔查询。你得做些类似的事情
if(@communityDesc = 'All Areas')
BEGIN
SELECT * FROM Table
WHERE V_CONSTAT_ACTUAL_DATES.AREA_DESC IN
(select name from dbo.splitstring(@communityDesc))
AND V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
END
else
BEGIN
SELECT * FROM Table
WHERE V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
END
您的另一个选择是有条件地生成查询:
DECLARE @Query VARCHAR(1000)
SET @Query = 'SELECT * FROM TABLE '
if(@communityDesc = 'All Areas')
BEGIN
SET @Query = @Query +
'WHERE V_CONSTAT_ACTUAL_DATES.AREA_DESC IN (select name from dbo.splitstring(@communityDesc)) AND V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE() '
END
ELSE
SET @Query = @Query +
'WHERE V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE() '
BEGIN
SET @Query = @Query + 'ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END'
END
exec sp_executesql @Query
或者只是把所有的逻辑放在一个地方
SELECT *
FROM [Table]
WHERE V_CONSTAT_ACTUAL_DATES.DATE_TO_END >= GETDATE()
AND (@communityDesc = 'All Areas'
OR V_CONSTAT_ACTUAL_DATES.AREA_DESC IN (SELECT name
FROM dbo.splitstring(@communityDesc)))
我不确定你的逻辑是否正确。。如果@communityDesc不等于我更新答案以反映我的意思的所有方面,您可能需要将其拆分。或者将所有逻辑放在一个WHERE中
SELECT *
FROM [Table]
WHERE V_CONSTAT_ACTUAL_DATES.DATE_TO_END >= GETDATE()
AND (@communityDesc = 'All Areas'
OR V_CONSTAT_ACTUAL_DATES.AREA_DESC IN (SELECT name
FROM dbo.splitstring(@communityDesc)))
我不确定你的逻辑是否正确。。如果@communityDesc不等于我更新了答案以反映我的意思的所有区域,您可能需要拆分它。您可能应该这样写
-- your preceding select statements followed by this line
SELECT * FROM V_CONSTAT_ACTUAL_DATES
WHERE
(
@communityDesc = 'All Areas' AND
V_CONSTAT_ACTUAL_DATES.AREA_DESC IN (select name from dbo.splitstring(@communityDesc)) AND
V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
)
OR
( @communityDesc <> 'All Areas' AND V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
)
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
这也可以简化为
-- your preceding select statements followed by this line
SELECT * FROM V_CONSTAT_ACTUAL_DATES
WHERE
V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
AND NOT
(
@communityDesc <> 'All Areas' OR
V_CONSTAT_ACTUAL_DATES.AREA_DESC not IN (select name from dbo.splitstring(@communityDesc))
)
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
可以进一步优化,如下所示
-- your preceding select statements followed by this line
SELECT * FROM V_CONSTAT_ACTUAL_DATES
LEFT JOIN (select name from dbo.splitstring(@communityDesc) Temp
ON Temp.name=V_CONSTAT_ACTUAL_DATES.AREA_DESC
WHERE
V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
AND NOT
(
@communityDesc <> 'All Areas' OR Temp.Name is null
)
)
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
你应该这样写
-- your preceding select statements followed by this line
SELECT * FROM V_CONSTAT_ACTUAL_DATES
WHERE
(
@communityDesc = 'All Areas' AND
V_CONSTAT_ACTUAL_DATES.AREA_DESC IN (select name from dbo.splitstring(@communityDesc)) AND
V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
)
OR
( @communityDesc <> 'All Areas' AND V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
)
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
这也可以简化为
-- your preceding select statements followed by this line
SELECT * FROM V_CONSTAT_ACTUAL_DATES
WHERE
V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
AND NOT
(
@communityDesc <> 'All Areas' OR
V_CONSTAT_ACTUAL_DATES.AREA_DESC not IN (select name from dbo.splitstring(@communityDesc))
)
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END
可以进一步优化,如下所示
-- your preceding select statements followed by this line
SELECT * FROM V_CONSTAT_ACTUAL_DATES
LEFT JOIN (select name from dbo.splitstring(@communityDesc) Temp
ON Temp.name=V_CONSTAT_ACTUAL_DATES.AREA_DESC
WHERE
V_CONSTAT_ACTUAL_DATES.DATE_TO_END>=GETDATE()
AND NOT
(
@communityDesc <> 'All Areas' OR Temp.Name is null
)
)
ORDER BY V_CONSTAT_ACTUAL_DATES.DATE_TO_END