存储过程中的条件 UNION
你好!
因此,在存储过程中,我想执行由参数决定的条件并集。 我怎样才能做到这一点?
这是我的“不起作用”程序:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spp_GetAdressesList]
@OnlyLinked bit = 1,
@ObligedId int = -1
AS
BEGIN
SELECT
[ID_ADRESS]
,[ID_ENT]
,[VOI_ADRESS]
,[NUM_ADRESS]
,[BTE_ADRESS]
,[CP_ADRESS]
,[VIL_ADRESS]
FROM [ADRESSES]
WHERE
(
(VIL_ADRESS != 'NC' AND VIL_ADRESS != '--')
AND
(@OnlyLinked = 0 OR ID_ENT is not null)
)
IF (@ObligedId != -1)
BEGIN
UNION
SELECT
[ID_ADRESS]
,[ID_ENT]
,[VOI_ADRESS]
,[NUM_ADRESS]
,[BTE_ADRESS]
,[CP_ADRESS]
,[VIL_ADRESS]
FROM [ADRESSES]
WHERE
ID_ADRESS = @ObligedId
END
END
因此,如果 @ObligedId est = a -1 我希望没有 UNION。
我用动态 varchar 查询做了这个,最后我用 exec 执行查询。但它显然效率较低,您可以使用动态查询进行sql注入(适用于asp.net应用程序)。我决定更改所有存储过程
不可能在 IF 子句中进行并集吗?
感谢所有答案,无一例外。
Bonjour!
So, in a stored procedure I would like to do a conditional union decided by a parameter.
How can I do that?
Here is my "doesn't work" procedure :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[spp_GetAdressesList]
@OnlyLinked bit = 1,
@ObligedId int = -1
AS
BEGIN
SELECT
[ID_ADRESS]
,[ID_ENT]
,[VOI_ADRESS]
,[NUM_ADRESS]
,[BTE_ADRESS]
,[CP_ADRESS]
,[VIL_ADRESS]
FROM [ADRESSES]
WHERE
(
(VIL_ADRESS != 'NC' AND VIL_ADRESS != '--')
AND
(@OnlyLinked = 0 OR ID_ENT is not null)
)
IF (@ObligedId != -1)
BEGIN
UNION
SELECT
[ID_ADRESS]
,[ID_ENT]
,[VOI_ADRESS]
,[NUM_ADRESS]
,[BTE_ADRESS]
,[CP_ADRESS]
,[VIL_ADRESS]
FROM [ADRESSES]
WHERE
ID_ADRESS = @ObligedId
END
END
So if @ObligedId est = a -1 I would like to doesn't have the UNION.
I made this with a dynamic varchar query, at the end I was executing the query with an exec. But it's apparently less efficient and you can make sql injection (It is for asp.net application) with dynamic queries. I decided to change all my stored procedures
It's not possible to do an union in a IF clause?
Thanks for all answers without exceptions..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常,要进行基于案例的联合,您会将伪转换
为
对于您的查询,这将变为
但是,由于在此特定查询中,数据来自同一个表,只是不同的过滤器,因此您将使用 OR 过滤器。 注意: 如果您使用过 UNION ALL,则无法以这种方式减少它,因为 UNION ALL 可能会保留重复项。对于 UNION(无论如何都会删除重复项),OR 缩减效果很好
Normally to do a case based union, you transform the pseudo
into
For your query, that becomes
However, since in this specific query, the data is from the same table just different filters, you would OR the filters instead. Note: if you had used UNION ALL, it can not be reduced this way because of possible duplicates that UNION ALL preserves. For UNION (which removes duplicates anyway), the OR reduction works just fine
您可以使用
where
子句来选择联合的任一端:在您的示例中,您可以完全省略
IF
语句,因为没有地址的 ID_ADDRESS 为 -1 。You could use a
where
clause to choose either end of the union:In your example, you could omit the
IF
statement entirely, since no address will have an ID_ADDRESS of -1.难道你不能像这样重写你的查询吗:
如果
@obligedid
等于-1,它将找不到ID,并且不会返回该行。如果它是有效的 ID,那么它将返回该行以及第一个查询中返回的行。Couldn't you just rewrite your query like this:
If
@obligedid
is equal to -1 it won't find the Id and there for won't return the row. If it is valid id then it will return the row along with the rows returned in the first query.我很好奇使用一个 select 然后使用两个 where 语句但用 or 分隔它们是否行不通
i'm curious would it not work to use one select and then use the two where statements but seperate them by an or like