SELECT WHERE中的条件NOT IN子句

时间:2016-07-05 14:28:16

标签: sql-server sql-server-2008 tsql stored-procedures

SQL Server 2008 R2的存储过程是否可以为NOT IN语句SELECT提供条件WHERE子句?我有三个表,EmployeeTestingATestingB,其中employeeIDTestingATestingB中的外键:

CREATE TABLE [dbo].[Employee](
    [employeeID] [int] IDENTITY(9500,1) NOT NULL,
    [firstName] [nvarchar](50) NOT NULL,
    [middleName] [nvarchar](50) NULL,
    [lastName] [nvarchar](50) NOT NULL,
    [suffix] [nvarchar](50) NULL,
    [createdDate] [datetime] NOT NULL CONSTRAINT [DF_Employee_createdDate]  DEFAULT (getdate()),
 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED 
(
    [employeeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[TestingA](
    [testingA_ID] [int] IDENTITY(1,1) NOT NULL,
    [employeeID] [int] NOT NULL,
    [isComplete] [bit] NULL CONSTRAINT [DF_TestingA_isComplete]  DEFAULT ((0)),
    [createdDate] [datetime] NOT NULL CONSTRAINT [DF_TestingA_createdDate]  DEFAULT (getdate()),
 CONSTRAINT [PK_TestingA] PRIMARY KEY CLUSTERED 
(
    [testingA_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[TestingB](
    [testingB_ID] [int] IDENTITY(1,1) NOT NULL,
    [employeeID] [int] NOT NULL,
    [isComplete] [bit] NULL CONSTRAINT [DF_TestingB_isComplete]  DEFAULT ((0)),
    [createdDate] [datetime] NOT NULL CONSTRAINT [DF_TestingB_createdDate]  DEFAULT (getdate()),
 CONSTRAINT [PK_TestingB] PRIMARY KEY CLUSTERED 
(
    [testingB_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

我需要在SELECT表上Employee根据存储过程的输入参数为NOT IN子句添加条件WHERE

@Type nvarchar(50)

根据@Type的值,我想在NOT INTestingA上执行TestingB。所以它是这样的:

select e.employeeId from Employee e
    where 
        -- If @Type = 'TestingA' Then 
        -- e.employeeID NOT IN (select ta.employeeID from TestingA ta)
        -- End
        -- Else             
        -- e.employeeID NOT IN (select tb.employeeID from TestingB ta)
        -- End

如何在Transact-SQL中执行此操作?感谢。

3 个答案:

答案 0 :(得分:3)

/* /wp-content/plugins/ajax-test/ajax-test.php */
/**
 * Plugin Name: Ajax Test
 * Plugin URI: http://mysite.co.uk
 * Description: This is a plugin that allows us to test Ajax functionality in WordPress
 * Version: 1.0.0
 * Author: Me
 * Author URI: http://mysite.co.uk
 * License: GPL2
 */
add_action( 'wp_enqueue_scripts', 'ajax_test_enqueue_scripts' );
function ajax_test_enqueue_scripts() {
 wp_enqueue_script( 'test', plugins_url( '/test.js', __FILE__ ), array('jquery'), '1.0', true );
    wp_localize_script( 'test', 'MYajax', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}



# /wp-content/plugins/ajax-test/test.js
jQuery(document).ready( function($) {
 $.ajax({
    url: MYajax.ajax_url,
    type : 'get',
    data : {
        action : 'example_ajax_request'
    },
    success: function( response ) {
        console.log(response);
    }
 })
})

<?php /* page-test.php */
 get_header(); ?>

<?php 
function example_ajax_request() {
 if ( isset($_GET) ) {
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
        $fruit = $_GET['fruit'];
        echo $fruit;
    }
    die();
 }
}

add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
?>

答案 1 :(得分:1)

由于它是一个存储过程,我会做两个语句(它可能会提高性能)

select e.employeeId
from Employee e
where @Type = 'TestingA'
  AND not exists(select 1 from TestingA ta where ta.employeeID=e.employeeID)
  OR @Type != 'TestingA' 
  AND not exists(select 1 from TestingB tb where tb.employeeID=e.employeeID)

答案 2 :(得分:1)

使用此

select e.employeeId from Employee e
where 
     e.employeeID NOT IN (case when @Type = 'TestingA' Then 
      (select ta.employeeID from TestingA ta)
     Else             
     (select tb.employeeID from TestingB tb)
     End)

您也可以使用连接进行相同的查询,如下所示

select e.employeeId from Employee e
left join TestingA e1 on @Type='TestingA' and e.employeeId != e1.employeeID 
left join TestingB e2 on @Type!='TestingA' and e.employeeId != e2.employeeID