-- สร้างตารางใหม่
CREATE TABLE [ dbo ] . [ MIL_New ] (
[ MILday ] [ smallint ] NOT NULL ,
[ MILmonth ] [ smallint ] NOT NULL ,
[ MILyear ] [ smallint ] NOT NULL ,
[ MILtype ] [ nvarchar ] ( 2 ) NOT NULL ,
[ MILvnos ] [ nvarchar ] ( 15 ) NOT NULL ,
[ MILcus ] [ nvarchar ] ( 15 ) NOT NULL ,
[ MILstk ] [ nvarchar ] ( 25 ) NULL ,
[ MILsto ] [ nvarchar ] ( 15 ) NULL ,
[ MILstoMT ] [ nvarchar ] ( 15 ) NULL ,
[ MILlistNo ] [ smallint ] NOT NULL ,
[ MILquan ] [ float ] NULL ,
[ MILquanP2 ] [ float ] NULL ,
[ MILadisc ] [ money ] NULL ,
[ MILcog ] [ money ] NULL ,
[ MILconv ] [ real ] NULL ,
[ MILdiscA ] [ money ] NULL ,
PRIMARY KEY ( MILvnos , MILlistNo ) -- กำหนด Primary Key
) ;
-- การนำข้อมูลจาก MIL ไปยัง MIL_New
MERGE [ dbo ] . [ MIL_New ] AS target
USING [ dbo ] . [ MIL ] AS source
ON target . MILvnos = source . MILvnos AND target . MILlistNo = source . MILlistNo
WHEN MATCHED THEN
UPDATE SET
target . MILday = source . MILday ,
target . MILmonth = source . MILmonth ,
target . MILyear = source . MILyear ,
target . MILtype = source . MILtype ,
target . MILcus = source . MILcus ,
target . MILstk = source . MILstk ,
target . MILsto = source . MILsto ,
target . MILstoMT = source . MILstoMT ,
target . MILquan = source . MILquan ,
target . MILquanP2 = source . MILquanP2 ,
target . MILadisc = source . MILadisc ,
target . MILcog = source . MILcog ,
target . MILconv = source . MILconv ,
target . MILdiscA = source . MILdiscA
WHEN NOT MATCHED THEN
INSERT ( MILday , MILmonth , MILyear , MILtype , MILvnos , MILcus , MILstk , MILsto , MILstoMT , MILlistNo , MILquan , MILquanP2 , MILadisc , MILcog , MILconv , MILdiscA )
VALUES ( source . MILday , source . MILmonth , source . MILyear , source . MILtype , source . MILvnos , source . MILcus , source . MILstk , source . MILsto , source . MILstoMT , source . MILlistNo , source . MILquan , source . MILquanP2 , source . MILadisc , source . MILcog , source . MILconv , source . MILdiscA ) ;
คัดลอก