我正在使用SQL Server 2008中的存储过程我编写了以下存储过程
CREATE PROCEDURE [dbo].[sp_]
@jobNumber VARCHAR(60),
@customerId VARCHAR(6),
@isType BIT
AS
BEGIN
DECLARE
@dynamicDir BIT,
@permit VARCHAR(6),
@fPSVersion VARCHAR(2)
SELECT @dynamicDir = [EDEXMIS].[dbo].[CM].[DynamicDirs] FROM [EDEXMIS].[dbo].[CM]
WHERE [EDEXMIS].[dbo].[CM].[CustID] = @customerId
IF @isType = 1
BEGIN
IF @dynamicDir = 0
BEGIN
SET @permit = 'FAX'
SET @fPSVersion = '='
END
ELSE
BEGIN
SET @permit = 'FAX'
SET @fPSVersion = '>'
END
END
Select * From MM where Permit =@permit and FPSVersion "Here i want the variable value @fpsVersion" 1
END
但我不知道如何在where子句中使用条件运算符。我想我做错了但是如果有其他方法可以做,请建议。任何帮助都会很棒。
答案 0 :(得分:4)
怎么样:
SELECT *
FROM MM
WHERE
Permit = @permit
AND (FPSVersion = 1 AND @dynamicDir = 0)
OR (FPSVersion > 1 AND @dynamicDir <> 0)
答案 1 :(得分:2)
试试这个 -
CREATE PROCEDURE [dbo].[usp_]
@customerId VARCHAR(6),
@isType BIT
AS BEGIN
DECLARE
@permit VARCHAR(6),
@fPSVersion VARCHAR(2)
SELECT
@fPSVersion = CASE WHEN t.DynamicDirs = 0 THEN '=' ELSE '>' END
, @permit = 'FAX'
FROM dbo.CM t
WHERE t.CustID = @customerId
AND @isType = 1
DECLARE @SQL NVARCHAR(MAX)
SELECT @SQL = '
SELECT *
FROM dbo.MM
WHERE Permit = ''' + @permit + '''
AND FPSVersion ''' + @fPSVersion + ''' 1'
EXEC sys.sp_executesql @SQL
END