如果我理解正確的問題,那麼我會簡單地創建一個初始的excel導入表,並在表中定義一個觸發器。導入表將生成您的ID,並且導入表上的觸發器可以將數據轉換爲您想要的格式。下表導入表的一個例子:
CREATE TABLE [dbo].[VegetableTableExcel](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Vegetables] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_VegetableTable] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
接下來的結果表:
CREATE TABLE [dbo].[Vegetables](
[ID] [int] NOT NULL,
[VegetableName] [nvarchar](50) NOT NULL
) ON [PRIMARY]
最後,在原裝進口的觸發器,從Excel將你的源代碼的導入:
CREATE TRIGGER SplitVegetable ON VegetableTableExcel
AFTER INSERT
AS
MERGE INTO Vegetables AS [Target]
USING (
SELECT [Source].ID
,Vegetables.VegetableName
FROM inserted [Source]
CROSS APPLY (
SELECT splitdata AS VegetableName
FROM dbo.SplitString([Source].Vegetables, '/')
) Vegetables
) AS [Source]
ON ([Target].[ID] = [Source].[ID]) AND ([Target].[VegetableName] = [Source].[VegetableName])
WHEN NOT MATCHED BY TARGET THEN
INSERT (
[ID]
,[VegetableName]
) VALUES
(
[Source].[ID]
,[Source].[VegetableName]
) ;
爲了完整,我已經包含了splitter函數,用於分割包含以下蔬菜名稱的字符串:
CREATE FUNCTION [dbo].[SplitString]
(
@string NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @output TABLE(splitdata NVARCHAR(MAX)
)
BEGIN
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @output (splitdata)
VALUES(SUBSTRING(@string, @start, @end - @start))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END
RETURN
END
查找標準拆分函數(有一大堆可用)。你的分隔符是'/'。這裏有一堆鏈接:http://sqlperformance.com/2012/07/t-sql-queries/split-strings – ZLK
請參閱編輯。它可能會突出顯示我需要更好的幫助 – user3697498