如何设置等于CTE表结果的局部变量值?

时间:2019-01-31 04:03:58

标签: sql sql-server tsqlt

我有一个问题 我们有这样的表HR

use std::net::{Ipv4Addr};
use std::any::Any;


//this struct is made to show the device-type-specific configurable items
struct RaspberryPiCfg {
    led_pin : u8,
    //GPIO pin connected to the LED strip pixels (must support PWM)
    led_freq_hz : u32,
    //LED signal frequency in Hz (usually 800kHz)
    led_dma : u8,
    //DMA channel used for generating PWM signal (try 5)
    brightness : u8,
    //Brightness of LED strip between 0 and 255"
    led_invert : bool,
    //Set True if using an inverting logic level converter
    software_gamma_correction : bool
    //Set to True because Raspberry Pi doesn't use hardware dithering
    }

//this creates a default function which sets all configurable items
impl Default for RaspberryPiCfg {
    fn default() -> RaspberryPiCfg {
        RaspberryPiCfg {
            led_pin : 18,
            led_freq_hz : 800_000,
            led_dma : 5,
            brightness : 255,
            led_invert : true,
            software_gamma_correction : true
        }
    }
}
 //this struct is made to show the device-type-specific configurable items
 struct Esp8266Cfg {
    udp_ip : Ipv4Addr,
    //IP address of the ESP8266. Must match IP in ws2812_controller.ino
    udp_port : u16,
    //Port number used for socket communication between Python and ESP8266"
    software_gamma_correction : bool
    //Set to True because Raspberry Pi doesn't use hardware dithering
}


//this creates a default function which sets all configurable items
impl Default for Esp8266Cfg {
   fn default() -> Esp8266Cfg {
        Esp8266Cfg {
            udp_ip : Ipv4use std::net::{Ipv4Addr};
            udp_port : 7777,
            software_gamma_correction : false
        }
    }
}
//this struct is made to show the device-type-specific configurable items
struct BlinkstickCfg {
    software_gamma_correction : bool
    //Set to True because BlinkstickCfg doesn't use hardware dithering
}


//this creates a default function which sets all configurable items
impl Default for BlinkstickCfg {
    fn default() -> BlinkstickCfg {
        BlinkstickCfg {
            software_gamma_correction : true
        }
    }
}

enum DeviceType {
    ESP8266,
    RASPBERRY_PI,
    BLINKSTICK
}

enum StatusType {
   ERROR,
   OK
}

//this struct is made to show the configurable items that are relevant
//for all devices
struct Devicecfg {
    use_gui : bool,
    //Whether or not to display a PyQtGraph GUI plot of visualization
    display_fps : bool,
    //Whether to display the FPS when running (can reduce performance)
    pixel_num : u8,
    //Number of pixels in the LED strip (must match ESP8266 firmware)
    gamma_table_path : String,
    //Location of the gamma correction table"
    mic_rate : u32,
    //Sampling frequency of the microphone in Hz
    fps : u8,
    //Desired refresh rate of the visualization (frames per second)
    min_led_fps : u32,
    //Frequencies below this value will be removed during audio processing
    max_led_fps : u32,
    //Frequencies above this value will be removed during audio processing
    device_type : DeviceType//,
    //device_cfg : T
}

//I am trying to create a trait to implement which I could use to create
//an object which when referenced represents the configuration of 
//both a devices device-type-specific specs and the specs that are not
//specific to the type of device being configured. 
trait DeviceSpec {
    type specs;
    fn setSpecs(&self);
}

impl DeviceSpec for Devicecfg {
    fn setSpecs(&self) {
        match self.device_type {
            DeviceType::ESP8266 => {
                self.specs = Esp8266Cfg::default();
            }
            DeviceType::RASPBERRY_PI => {
                self.specs = Ra\spberryPiCfg::default();
            }
            DeviceType::BLINKSTICK => {
                self.specs = BlinkstickCfg::default();
            }
        }
    }
}

impl Default for Devicecfg {
    fn default() -> Devicecfg {
        Devicecfg {
            use_gui : true,
            display_fps : true,
            pixel_num : 65,
            gamma_table_path : "directory".to_string(),
            mic_rate : 44_100,
            fps : 60,
            min_led_fps : 200,
            max_led_fps : 12_000,
            device_type : DeviceType::ESP8266,
            device_cfg : self.setSpecificCfg();
        }
    }
}

所以我想提取DISTINCT emp.manager,然后按行选择它们,并将它们赋予一个变量,假设为mgrname。我在SET @mgrname语法时出错?

EmpName    Empmanager
Jack        Jane
Tom         Jane 
Tony        Victor
Isabella    Hugo
Anna        Victor

3 个答案:

答案 0 :(得分:0)

select 'Jack'      as Empname ,  'Jane'   as empmanager
into #temp1  union all 
select 'Tom'       as Empname ,  'Jane'   as empmanager union all  
select 'Tony'      as Empname ,  'Victor' as empmanager union all 
select 'Isabella'  as Empname ,  'Hugo'   as empmanager union all 
select 'Anna'      as Empname ,  'Victor' as empmanager  


DECLARE @count INT
DECLARE @mgrname VARCHAR(20)
SET @count = 3

SELECT DISTINCT Empmanager into #temp2 FROM #temp1


set @mgrname =   (SELECT Empmanager  FROM (
SELECT ROW_NUMBER() OVER (ORDER BY empmanager ASC) AS RowNumber,
*
FROM #temp2
) AS RESULT
WHERE RowNumber = @count)  -- FIND THE CURRENT MANAGER NAME

select @mgrname as CurrentManagername

输出:

   CurrentManagername
    Victor

您可以改为创建一个临时表,并使用该表进行引用,直到会话处于活动状态。

答案 1 :(得分:0)

DECLARE @count INT
DECLARE @mgrname VARCHAR(20)
SET @count = 3
;WITH  MGRlist as(
    SELECT DISTINCT Empmanager FROM HR)
    SELECT @mgrname= Empmanager  FROM (
    SELECT ROW_NUMBER() OVER (ORDER BY Empmanager ASC) AS RowNumber,
    *
    FROM MGRlist
    ) AS RESULT
    WHERE RowNumber = @count

答案 2 :(得分:0)

在这种情况下,如果您想获得唯一的经理姓名,则不需要CTE

您正在尝试在VARCHAR数据类型中插入多个值

DECLARE @mgrname VARCHAR(20)

您应该创建一个VARCHAR类型的变量,而不是创建TABLE变量,该变量可以容纳多个值,如下所示。

DECLARE @mgrname TABLE(ManagerName VARCHAR(100))

要获取特定数量的不同经理名称,可以使用

SELECT DISTINCT TOP (@count)

您的最终查询应如下所示。

DECLARE @count INT
DECLARE @mgrname table (ManagerName VARCHAR(100))
SET @count = 3

INSERT INTO @mgrname
SELECT DISTINCT TOP (@count)  Empmanager FROM HR
ORDER BY Empmanager
--Now output is stored inside a table variable.
SELECT * FROM @mgrname