[英]Checking to see if an ID exists inside of a table using SQL Server 2008
I am having a little trouble with a select statement in SQL Server 2008. I am building a ticket sale application web site and I need to make sure tickets that have already been sold can't be searched. 我在使用SQL Server 2008中的select语句时遇到了一些麻烦。我正在构建售票应用程序网站,并且需要确保无法搜索已售出的票。 Here is my SQL stored procedure:
这是我的SQL存储过程:
spGetSeatBySection
@SectionID int
AS
SELECT
[SeatID],
[SectionID],
[SeatName],
[SeatPrice],
[SeatDesc],
[SeatIsActive],
FROM [myDataBase].[dbo].[Seats]
WHERE SectionID = @SectionID
AND SeatIsActive = 1
AND **IF EXISTS CLAUSE**;
I have tried a couple of the other methods that have been posted here like the if exists('table_name, 'column_name'). 我已经尝试了一些其他的方法,例如if exist('table_name,'column_name')。 Is there another way of checking this without redesigning my
SELECT
statement? 还有另一种无需重新设计
SELECT
语句即可检查此问题的方法吗?
Something like that with EXISTS : EXISTS就是这样的 :
SELECT
[SeatID],
[SectionID],
[SeatName],
[SeatPrice],
[SeatDesc],
[SeatIsActive],
FROM [myDataBase].[dbo].[Seats]
WHERE SectionID = @SectionID
AND SeatIsActive = 1
AND EXISTS (SELECT o.SeatID
FROM OrderItems o
WHERE o.SeatID = [Seats].SeatID )
You probably can do and this: 您可能可以做到这一点:
SELECT
s.[SeatID],
s.[SectionID],
s.[SeatName],
s.[SeatPrice],
s.[SeatDesc],
s.[SeatIsActive],
FROM [myDataBase].[dbo].[Seats] s
INNER JOIN OrderItems o ON o.SeatID = s.SeatID
WHERE s.SectionID = @SectionID
AND s.SeatIsActive = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.