| Author |
Topic |
|
mannahboy
Starting Member
11 Posts |
Posted - 2008-01-02 : 19:26:03
|
Can seem to get it right, can anybody spot what I'm missing?INSERT INTO dbo.StockMasterUPDATE(GroupCode)SELECT GroupCodeFROM dbo.StockMasterBAKWHERE (dbo.StockMasterBAK.StockCode = dbo.StockMasterUPDATE.StockCode) Thanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-01-02 : 19:34:02
|
| You need to join to the StockMasterUPDATE table in your SELECT statement.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
mannahboy
Starting Member
11 Posts |
Posted - 2008-01-02 : 19:43:03
|
what would the syntax of the join be i tried:INSERT INTO dbo.StockMasterUPDATE (GroupCode)SELECT GroupCodeFROM dbo.StockMasterBAK join dbo.StockMasterUPDATEWHERE (StockCode = dbo.StockMasterUPDATE.StockCode) but doesnt pass |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-01-02 : 19:45:47
|
| INSERT INTO dbo.StockMasterUPDATE(GroupCode)SELECT GroupCodeFROM dbo.StockMasterBAK bJOIN dbo.StockMasterUPDATE uON b.StockCode = u.StockCodeTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
mannahboy
Starting Member
11 Posts |
Posted - 2008-01-02 : 19:54:39
|
| SQL says that I cannot insert nulls into dbo.StockMasterUPDATE but I have looked in the modify view and allow nulls is checked. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-01-02 : 20:00:38
|
| GroupCode must be set to NOT NULL in StockMasterUPDATE table. Switch it to NULL.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
mannahboy
Starting Member
11 Posts |
Posted - 2008-01-02 : 20:16:17
|
| It actually looks like another table is related to it, is there a way so that the select part of the statement does not select the null values? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-01-02 : 20:18:42
|
| Yes. But please show us a data example so that we can be sure to provide you with the correct statement.Just from what I understand though, it would be WHERE b.GroupCode IS NOT NULL.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
mannahboy
Starting Member
11 Posts |
Posted - 2008-01-02 : 20:51:04
|
| Have tried this:INSERT INTO dbo.StockMasterUPDATE(GroupCode)SELECT GroupCodeFROM dbo.StockMasterBAK bJOIN dbo.StockMasterUPDATE uON b.StockCode = u.StockCodeWHERE b.GroupCode IS NOT NULLBut sql returns an error like this:'Cannot insert the value NULL into column 'ClassInclude' table diabey_db.dbo.StockMasterUPDATE; column does not allow nulls', insert fails.Below is some sample data.dbo.StockMasterBAK TableStockCode GroupCode--------- ---------CH001 NULLCH002 NULLCH003 NULLCH004 NULLCH005 GR34 CH006 GR34 CJ001 GR29 CJ002 GR29 CJ003 GR29 CJ004 GR29 dbo.StockMasterUPDATE TableStockCode GroupCode--------- ---------CH001 NULLCH002 NULLCH003 NULLCH004 NULLCH005 NULLCH006 NULLCH007 NULLCJ001 NULLCJ002 NULLCJ003 NULLCJ004 NULLIm trying to get values in StockMasterBAK that are not null to insert into StockMasterUPDATE. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-01-02 : 20:55:14
|
| I think you want an UPDATE of your table rather than an INSERT:UPDATE bSET GroupCode = u.GroupCodeFROM dbo.StockMasterBAK bJOIN dbo.StockMasterUPDATE uON b.StockCode = u.StockCodeWHERE b.GroupCode IS NOT NULLNext time, please show us this detailed information so that we don't waste time on an INSERT when you really need an UPDATE.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/ |
 |
|
|
|