English 中文(简体)
INSERT服务器触发故障
原标题:SQL Server INSTEAD OF INSERT trigger failure

我有两个表格:

CREATE TABLE users
(
    id int PRIMARY KEY IDENTITY,
    firstname varchar(30),
    lastname varchar(30),
    age int
)

CREATE TABLE rejectedUsers
(
    id int PRIMARY KEY IDENTITY,
    firstname nvarchar,
    lastname nvarchar,
    age nvarchar
)

触发因素:

CREATE TRIGGER checkNumeric
ON users
INSTEAD OF INSERT
AS
INSERT INTO users SELECT firstname, lastname, age FROM inserted WHERE ISNUMERIC(age) = 1;
INSERT INTO rejectedUsers SELECT firstname, lastname, age FROM inserted WHERE ISNUMERIC(age) = 0;

-- goes fine
INSERT INTO users
VALUES
( Vlad ,  P , 21)

-- fails

INSERT INTO users
VALUES
( Chuck ,  Norris ,  abc )

第二篇发言给我留下了一个错误:

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value abc to data type int.

我期望触发器在<条码>上插入什么错误?

最佳回答

这是一种时间错误,而不是汇编错误。 类型与基表比较,无论你在哪一种助推逻辑上可以编造,而不是触发。 插入的假象是根据基数表创建的,不是根据你给某些武断说明的变量类型。

我不认为你能够提供某种透明度,简单地 s掉一个而不是一个触发点。 如果你想要让人们在他们能够向那里扔任何数据类型的地方提出临时询问,那么,你就不得不在一间中间桌上消费那些能够容纳 ju的人。 或者大多数人做些什么:接口可强制使用数据类型。 如果你采用一种接受“@age para amount”为“INT”的存储程序,那么,他们可以在桌旁任何地方带一刀切......年龄也没有什么价值,因为明天或第二天可能会发生变化。 为什么不接受出生日?

最后,请不要将栏目或变量定义为无期的var/n。 在某些情况下,这些是1个单元,而在另一些情况下是30个。 明确告诉大家你真的意思。

问题回答

Your code here:

INSERT INTO users
VALUES( Chuck ,  Norris ,  abc )

将尝试将<条码>abc插入<条码>>。 哪一类是<条码>int? 我相信,即使有<条码>。 INSTEAD OF INSERTstart in place, that Inserted table that life within thestart will have same structure as You Users table - that栏目age is an INT 页: 1

作为一般性建议,我将在always上明确指明你在后面插入哪一栏——因此,我将上述问题写成:

INSERT INTO users(firstname, lastname, age)
VALUES( Chuck ,  Norris , 71)

页: 1

INSERT INTO users 
    SELECT firstname, lastname, age 
    FROM inserted 
    WHERE ISNUMERIC(age) = 1;

为此:

INSERT INTO users(firstname, lastname, age)
    SELECT firstname, lastname, age 
    FROM inserted 
    WHERE ISNUMERIC(age) = 1;

That way, you won t have any nasty surprises if some table gets modified and suddenly gets a new column that your "generic" INSERT doesn t fill in...

我愿指出:

CREATE TABLE rejectedUsers
(
    id int PRIMARY KEY IDENTITY,
    firstname nvarchar,
    lastname nvarchar,
    age nvarchar
)

各位重新有效地制作一张桌子,上面有许多<代码>nvarchar(1)栏目,可以每个栏目打上最多1 ×的字面。 页: 1 页: 1

页: 1 自<代码>int外。 将年龄数据类型改为usersVARCHAR(10),或者如果你真的想这样做,或插入int

只是一位野gues,但也许会试图把被拒的辛迪加带到以下几个方面:

INSERT INTO rejectedUsers SELECT firstname, lastname, age FROM inserted WHERE ISNUMERIC(age) >= 0 and ISNUMERIC(age) > 120;

这将防止人们进入120岁以上或不到0岁的年龄(或你决定为未成年人/最高年龄的人),但理论上,如果允许通过var子,也会使 a子被拒。

如果你仍然有转换问题,那么就会尝试:

INSERT INTO rejectedUsers SELECT firstname, lastname, case when age between 0 and 120 then age else 0 end as age FROM inserted WHERE ISNUMERIC(age) = 0;

如果一米了解你的触发器使用正确(无论是否真正使用)的话,这将再次检查年龄在幅度之内,否则就会有零,但你会失去年龄范围内的数据。

帮助的希望

马 里





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签