当引用的列是char时,为什么nchar变量有效?

时间:2015-05-14 17:01:49

标签: sql tsql variables char varchar

这是一个基于好奇心的问题。我试图解决以下问题:

  1. 我的存储过程在运行1-2秒内产生结果,没有问题。
  2. 当我通过SSRS运行我的proc时,页面在加载时卡住,并且永远不会产生结果。需要大约30秒 - 一分钟才能解开并退出处理查询。
  3. 我在我的变量上引用/加入的任何表都具有SAME数据类型char(3)。
  4. 以下是代码:

    ALTER PROC [dbo].[Inventory_pr_rpt_Scanfile_InvalidColorSize]
    
    (
        @STORE CHAR(3)
    )
    AS
    
    
    --set statistics io on 
    
    
    /** TEST **/
    
        --DECLARE   @STORE CHAR(3)
        --SET       @STORE = '180'
    
    
    /** Drop existing temp table **/ 
    
        IF OBJECT_ID('tempdb..#TEMP_ScanBadData') IS NOT NULL
        DROP TABLE #TEMP_ScanBadData
    
        IF OBJECT_ID('tempdb..#TEMP_ScanDiscrepancy') IS NOT NULL
        DROP TABLE #TEMP_ScanDiscrepancy
    
        IF OBJECT_ID('tempdb..#TEMP_ScanFinalReport') IS NOT NULL
        DROP TABLE #TEMP_ScanFinalReport    
    
    
        CREATE TABLE #TEMP_ScanBadData (scan_store char(3)
                                          ,scan_sku char(45)
                                          ,scan_qty int
                                          ,errormessage varchar(255))
    
        CREATE TABLE #TEMP_ScanFinalReport(scan_store char(3)
                                          ,scan_sku char(45)
                                          ,scan_qty int
                                          ,storeOH_qty int
                                          ,errormessage varchar(255))   
    
        CREATE TABLE #TEMP_ScanDiscrepancy(scan_store char(3)
                                          ,scan_sku char(45)
                                          ,scan_qty int
                                          ,storeOH_qty int
                                          ,errormessage varchar(255))                             
    
    /** Does SKU exist in our system? (Valid color/size/style check) **/
    INSERT INTO #TEMP_ScanBadData (scan_store
                                          ,scan_sku
                                          ,scan_qty
                                          ,errormessage)
        SELECT scan_store
            ,scan_sku
            ,scan_qty
            ,'SKU does not exist.' as errormessage           
        FROM [LP].[dbo].[scan_file]
        WHERE scan_sku NOT IN (SELECT ItemID
                               FROM [Product].[dbo].[MasterSKU])
        AND scan_store = @STORE
    
    
    
    /** Does scanned SKU exist in store on hand? **/
        INSERT INTO #TEMP_ScanDiscrepancy (scan_store
                                          ,scan_sku
                                          ,scan_qty
                                          ,storeOH_qty
                                          ,errormessage)    
        SELECT aa.scan_store 
              ,aa.scan_sku
              ,aa.scan_qty
              ,'' as storeOH_qty
              ,'Scanned SKU missing in store OH.' as errormessage
        FROM [LP].[dbo].[scan_file] aa
        FULL OUTER JOIN [Product].[dbo].[CurrentOnHand] bb
                ON aa.scan_sku = bb.ItemID
                and aa.scan_store = bb.Store 
        WHERE bb.ItemID IS NULL
        AND aa.scan_store = @STORE
        and bb.Store = @STORE
    
    
    /** Were any store OH SKU's missed in scan? **/
        INSERT INTO #TEMP_ScanDiscrepancy (scan_store
                                          ,scan_sku
                                          ,scan_qty
                                          ,storeOH_qty
                                          ,errormessage)    
        SELECT aa.Store   as 'scan_store'
              ,aa.ItemId  as 'scan_sku'
              ,aa.OnHandU as 'scan_qty'
              ,'' as storeOH_qty
              ,'SKU was not scanned.' as errormessage
        FROM [Product].[dbo].[CurrentOnHand] aa
        LEFT JOIN [LP].[dbo].[scan_file] bb
                ON aa.ItemID = bb.scan_sku
                and aa.Store = bb.scan_store 
        WHERE bb.scan_sku IS NULL
        AND aa.Store = @STORE
        and bb.scan_store = @STORE
    
    /** What are the qty differences between scanned and OH? **/
        INSERT INTO #TEMP_ScanDiscrepancy (scan_store
                                          ,scan_sku
                                          ,scan_qty
                                          ,storeOH_qty
                                          ,errormessage)    
        SELECT aa.scan_store
            ,aa.scan_sku
            ,aa.scan_qty
            ,bb.OnHandU as 'storeOH_qty'
            ,'QTY does not match between scan and store OH.' as errormessage
        FROM [LP].[dbo].[scan_file] aa
        JOIN [Product].[dbo].[CurrentOnHand] bb
            ON bb.ItemId = aa.scan_sku
            AND aa.scan_store = bb.Store        
        WHERE bb.Store = @STORE
        AND bb.OnHandU <> aa.scan_qty   
        AND aa.scan_store = @STORE
    
    
          INSERT INTO #TEMP_ScanFinalReport (scan_store
          ,scan_sku
          ,scan_qty
          ,storeOH_qty
          ,errormessage)    
          SELECT scan_store 
              ,scan_sku 
              ,scan_qty 
              ,storeOH_qty 
              ,errormessage 
          FROM #TEMP_ScanDiscrepancy    
    
        INSERT INTO #TEMP_ScanFinalReport (scan_store
          ,scan_sku
          ,scan_qty
          ,storeOH_qty
          ,errormessage)    
          SELECT scan_store 
              ,scan_sku 
              ,scan_qty 
              ,'' as storeOH_qty 
              ,errormessage 
          FROM #TEMP_ScanBadData    
    
          SELECT *
          FROM #TEMP_ScanFinalReport 
    

    解决方案:在尝试VARCHAR(3)和CHAR(3)后,我尝试了NCHAR(3),这很有用!

    我的问题是:为什么?如果我查看了我引用的每个表,并看到Store的列始终是CHAR(3),我创建的所有临时表都是CHAR(3),为什么NCHAR(3)有效?

    我觉得它与SSRS如何将文本变量传递到存储过程中有关...但是对于我如何在sql服务器上运行存储过程仍然没有意义。变量设置为CHAR,VARCHAR和NCHAR。为什么这三个都在sql server上工作,但只有NCHAR通过SSRS使用变量?

    表格信息:

    1. [LP]。[DBO]。[scan_file]
      • scan_store CHAR(3)
    2. [产品]。[DBO]。[CurrentOnHand]
      • Store VARCHAR(4)

0 个答案:

没有答案