用于整数值的大小写

时间:2017-02-17 05:28:43

标签: sql-server-2008

LocationID  LocationName
50          Sharjah
51          Qatar
55          Rigga
6           Dubai Mall

我需要创建以locationId为参数的sql查询, 如果locationId传递0,那么我想显示所有这些记录,否则按照locationId传递。 我尝试过case case但没有成功。

3 个答案:

答案 0 :(得分:0)

试试这个:这里@Parm会将值传递给您的脚本

SELECT * 
FROM TableName
WHERE (@Parm = 0 OR LocationID = @Parm)  

答案 1 :(得分:0)

DECLARE @locationId INT;

SELECT LocationID, LocationName
FROM yourTable
WHERE @locationId = 0 OR LocationID = @locationId

答案 2 :(得分:0)

我为你创建了一个存储过程。

CREATE TABLE #LOCATIONS
(
    LocationID  INT
    ,LocationName VARCHAR(100)
) 

INSERT INTO #LOCATIONS VALUES(
50,'Sharjah')
,(51,'Qatar')
,(55,'Rigga')
,(6,'Dubai Mall')

SELECT * FROM #LOCATIONS
GO

CREATE PROC LOC ( @LOC_ID INT )
AS
BEGIN

    IF(@LOC_ID = 0)
        SELECT * FROM #LOCATIONS
    ELSE

    SELECT * FROM #LOCATIONS WHERE LocationID = @LOC_ID
END
GO

EXEC LOC 0 -- Try Executing the stored proc like this
EXEC LOC 55 -- Try Executing the stored proc like this