聚热点 juredian

[资料表设计] 若insert新资料前,要检查原资料表内是否已有「同一组」的资料 一起帮忙 ...

求教大伙儿一个材料表整体规划的难题,有一情景:

一笔材料增加到到达站材料表(@Target)以前,要先查验序号(no)是不是已存有,

沒有就立即增加,有就分辨欲插进的Value是不是与已存有的Value同一组;

而分辨Value是不是一个组是否要用参照Table(@RefTable)来纪录是不是属於同一组?

像英语的语法中的@RefTable ([Value] char(1), [Group] char(1))那样?

或是说有更合适的作法?

MSSQL英语的语法以下:

--要插进的到达站Table

DECLARE @Target TABLE ([no] char(3), [Value] char(1))

INSERT INTO @Target ([no], [Value]) SELECT "101","A"

INSERT INTO @Target ([no], [Value]) SELECT "102","B"

INSERT INTO @Target ([no], [Value]) SELECT "103","D"

INSERT INTO @Target ([no], [Value]) SELECT "104","F"

INSERT INTO @Target ([no], [Value]) SELECT "105","G"

INSERT INTO @Target ([no], [Value]) SELECT "106","J"

SELECT * FROM @Target

--欲插进的data

SELECT "107","A" --@Target沒有107 => 可插进

SELECT "101","C" --@Target有101,但C与A是同一个Group => 不能插进

SELECT "102","C" --@Target有102,且C与B是不一样Group => 可插进

--排序参照的Table

DECLARE @RefTable TABLE ([Value] char(1), [Group] char(1))

INSERT INTO @RefTable ([Value],[Group]) SELECT "A", "1"

INSERT INTO @RefTable ([Value],[Group]) SELECT "B", "2"

INSERT INTO @RefTable ([Value],[Group]) SELECT "C", "1"

INSERT INTO @RefTable ([Value],[Group]) SELECT "D", "2"

INSERT INTO @RefTable ([Value],[Group]) SELECT "E", "3"

INSERT INTO @RefTable ([Value],[Group]) SELECT "F", "3"

INSERT INTO @RefTable ([Value],[Group]) SELECT "G", "4"

INSERT INTO @RefTable ([Value],[Group]) SELECT "H", "2"

INSERT INTO @RefTable ([Value],[Group]) SELECT "I", "4"

INSERT INTO @RefTable ([Value],[Group]) SELECT "J", "4"

INSERT INTO @RefTable ([Value],[Group]) SELECT "K", "1"

SELECT * FROM @RefTable

搜索建议: