2010-07-17 97 views
2

我在我的數據庫中有很多表,我正在收集下面的代碼的計算值,並希望將這些值插入到其他表中。我使用SELECT INTO方法,但數據庫告訴我「關鍵字INTO行附近的語法不正確......」。我相信有一些我失蹤,但不知道在哪裏。代碼看起來很好。這是我的代碼。任何幫助,將不勝感激。SELECT INTO advanced

SELECT (second.[cdate][email protected]) AS 'Date', first.[machine_no] AS 'No', 
tbl_machines.[manufacturer] As 'Manufacturer', 
tbl_machines.[type] As 'Machine Type',tbl_machines.[game_name] AS 'Game Name', 
tbl_machines.[accounting_denomination] AS 'Denom', 
(second.[turnover])-(first.[turnover]) AS 'Turnover', 
(second.[total win])-(first.[total win]) AS 'Total win', 
(second.[games played])-(first.[games played]) AS 'Games Played', 
(second.[Bill in])-(first.[Bill in]) AS 'Bill In', 
(second.[credit in])-(first.[credit in]) AS 'Credit IN', 
(second.[cancel credit])-(first.[cancel credit]) AS 'Cancel Credit', 
tbl_rate.[euro] AS 'euro rate', 
tbl_rate.[dollar] AS 'dollar rate' 
INTO tbl_daily 
FROM tbl.meter first,tbl.machines,tbl_rate 
INNER JOIN tbl_meter second ON first.[Machine_No] = second.[Machine_No] 
AND 
tbl_machines.[local_no]=first.[machine_no] 
WHERE first.[cDate] = @StartDate 
AND second.[cDate] = @EndDate 
AND tbl_rate.[cdate][email protected]; 

好吧,我用INSERT INTO語法,一切都很順利,但現在我有問題,日期時間。當我使用下面的sql命令時,我收到錯誤,它說「無法將數據類型位轉換爲日期時間」我試過Martin的轉換方法,但它是一樣的。

我的代碼是

INSERT INTO tbl_daily SELECT tbl_machines.[ID] AS 'ID', (second.[cdate][email protected]) AS 'CDate', first.[machine_no] AS 'No', 
tbl_machines.[manufacturer] As 'Manufacturer', 
tbl_machines.[type] As 'MachineType', 
tbl_machines.[game_name] AS 'GameName', 
tbl_machines.[accounting_denomination] AS 'Denom', 
(second.[turnover]-first.[turnover]) AS 'Turnover', 
(second.[total win]-first.[total win]) AS 'Totalwin', 
(second.[games played]-first.[games played]) AS 'GamesPlayed', 
(second.[credit in]-first.[credit in]) AS 'CreditIN', 
(second.[Bill in]-first.[Bill in]) AS 'BillIn', 
(second.[cancel credit]-first.[cancel credit]) AS 'CancelCredit', 
tbl_rate.[euro] AS 'eurorate', 
tbl_rate.[dollar] AS 'dollarrate' 
FROM tbl_meter first,tbl_machines,tbl_rate 
INNER JOIN tbl_meter second ON first.[Machine_No] = second.[Machine_No] AND tbl_machines.[local_no]=first.[machine_no] 
WHERE first.[cDate] = @StartDate AND second.[cDate] = @EndDate AND tbl_rate.[cdate][email protected]; 
+1

這是什麼SQL語言? – 2010-07-17 18:38:42

回答

0

注:以下答案寫假設的SQL Server。我刪除它收到VistaDB的澄清時,但在再次反刪除它reading

VistaDB的,可以看作是一個子集,Microsoft SQL Server的T-SQL的 。所有 我們的語法在SQL Server中, 支持,但周圍沒有

在這種情況下,其他的方式,我認爲它是安全地說,如果是在SQL Server無效,將在VistaDB的無效也?這在SQL Server中是無效的語法。

SELECT (second.[cdate][email protected]) AS 'Date' 

這段代碼的目的是什麼?它是否意味着一個布爾值? (即當列匹配變量時返回true)。如果是這樣的話,在SQL Server中最接近於此。

SELECT CAST((CASE WHEN second.[cdate][email protected] THEN 1 ELSE 0 END) AS BIT) AS 'Date' 

編輯從我看到它旨在

SELECT @enddate AS 'Date' 

此外,我沒有看到SELECT ... INTOlisted as a VistaDB command這裏的意見。它是否受到支持?

+0

我正在使用VistaDB。我正在爲我的客戶之一寫一個有小賭場的軟件。我正在計算每日基地的機器贏得或虧損。這真的很複雜。 – Hakan 2010-07-17 18:43:25

+0

@Hakan - 如果你擺脫了這一點'(second。[cdate] = @ enddate)AS'Date','它工作嗎?如果你只是做一個選擇並暫時擺脫'INTO tbl_daily'位,該怎麼辦?那它有用嗎? – 2010-07-17 19:08:08

+0

如果我不使用INTO tbl_daily它正在工作。我可以隨時創建表格,但是當我嘗試使用INTO時,我得到的是同樣的錯誤。 – Hakan 2010-07-17 19:27:32

0

如果您使用的是SQL Server,Insert Into更適用於插入到即時創建的表中。這是一個好的做法,但如果表已經存在,那麼我會用這個:

INSERT INTO table (column1, columns2, ...) 
SELECT 
    Value1, 
    Value2, 
    ... 
FROM ... 
+0

我沒有使用SQL Server,但我會嘗試。我正在使用Vistadb。我會讓你知道結果。 – Hakan 2010-07-17 18:44:57