2017-02-27 126 views
-1

任何熟悉此錯誤的人?非常感謝:)操作數類型衝突:smalldatetime與uniqueidentifier不兼容

我的代碼:

declare @FromDate date 
declare @ToDate date 
set @FromDate = '2017/01/01' 
set @ToDate = '2017/02/28' 

/*********************** INVOICE DETAILS **************************************/ 
--declare @FromDate date 
--declare @ToDate date 
--set @FromDate = '1/1/2017' 
--set @ToDate = '2/28/2017' 

if object_id('tempdb..#tmpInvoiceDetails') IS NOT NULL 
begin 
    drop table #tmpInvoiceDetails 
end 

create table dbo.#tmpInvoiceDetails 
    (
     [PurchaseOrderKey] uniqueidentifier, 
     [AgreementKey] varchar(40), 
     [CreatedDate] datetime, 
     [SalesOrderID] varchar(30), 
     [ConfirmedInvoiceNumber] varchar(50), 
     [CurrencyCode] varchar(5), 
     [ExtendedLineItemAmount] int, 
     [ExtendedLineItemAmountUSD] int 
    ) 

insert into dbo.#tmpInvoiceDetails 
    (
     [PurchaseOrderKey], 
     [AgreementKey], 
     [CreatedDate], 
     [SalesOrderID], 
     [ConfirmedInvoiceNumber], 
     [CurrencyCode], 
     [ExtendedLineItemAmount], 
     [ExtendedLineItemAmountUSD] 
    )  
select 
    [CreatedDate], 
    [SalesOrderID], 
    [PurchaseOrderKey], 
    [AgreementKey], 
    [ConfirmedInvoiceNumber], 
    [CurrencyCode], 
    [ExtendedLineItemAmount], 
    [ExtendedLineItemAmountUSD] 
from [dbo].[SalesOrderLineItem] 
where [CreatedDate] >= @FromDate 
    and [CreatedDAte] < @ToDate 

--create index on temp audit table 

create nonclustered index [tmpIDX] on [dbo].[#tmpInvoiceDetails] 
(
    [CreatedDate] ASC, 
    [PurchaseOrderKey] ASC 
) 

我得到這個錯誤:

Msg 206, Level 16, State 2, Line 31
Operand type clash: smalldatetime is incompatible with uniqueidentifier.

我已經試圖改變日期格式,但仍然是相同的。

+1

請仔細閱讀[如何問一個很好的SQL問題](http://meta.stackoverflow.com/questions/271055/tips-for-asking-a-good-structured-query-language-sql-question/271056)和[如何創建一個MCVE](http:/ /stackoverflow.com/help/mcve) – Shnugo

+0

我的道歉,我編輯了我的帖子。 :) –

回答

1

你有錯爲了您的列:

insert into dbo.#tmpInvoiceDetails 
    (
     [PurchaseOrderKey], 
     [AgreementKey], 
     [CreatedDate], 
     ... 
select 
    [CreatedDate], 
    [SalesOrderID], 
    [PurchaseOrderKey], 
    ... 

所以,你要插入CreatedDate到PurchaseOrderKey,進入的SalesOrderID等AgreementKey結果

+0

完美!非常感謝你!我的問題已解決!謝謝sooooooo多! :d –

相關問題