4

我正在使用 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 子句中连接条件运算符。我认为我做错了,但如果有其他方法可以做到,请提出建议。任何帮助都会很棒。

4

2 回答 2

4

怎么样:

SELECT *
FROM MM
WHERE
    Permit = @permit 
    AND (FPSVersion = 1 AND @dynamicDir = 0)
    OR (FPSVersion > 1 AND @dynamicDir <> 0)
于 2013-08-19T06:35:42.327 回答
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 
于 2013-08-19T06:36:01.943 回答