SQL服务器触发concat

时间:2015-06-05 03:38:50

标签: sql sql-server conditional string-concatenation

我尝试编写一个连接3列的SQL Server触发器,但根据条件插入一个字符串代替第三列。

例如,当col 1插入concat col 1col 2col 3时。但是col 3 = 'DR' concat Drivecol 3 = 'ST' concat Street等等。

我可以为所有3列写入触发器,但是有条件的问题。

CREATE TRIGGER updateAddress
   ON [ARCHIVENEW].dbo.a11
   AFTER UPDATE
AS BEGIN
  SET NOCOUNT ON;
  IF UPDATE(STRNAME)
    SELECT CASE
      WHEN a11.STRTYPE = 'DR' THEN dbo.a11.STRFNAME = cast(dbo.a11.STRADDLF as varchar(2)) + ' ' + STRNAME + ' ' + 'Drive'
      WHEN a11.STRTYPE = 'RP' THEN dbo.a11.STRFNAME = cast(dbo.a11.STRADDLF as varchar(2)) + ' ' + STRNAME + ' ' + 'Ramp'
      WHEN a11.STRTYPE = 'EX' THEN dbo.a11.STRFNAME = cast(dbo.a11.STRADDLF as varchar(2)) + ' ' + dbo.a11.STRNAME + ' ' + 'Express Way'
      ELSE dbo.a11.STRFNAME = cast(dbo.a11.STRADDLF as varchar(2)) + ' ' + dbo.a11.STRNAME 
END

很抱歉,花了几天时间才回到此处并添加代码示例。我在' ='附近收到语法错误我尝试将STRFNAME设置为连接字符串。为了进一步澄清,col1是" 1234" col2是" Main"和col3是" ST"。当插入一行时,我喜欢用col4说" 1234 Main Street"。

3 个答案:

答案 0 :(得分:0)

样本表:

string CSVFilePathName = @file.DirectoryName + "\\" + file.Name;
string[] Lines = File.ReadAllLines(CSVFilePathName);
string[] Fields;
Fields = Lines[0].Split(new char[] { ',' });
int Cols = Fields.GetLength(0);
DataTable dt = new DataTable();
//1st row must be column names; force lower case to ensure matching later on.
for (int i = 0; i < Cols; i++)
    dt.Columns.Add(Fields[i].ToLower(), typeof(string));
DataRow Row;
for (int i = 1; i < Lines.GetLength(0); i++)
{
    Fields = Lines[i].Split(new char[] { ',' });
    Row = dt.NewRow();
    for (int f = 0; f < Cols; f++)
        Row[f] = Fields[f];
    dt.Rows.Add(Row);
}

此触发器为ColC的ColA,ColB汇总值,并为ColD验证ColB值。

CREATE TABLE dbo.TestTable(
    Id INT IDENTITY(1,1) PRIMARY KEY
    ColA VARCHAR(50),
    ColB VARCHAR(50),
    ColC VARCHAR(50),
    ColD VARCHAR(50)
);

谢谢,

答案 1 :(得分:0)

INSERT触发器触发时无法更改表。但是,您可以在插入记录之前创建触发器。

在emp上更新后创建TRIGGER emplog_update 对于每一行 INSERT INTO emplog(id,lastnmae,firstname,...)     选择NEW.id,NEW.lastname,NEW.firstname,NEW.gender,NEW.dob,NEW.marital,NEW.SSN,&#39; U&#39;,NULL,USER(),            CONCAT_WS(&#39;&#39 ;,                      (当new.id&lt;&gt; old.id然后&#39; id&#39;结束时),                      (new.lastname&lt;&gt; old.lastname然后&#39; lastname&#39; end的情况),                      (当new.firstname&lt;&gt; old.firstname然后&#39; firstname&#39; end)时,                      。 。 。                     );

答案 2 :(得分:0)

如果您的座右铭是插入第三列,那么使用计算列很好。这是一个例子。但正如我对问题的评论一样,我不明白,col3值是怎么来的?

为此,我使用col4来获得正确的结果,也在计算列定义中,它不参考自己。

    CREATE TABLE dbo.Products 
    (
        ProductID int IDENTITY (1,1) NOT NULL
      , col1 varchar(50)
      , col2 varchar(50)
      , col3 varchar(50)  
      , col4  AS case col3 when 'DR' then 'Drive' when 'ST' then 'Street' else col1 + col2 end 
     --while computed column define, you can use all other column, except own.
    );

    -- Insert values into the table.
    INSERT INTO dbo.Products (col1, col2,col3)
    VALUES ('a', 'a1','a2'), ('b', 'b1', 'DR'), ('c', 'c1', 'ST'), ('d', 'd1', 'd2');

    -- Display the rows in the table.
    SELECT ProductID, col1, col2, col3, col4
    FROM dbo.Products;

    select * from products

    drop table products