不能將值 NULL 插入列 '',表 '';列不允許有 Null 值。INSERT 失敗。

來源:互聯網
上載者:User

問題描述:
在執行將查詢結果插入已經存在的表中時,報錯如下:

不能將值 NULL 插入列 '列名',表 '要插入的表名';列不允許有 Null 值。INSERT 失敗。

執行的語句如下:

INSERT INTO bitcoin.dbo.CoinbaseTransaction(TransactionHash)SELECT dbo.BitcoinTransaction.TransactionHashFROM [bitcoin].[dbo].BitcoinTransaction INNER JOIN [CoinbaseTransaction] ON  [bitcoin].[dbo].[CoinbaseTransaction].[BitcoinTransactionId]=[bitcoin].[dbo].BitcoinTransaction.[BitcoinTransactionId]

分析:
原來我要插入的表缺少(key)主鍵,所以在要插入的表中選擇了一個欄位作為主鍵。

但是,增加了選擇一列作為主鍵後,依然顯示同樣的錯誤。

然後上網搜了一下:
一般EF的主鍵都是系統自動產生的。可能忽略掉了你的顯式賦值。
其實一般的套路是主鍵自增長,然後EF儲存時會自動給UID返回資料庫裡的最新值。
你手動維護一個主鍵是很麻煩的事。不建議做

alter table [bitcoin].[dbo].[CoinbaseTransaction] add CoinbaseId bigintINSERT INTO bitcoin.dbo.CoinbaseTransaction(TransactionHash)SELECT dbo.BitcoinTransaction.TransactionHashFROM [bitcoin].[dbo].BitcoinTransaction INNER JOIN [CoinbaseTransaction] ON  [bitcoin].[dbo].[CoinbaseTransaction].[BitcoinTransactionId]=[bitcoin].[dbo].BitcoinTransaction.[BitcoinTransactionId]
訊息 512,層級 16,狀態 1,第 2 行
子查詢返回的值不止一個。當子查詢跟隨在 =、!=、<、<=、>、>= 之後,或子查詢用作運算式時,這種情況是不允許的。

先檢查這兩個子查詢select [1YUE] from View_1,和select [LGMC]from View_1是不是在結果集中有好多條記錄。如果有的話用=號就不合適了。應該考慮用in關鍵字或者表的自身串連等方法解決。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.